2 // This macro translate original Tracks Reference tree
3 // into new tree sorted according to track label.
5 // TClonesArray connected to a branch, is filled
6 // with Track References from one track only.
10 // 1) Open files and initialise trees
11 // 2) Create and initialise Look Up Table
12 // 3) Loop over entries in References tree
13 // - store entry number in a LUT
14 // - store first and last array index of references for one track
15 // 4) Check LUT's consistancy.
16 // 5) Fill up new tree with references ordered acording to label.
19 // Sylwester Radomski (S.Radomski@gsi.de)
23 void CopyReference () {
26 const char *outFileName = "trackRef.root";
27 TFile *outFile = new TFile(outFileName, "RECREATE");
29 // connect to gAlice object in input file
30 const char *inFileName = "galice.root";
31 TFile *inFile = new TFile(inFileName, "READ");
33 gAlice = (AliRun*)inFile->Get("gAlice");
36 // declare detectors to translate
38 const char *detectors[nDet] = {"ITS","TPC", "TRD", "TOF"};
42 Int_t nPart = gAlice->GetNtrack();
43 Int_t *lutTrack = new Int_t[nPart * nDet];
44 Int_t *lutStart = new Int_t[nPart * nDet];
45 Int_t *lutStop = new Int_t[nPart * nDet];
49 for(Int_t i=0; i<nPart; i++)
50 for (Int_t j=0; j<nDet; j++)
51 lutTrack[i*nDet+j] = lutStart[i*nDet+j] = lutStop[i*nDet+j] = -1;
54 // Declare tree, branches and CloneArrays
56 TTree *refTree = (TTree*)inFile->Get("TreeTR0");
57 TBranch *refBranch[nDet];
58 TClonesArray *refArray[nDet];
60 // Connect branches with arrays
62 for (Int_t j=0; j<nDet; j++) {
63 refBranch[j] = refTree->GetBranch(detectors[j]);
64 refArray[j] = new TClonesArray("AliTrackReference", 100);
65 refBranch[j]->SetAddress(&(refArray+j));
68 Int_t nTracks = refBranch[0]->GetEntries();
70 cout << "N Particles\t" << nPart << endl
71 << "N Tracks\t" << nTracks << endl;
73 cout << "Filling Look Up Tables ..." << endl;
75 for (Int_t i=0; i<nTracks; i++) {
79 for (Int_t j=0; j<nDet; j++) {
82 AliTrackReference *ref = 0;
83 refBranch[j]->GetEvent(i);
84 Int_t nObj = refArray[j]->GetEntries();
88 lab = ((AliTrackReference*)refArray[j]->At(0))->GetTrack();
91 for (Int_t e=0; e<nObj-1; e++) {
93 ref = (AliTrackReference*)refArray[j]->At(e+1);
95 if (ref->GetTrack() != lab) {
96 lutTrack[lab*nDet + j] = i;
97 lutStart[lab*nDet + j] = start;
98 lutStop[lab*nDet + j] = e+1;
101 lab = ref->GetTrack();
105 lutTrack[lab*nDet + j] = i;
106 lutStart[lab*nDet + j] = start;
107 lutStop[lab*nDet + j] = nObj;
111 cout << endl << "done" << endl;
112 cout << "Checking consistancy of LUTs ..." << endl;
116 for(Int_t i=0; i<nPart; i++) {
119 Int_t ctrlTrack = -1;
121 for(Int_t j=0; j<nDet; j++) {
123 if (ctrlTrack == -1 && lutTrack[i*nDet+j] != -1)
124 ctrlTrack = lutTrack[i*nDet+j];
126 if (lutTrack[i*nDet+j] != -1 && lutTrack[i*nDet+j] != ctrlTrack)
127 cout << "Error: " << i << " " << j << endl;
131 cout << endl << "done" << endl;
132 cout << "Writing to a new Tree ..." << endl;
137 TTree *outTree = new TTree("TreeTR0_Sorted", "Sorted Tracks References");
138 TBranch *outBranch[nDet];
139 TClonesArray *outArray[nDet];
143 for(Int_t j=0; j<nDet; j++) {
144 outArray[j] = new TClonesArray("AliTrackReference", 200);
145 outBranch[j] = outTree->Branch(detectors[j], &(outArray+j), 64000, 3);
148 // Loop over particles
149 // Fill Tree if entries exists
151 for(Int_t i=0; i<nPart; i++) {
155 for(Int_t j=0; j<nDet; j++) {
157 outArray[j]->Clear();
159 if (lutTrack[i*nDet+j] != -1) {
161 refBranch[j]->GetEvent(lutTrack[i*nDet+j]);
163 // rewrite objects in clonearrays
164 for(Int_t k = lutStart[i*nDet+j], en = 0; k<lutStop[i*nDet+j]; k++ ) {
165 AliTrackReference *ref = (AliTrackReference*)refArray[j]->At(k);
166 new( (*(outArray[j]))[en++] ) AliTrackReference(*ref);
174 cout << endl << "done" << endl;
182 //if (refTree) delete refTree;
183 //if (outTree) delete outTree;
184 //if (inFile) delete inFile;
185 //if (outFile) delete outFile;