#include "TS800.h"
#include <iostream>
#include <fstream>
#include <TBranch.h>
#include <TChain.h>
#include <TFile.h>
#include <TList.h>
#include <TROOT.h>
#include <TStopwatch.h>
#include <TTree.h>
ClassImp(TS800);
TS800::TS800(const TS800 &s800) : TObject(s800)
{
((TS800&)s800).Copy(*this);
}
Bool_t TS800::Calculate(Long64_t entry)
{
if(!fChain){printf("TTree or TChain has not been initialized!!!\n"); return kFALSE;}
e1.Calculate(entry);
e2.Calculate(entry);
return kTRUE;
}
Int_t TS800::Calculate(Option_t* prefix,Option_t* path,Option_t* treeName,
Bool_t calcTracking,Option_t* option)
{
if(!fChain){printf("TTree or TChain has not been initialized!!!\n"); return -1;}
TStopwatch stopWatch;
stopWatch.Start();
Int_t nTrees = 1;
TTree *inifChain = fChain;
if(strcmp(fChain->ClassName(),"TChain")==0) nTrees = ((TChain*)fChain)->GetNtrees();
TS800 *newS800 = new TS800(*this);
newS800->crdc1.calc.pad = &newS800->crdc1.pad; newS800->crdc2.calc.pad = &newS800->crdc2.pad;
newS800->e1.fCon = &e1;
newS800->e2.fCon = &e2;
newS800->crdc1.pad.fCon = &crdc1.pad;
newS800->crdc2.pad.fCon = &crdc2.pad;
newS800->crdc1.calc.fCon = &crdc1.calc;
newS800->crdc2.calc.fCon = &crdc2.calc;
newS800->crdc1.fCon = &crdc1;
newS800->crdc2.fCon = &crdc2;
newS800->fpTrack.fCon = &fpTrack;
#ifdef TS800_OP_TCRDC
newS800->tcrdc1.fCon = &crdc1;
newS800->tcrdc2.fCon = &crdc2;
#endif
#if TS800_OP_TPPAC==1
newS800->tppac1.fCon = &tppac1;
newS800->tppac2.fCon = &tppac2;
#endif
#if (TS800_OP_TCRDC==1 || TS800_OP_TPPAC==1)
newS800->imTrack.fCon = &imTrack;
#endif
newS800->tof.fCon = &tof;
newS800->ic.fCon = ⁣
Long64_t curEntry = 0;
for(Int_t tt=0; tt<nTrees; tt++){
fChain->LoadTree(curEntry);
TTree *cTree = fChain->GetTree();
if(strcmp(cTree->GetUserInfo()->FindObject("Analysis State")->GetTitle(),"0")!=0) {
printf("* ERROR: Analysis State!=0\n"); return -2;
}
curEntry = curEntry+cTree->GetEntries()+1;
InitTree(cTree);
TString fileName;
TNamed *runNumber = (TNamed*)cTree->GetUserInfo()->FindObject("Run Number");
Int_t runNum = atoi(runNumber->GetTitle());
Char_t pLine[1000];
sprintf(pLine,"loadS800RunSettings(%i,ex->s800)",runNum);
gROOT->ProcessLine(pLine);
TString fPath(path);
TString fPrefix(prefix);
fileName = runNumber->GetTitle();
printf("Processing file: %s\n",fileName.Data());
if(prefix == "") fileName = fPath+"run"+fileName+"Cal.root";
else fileName = fPath+fPrefix+fileName+"Cal.root";
if(option=="RECREATE"){ cout << "Creating file: " << fileName << endl;
}else {option="UPDATE"; cout << "Updating file: " << fileName << endl;}
TString newTName((Char_t*)treeName);
TString oldTName(cTree->GetName());
TFile newFile(fileName,option);
TTree *newTree = new TTree("Cal","Calibrated "+oldTName+" Tree",99);
TNamed aState("Analysis State","1");
if(newFile.FindObject(oldTName+"Cal")!=0) printf("Tree already exists!\n");
else if(strcmp(newTName,"")==0){
newTName = oldTName+"Cal";
newTree->SetName(newTName);
newTree->GetUserInfo()->Add(&aState);
}else{
newTree->SetName(newTName+"Cal");
newTree->GetUserInfo()->Add(&aState);
}
for(Int_t j=0; j<cTree->GetUserInfo()->GetEntries(); j++){
TNamed *ntmp = (TNamed*)cTree->GetUserInfo()->At(j);
TNamed *ctmp = (TNamed*)ntmp->Clone();
if(strcmp(ntmp->GetName(),"Analysis State")!=0) newTree->GetUserInfo()->Add(ctmp);
}
newTree->Branch("s800","TS800",&newS800);
newTree->SetBranchAddress("s800",&newS800);
newS800->InitTree(newTree);
newS800->crdc1.SetYShift(runNum);
newS800->crdc2.SetYShift(runNum);
newS800->SetPIDShifts(runNum);
newS800->SetFpTrackShifts(runNum);
Long64_t nEntries = cTree->GetEntries();
for(Long64_t ientry=0; ientry<nEntries; ientry++){
Double_t prog = ((Double_t)ientry/nEntries)*100;
if(ientry%100==0){printf("Entry: %15lli \t%4.2lf %%\r",ientry,prog); cout.flush();}
newS800->crdc1.pad.Calibrate(ientry);
newS800->crdc2.pad.Calibrate(ientry);
newS800->crdc1.Calculate(ientry);
newS800->crdc2.Calculate(ientry);
newS800->e1.Calculate(ientry);
newS800->e2.Calculate(ientry);
if(calcTracking) newS800->fpTrack.Calculate(ientry);
#if TS800_OP_TCRDC==1
newS800->tcrdc1.Calculate(ientry);
newS800->tcrdc2.Calculate(ientry);
#endif
#if TS800_OP_TPPAC==1
#ifndef EXPERIMENT_02023
newS800->tppac1.Calculate(ientry);
newS800->tppac2.Calculate(ientry);
#endif
#if (TS800_OP_TCRDC==1 || TS800_OP_TPPAC==1)
newS800->imTrack.Calculate(ientry);
#endif
#endif
newS800->tof.Calculate(ientry);
newS800->ic.Calibrate(ientry);
newS800->ic.Calculate(ientry);
newTree->Fill();
}
newTree->AutoSave();
newFile.Close();
InitTree(inifChain);
}
stopWatch.Stop();
stopWatch.Print();
return 1;
}
Int_t TS800::Calibrate(Long64_t entry)
{
if(!fChain) { printf("TTree or TChain has not been initialized!!!\n"); return -1;}
e1.Calibrate(entry);
e2.Calibrate(entry);
e3.Calibrate(entry);
return 1;
}
Int_t TS800::Calibrate(Option_t* prefix,Option_t* path,Option_t* treeName,Option_t* option)
{
return 1;
}
void TS800::Copy(TObject &s800) const
{
TObject::Copy((TObject&)s800);
((TS800&)s800).fRandom.SetSeed(0);
((TS800&)s800).fName = fName;
((TS800&)s800).fTitle = fTitle;
((TS800&)s800).name = name;
((TS800&)s800).fAddRndm = fAddRndm;
((TS800&)s800).kBrhoA = kBrhoA;
((TS800&)s800).kBrho = kBrho;
((TS800&)s800).fEulerPhi = fEulerPhi;
((TS800&)s800).fEulerTheta = fEulerTheta;
((TS800&)s800).fEulerPsi = fEulerPsi;
e1.Copy(((TS800&)s800).e1);
((TS800&)s800).e1.parent = &((TS800&)s800);
e2.Copy(((TS800&)s800).e2);
((TS800&)s800).e2.parent = &((TS800&)s800);
e3.Copy(((TS800&)s800).e3);
((TS800&)s800).e3.parent = &((TS800&)s800);
crdc1.Copy(((TS800&)s800).crdc1);
((TS800&)s800).crdc1.parent = &((TS800&)s800);
crdc2.Copy(((TS800&)s800).crdc2);
((TS800&)s800).crdc2.parent = &((TS800&)s800);
s800map.Copy(((TS800&)s800).s800map);
fpTrack.Copy(((TS800&)s800).fpTrack);
((TS800&)s800).fpTrack.parent = &((TS800&)s800);
imTrack.Copy(((TS800&)s800).imTrack);
((TS800&)s800).imTrack.parent = &((TS800&)s800);
ic.Copy(((TS800&)s800).ic);
((TS800&)s800).ic.parent = &((TS800&)s800);
tcrdc1.Copy(((TS800&)s800).tcrdc1);
tcrdc2.Copy(((TS800&)s800).tcrdc2);
tppac1.Copy(((TS800&)s800).tppac1);
tppac2.Copy(((TS800&)s800).tppac2);
trigger.Copy(((TS800&)s800).trigger);
tof.Copy(((TS800&)s800).tof);
((TS800&)s800).tof.parent = &((TS800&)s800);
fHICShifts.Copy(((TS800&)s800).fHICShifts);
fHToFShifts[0].Copy(((TS800&)s800).fHToFShifts[0]);
fHToFShifts[1].Copy(((TS800&)s800).fHToFShifts[1]);
fHYtaShifts.Copy(((TS800&)s800).fHYtaShifts);
((TS800&)s800).Clear();
}
Bool_t TS800::GenPrime(Option_t *output,Option_t *treeName,Option_t *option)
{
printf("* Method not implemented.\n");
return kFALSE;
}
void TS800::InitClass()
{
kBrhoA = 0.0;
kBrho = 0.0;
fEulerPhi = 0;
fEulerTheta = 0;
fEulerPsi = 0;
fAddRndm = kTRUE;
e1.InitClass("e1", this);
e2.InitClass("e2", this);
e3.InitClass("e3", this);
cout << "* Plastic initialized . . . \t\t\t\t\t [OK] " <<endl;
crdc1.settag(S800_FP_CRDC_PACKET);
crdc2.settag(S800_FP_CRDC_PACKET);
crdc1.setchannels(TS800_FP_CRDC_CHANNELS);
crdc2.setchannels(TS800_FP_CRDC_CHANNELS);
crdc1.InitClass("crdc1",this);
crdc2.InitClass("crdc2",this);
cout << "* S800 CRDC's initialized . . \t\t\t\t\t [OK]" <<endl;
#if TS800_OP_TCRDC==1
tcrdc1.InitClass("tcrdc1");
tcrdc2.InitClass("tcrdc2");
tcrdc1.settag(S800_II_CRDC_PACKET);
tcrdc2.settag(S800_II_CRDC_PACKET);
tcrdc1.setchannels(TS800_II_CRDC_CHANNELS);
tcrdc2.setchannels(TS800_II_CRDC_CHANNELS);
#endif
#if TS800_OP_TPPAC==1
tppac1.InitClass("tppac1");
tppac2.InitClass("tppac2");
tppac1.settag(S800_II_TRACK_PACKET);
tppac2.settag(S800_II_TRACK_PACKET);
tppac1.setchannels(TS800_II_PPAC_CHANNELS);
tppac2.setchannels(TS800_II_PPAC_CHANNELS);
cout << "* S800 tracking PPAC's initialized . . . \t\t\t\t\t [OK]" <<endl;
#endif
fpTrack.InitClass("fpTrack", this);
cout << "* S800 Tracking initialized . . . \t\t\t\t\t [OK]" <<endl;
#if (TS800_OP_TCRDC==1 || TS800_OP_TPPAC==1)
imTrack.InitClass("imTrack", this);
cout << "* S800 IM Tracking initialized . . . \t\t\t\t\t [OK]" <<endl;
#endif
s800map.InitClass(this);
cout << "* S800 Map initialized . . . \t\t\t\t\t [OK]" <<endl;
ic.InitClass("ic",this);
cout << "* Ion chamber initialized . . . \t\t\t\t\t [OK]" <<endl;
trigger.InitClass("trigger");
cout << "* Trigger initialized . . . \t\t\t\t\t [OK]" <<endl;
tof.InitClass("tof", this);
cout << "* TOF initialized . . . \t\t\t\t\t [OK]" <<endl;
Clear();
Char_t name[1000],title[1000];
sprintf(name,"fHICShifts_%s",GetName());
sprintf(title,"PID Ion Chamber Shifts");
fHICShifts.SetNameTitle(name,title);
fHICShifts.SetBins(2000,1,2001);
sprintf(name,"fHToFShifts0_%s",GetName());
sprintf(title,"PID ToF Shifts XfpTar");
fHToFShifts[0].SetNameTitle(name,title);
fHToFShifts[0].SetBins(2000,1,2001);
sprintf(name,"fHToFShifts1_%s",GetName());
sprintf(title,"PID ToF Shifts TarFp");
fHToFShifts[1].SetNameTitle(name,title);
fHToFShifts[1].SetBins(2000,1,2001);
sprintf(name,"fHYtaShifts_%s",GetName());
sprintf(title,"FP Tracking Yta Shifts");
fHYtaShifts.SetNameTitle(name,title);
fHYtaShifts.SetBins(2000,1,2001);
}
void TS800::InitTree(TTree *tree)
{
fChain = tree;
fCurrent = -1;
b_fNwords = fChain->GetBranch("fNwords");
e1.InitTree(tree);
e2.InitTree(tree);
e3.InitTree(tree);
crdc1.InitTree(tree);
crdc2.InitTree(tree);
#if TS800_OP_TCRDC==1
tcrdc1.InitTree(tree);
tcrdc2.InitTree(tree);
b_tcrdc1 = fChain->GetBranch("tcrdc1");
b_tcrdc2 = fChain->GetBranch("tcrdc2");
#else
#endif
#if TS800_OP_TPPAC==1
tppac1.InitTree(tree);
tppac2.InitTree(tree);
b_tppac1 = fChain->GetBranch("tppac1");
b_tppac2 = fChain->GetBranch("tppac2");
#else
#endif
fpTrack.InitTree(tree);
b_fpTrack = fChain->GetBranch("fpTrack");
#if (TS800_OP_TCRDC==1 || TS800_OP_TPPAC==1)
imTrack.InitTree(tree);
b_imTrack = fChain->GetBranch("imTrack");
#else
#endif
ic.InitTree(tree);
trigger.InitTree(tree);
tof.InitTree(tree);
b_e1 = fChain->GetBranch("e1");
b_e2 = fChain->GetBranch("e2");
b_e3 = fChain->GetBranch("e3");
b_crdc1 = fChain->GetBranch("crdc1");
b_crdc2 = fChain->GetBranch("crdc2");
b_ic = fChain->GetBranch("ic");
b_trigger = fChain->GetBranch("trigger");
b_tof = fChain->GetBranch("tof");
}
void TS800::Clear(Option_t*)
{
fNwords = 0;
e1.Clear();
e2.Clear();
e3.Clear();
#if TS800_OP_TCRDC==1
tcrdc1.Clear();
tcrdc2.Clear();
#endif
#if TS800_OP_TPPAC==1
tppac1.Clear();
tppac2.Clear();
#endif
crdc1.Clear();
crdc2.Clear();
fpTrack.Clear();
#if (TS800_OP_TCRDC==1 || TS800_OP_TPPAC==1)
imTrack.Clear();
#endif
ic.Clear();
trigger.Clear();
tof.Clear();
}
void TS800::GetEntry(int i)
{
b_fNwords->GetEntry(i);
e1.GetEntry(i);
e2.GetEntry(i);
e3.GetEntry(i);
#if TS800_OP_TCRDC==1
tcrdc1.GetEntry(i);
tcrdc2.GetEntry(i);
#endif
#if TS800_OP_TPPAC==1
tppac1.GetEntry(i);
tppac2.GetEntry(i);
#endif
crdc1.GetEntry(i);
crdc2.GetEntry(i);
fpTrack.GetEntry(i);
ic.GetEntry(i);
trigger.GetEntry(i);
tof.GetEntry(i);
}
Bool_t TS800::LoadPIDShifts(const Char_t *fileName)
{
ifstream iFile(fileName,ios::in);
Char_t line[1000];
Int_t runNum;
Double_t icShift,xfpShift,tarShift;
if(!iFile.is_open()) {printf("* File could not be opened!\n"); return kFALSE;}
iFile.getline(line,1000);
while(line[0]=='#' || line[0]==' ') iFile.getline(line,1000);
Bool_t firstLine = kTRUE;
while (!iFile.eof()){
if(!firstLine) iFile.getline(line,1000);
firstLine = kFALSE;
sscanf(line,"%i %lf %lf %lf",&runNum,&xfpShift,&tarShift,&icShift);
fHICShifts.SetBinContent(runNum,icShift);
fHToFShifts[0].SetBinContent(runNum,xfpShift);
fHToFShifts[1].SetBinContent(runNum,tarShift);
}
iFile.close();
printf("* Loading %s PID stablization shifts . . . \t\t\t [OK]\n",GetName());
return kTRUE;
}
Bool_t TS800::LoadYtaShifts(const Char_t *fileName)
{
ifstream iFile(fileName,ios::in);
Char_t line[1000];
Int_t runNum;
Double_t ytaShift;
if(!iFile.is_open()) {printf("* File could not be opened!\n"); return kFALSE;}
iFile.getline(line,1000);
while(line[0]=='#' || line[0]==' ') iFile.getline(line,1000);
Bool_t firstLine = kTRUE;
while (!iFile.eof()){
if(!firstLine) iFile.getline(line,1000);
firstLine = kFALSE;
sscanf(line,"%i %lf",&runNum,&ytaShift);
fHYtaShifts.SetBinContent(runNum,ytaShift);
}
iFile.close();
printf("* Loading %s Yta stablization shifts . . . \t\t\t [OK]\n",GetName());
return kTRUE;
}
void TS800::SetCrdcCalFile(Char_t *crdc1Path,Char_t *crdc2Path)
{
crdc1CalPath = crdc1Path;
crdc2CalPath = crdc2Path;
}
void TS800::SetPIDShifts(Int_t runNum)
{
Double_t icShift = fHICShifts.GetBinContent(runNum);
Double_t xfpShift = fHToFShifts[0].GetBinContent(runNum);
Double_t tarShift = fHToFShifts[1].GetBinContent(runNum);
ic.fPIDShift = icShift;
tof.fPIDShift[0] = xfpShift;
tof.fPIDShift[1] = tarShift;
}
void TS800::SetFpTrackShifts(Int_t runNum)
{
Double_t ytaShift = fHYtaShifts.GetBinContent(runNum);
fpTrack.fYtaShift = ytaShift;
}
void TS800::SetName(const Char_t *name)
{
fName = name;
}
void TS800::SetTitle(const Char_t *title)
{
fTitle = title;
}
void TS800::SetNameTitle(const Char_t *name, const Char_t *title)
{
fName = name;
fTitle = title;
}
Int_t TS800::Unpack(UShort_t *pEvent)
{
UShort_t *pp = (UShort_t*)pEvent;
UShort_t *p = (UShort_t*)pEvent;
UShort_t packetSize;
UShort_t subEventLength,
subPacketSize,
pTag,
ID,
words;
*p--;
*p--;
packetSize = *p;
packetSize = packetSize - 3;
*p++;
*p++;
fNwords = packetSize;
if (*p++ != S800_VERSION) {
cout << "Wrong version of S800 sub-event. It is " << *p << ". Aborting ..." << endl;
return 1;
}
#ifdef TS800_DEBUG
Int_t lineCount = 3;
printf("---- S800 Packet ---- length = %i words\n",packetSize+3);
*p--; *p--; *p--;
printf("%04x ",*p++); printf("%04x ",*p++); printf("%04x ",*p++);
#endif
while (packetSize > 0) {
#ifdef TS800_DEBUG
printf("%04x ",*p);
if(lineCount==10){printf("\n"); lineCount=0;}
lineCount++; *p++; packetSize--;
if(packetSize==0) printf("\n\n");
#else
subPacketSize = *p++;
pTag = *p++;
switch(pTag) {
case S800_TRIGGER_PACKET:
p = this->trigger.Unpack(p);
break;
case S800_TOF_PACKET:
p = this->tof.Unpack(p);
break;
case S800_FP_SCINT_PACKET:
words = subPacketSize-2;
while (words > 0) {
ID = ((*p)&0xF000)>>12;
if (ID == 0 || ID == 1) p = this->e1.Unpack(p, ID);
if (ID == 2 || ID == 3) p = this->e2.Unpack(p, ID);
if (ID == 4 || ID == 5) p = this->e3.Unpack(p, ID);
words -= 2;
}
break;
case S800_FP_IC_PACKET:
p = this->ic.Unpack(p);
break;
case S800_FP_CRDC_PACKET:
ID = *p++;
if (ID == 0) p = this->crdc1.Unpack(p);
if (ID == 1) p = this->crdc2.Unpack(p);
break;
#if TS800_OP_TPPAC==1
case S800_II_TRACK_PACKET:
p = this->tppac1.Unpack(p,1);
p = this->tppac2.Unpack(p,2);
break;
#endif
#if TS800_OP_TCRDC==1
case S800_II_CRDC_PACKET:
ID = *p++;
if (ID == 0) p = this->tcrdc1.unpack(p);
if (ID == 1) p = this->tcrdc2.unpack(p);
break;
#endif
case S800_OB_PIN_PACKET:
break;
default:
printf("%i\n",pTag);
printf("Unrecognized packet tag: 0x%x @ packetSize = %d\n", pTag,packetSize);
printf("Beginning of event: 0x%x 0x%x 0x%x 0x%x ...\n", *pp, *(pp+1), *(pp+2), *(pp+3));
printf("Failing part: ");
for (Int_t yp=subEventLength-packetSize; yp<=subEventLength-packetSize+10; yp++) {
printf("0x%x ", *(pp+yp));
}
printf("\nBailing out ...\n");
p += subPacketSize - 2;
break;
}
packetSize -= subPacketSize;
if(pTag == 1) break;
#endif
}
return 0;
}
Last change: Sun Dec 21 12:38:53 2008
Last generated: 2008-12-21 12:38
This page has been automatically generated. If you have any comments or suggestions about the page layout send a mail to ROOT support, or contact the developers with any questions or problems regarding ROOT.