b6b54d54c20d61e675492612d4db71a264de9601
[u/mrichter/AliRoot.git] / STEER / AliDetector.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /* $Id$ */
17
18 ///////////////////////////////////////////////////////////////////////////////
19 //                                                                           //
20 // Base class for ALICE modules. Both sensitive modules (detectors) and      //
21 // non-sensitive ones are described by this base class. This class           //
22 // supports the hit and digit trees produced by the simulation and also      //
23 // the objects produced by the reconstruction.                               //
24 //                                                                           //
25 // This class is also responsible for building the geometry of the           //
26 // detectors.                                                                //
27 //                                                                           //
28 //Begin_Html
29 /*
30 <img src="picts/AliDetectorClass.gif">
31 */
32 //End_Html
33 //                                                                           //
34 ///////////////////////////////////////////////////////////////////////////////
35
36 #include <assert.h>
37
38 #include <Riostream.h>
39 #include <TBrowser.h>
40 #include <TFile.h>
41 #include <TFolder.h>
42 #include <TROOT.h>
43 #include <TTree.h>
44
45 #include "AliConfig.h"
46 #include "AliDetector.h"
47 #include "AliHit.h"
48 #include "AliPoints.h"
49 #include "AliRun.h"
50 #include "AliTrackReference.h"
51
52
53 // Static variables for the hit iterator routines
54 static Int_t sMaxIterHit=0;
55 static Int_t sCurIterHit=0;
56
57
58 ClassImp(AliDetector)
59  
60 //_______________________________________________________________________
61 AliDetector::AliDetector():
62   fTimeGate(200.e-9),
63   fIshunt(0),
64   fNhits(0),
65   fNdigits(0),
66   fBufferSize(1600),
67   fHits(0),
68   fDigits(0),
69   fDigitsFile(0),
70   fPoints(0),
71   fTrackReferences(0),
72   fMaxIterTrackRef(0),
73   fCurrentIterTrackRef(0)
74 {
75   //
76   // Default constructor for the AliDetector class
77   //
78 }
79  
80 //_______________________________________________________________________
81 AliDetector::AliDetector(const AliDetector &det):
82   AliModule(det),
83   fTimeGate(200.e-9),
84   fIshunt(0),
85   fNhits(0),
86   fNdigits(0),
87   fBufferSize(1600),
88   fHits(0),
89   fDigits(0),
90   fDigitsFile(0),
91   fPoints(0),
92   fTrackReferences(0),
93   fMaxIterTrackRef(0),
94   fCurrentIterTrackRef(0)
95 {
96   det.Copy(*this);
97 }
98
99 //_____________________________________________________________________________
100 AliDetector::AliDetector(const char* name,const char *title):
101   AliModule(name,title),
102   fTimeGate(200.e-9),
103   fIshunt(0),
104   fNhits(0),
105   fNdigits(0),
106   fBufferSize(1600),
107   fHits(0),
108   fDigits(0),
109   fDigitsFile(0),
110   fPoints(0),
111   fTrackReferences(new TClonesArray("AliTrackReference", 100)),
112   fMaxIterTrackRef(0),
113   fCurrentIterTrackRef(0)
114 {
115   //
116   // Normal constructor invoked by all Detectors.
117   // Create the list for detector specific histograms
118   // Add this Detector to the global list of Detectors in Run.
119   //
120
121   fActive     = kTRUE;
122   AliConfig::Instance()->Add(this);
123
124 }
125  
126 //_______________________________________________________________________
127 AliDetector::~AliDetector()
128 {
129   //
130   // Destructor
131   //
132
133   // Delete space point structure
134   if (fPoints) {
135     fPoints->Delete();
136     delete fPoints;
137     fPoints     = 0;
138   }
139   // Delete digits structure
140   if (fDigits) {
141     fDigits->Delete();
142     delete fDigits;
143     fDigits     = 0;
144   }
145   // Delete track references
146   if (fTrackReferences) {
147     fTrackReferences->Delete();
148     delete fTrackReferences;
149     fTrackReferences     = 0;
150   }
151   if (fDigitsFile) delete [] fDigitsFile;
152 }
153
154 //_______________________________________________________________________
155 void AliDetector::Publish(const char *dir, void *address, const char *name)
156 {
157   //
158   // Register pointer to detector objects. 
159   // 
160   TFolder *topFolder = dynamic_cast<TFolder *>(gROOT->FindObjectAny("/Folders"));
161   if  (topFolder) { 
162     TFolder *folder = dynamic_cast<TFolder *>(topFolder->FindObjectAny(dir));
163     // TFolder *folder = dynamic_cast<TFolder *>(gROOT->FindObjectAny(dir));
164     if (!folder)  {
165       cerr << "Cannot register: Missing folder: " << dir << endl;
166     } else {
167       TFolder *subfolder = dynamic_cast<TFolder *>(folder->FindObjectAny(this->GetName())); 
168
169       if(!subfolder)
170          subfolder = folder->AddFolder(this->GetName(),this->GetTitle());
171       if (address) {
172         TObject **obj = static_cast<TObject **>(address);
173         if ((*obj)->InheritsFrom(TCollection::Class())) {
174            TCollection *collection = dynamic_cast<TCollection *>(*obj); 
175            if (name)
176              collection->SetName(name);
177         } 
178         subfolder->Add(*obj);
179       } 
180     }  
181   }
182 }
183
184 //_______________________________________________________________________
185 TBranch* AliDetector::MakeBranchInTree(TTree *tree, const char* name, 
186                                        void* address, Int_t size,
187                                        const char *file)
188
189     return(MakeBranchInTree(tree,name,0,address,size,99,file));
190 }
191
192 //_______________________________________________________________________
193 TBranch* AliDetector::MakeBranchInTree(TTree *tree, const char* name, 
194                                        const char *classname, 
195                                        void* address,Int_t size, 
196                                        Int_t splitlevel, const char *file)
197
198     //
199     // Makes branch in given tree and diverts them to a separate file
200     //  
201     if (GetDebug()>1)
202       printf("* MakeBranch * Making Branch %s \n",name);
203       
204     TDirectory *cwd = gDirectory;
205     TBranch *branch = 0;
206     
207     if (classname) {
208       branch = tree->Branch(name,classname,address,size,splitlevel);
209     } else {
210       branch = tree->Branch(name,address,size);
211     }
212        
213     if (file) {
214         char * outFile = new char[strlen(gAlice->GetBaseFile())+strlen(file)+2];
215         sprintf(outFile,"%s/%s",gAlice->GetBaseFile(),file);
216         branch->SetFile(outFile);
217         TIter next( branch->GetListOfBranches());
218         while ((branch=dynamic_cast<TBranch*>(next()))) {
219            branch->SetFile(outFile);
220         } 
221        delete outFile;
222         
223        cwd->cd();
224         
225        if (GetDebug()>1)
226            printf("* MakeBranch * Diverting Branch %s to file %s\n",name,file);
227     }
228     const char *folder = 0;
229     TString folderName(name);  
230     
231     if (!strncmp(tree->GetName(),"TreeE",5)) folder = "RunMC/Event/Data";
232     if (!strncmp(tree->GetName(),"TreeK",5)) folder = "RunMC/Event/Data";
233     if (!strncmp(tree->GetName(),"TreeH",5)) {
234       folder     = "RunMC/Event/Data/Hits";
235       folderName = "Hits" ; 
236     }
237     if (!strncmp(tree->GetName(),"TreeTrackReferences",5)) {
238       folder     = "RunMC/Event/Data/TrackReferences";
239       folderName = "TrackReferences" ; 
240     }
241
242     if (!strncmp(tree->GetName(),"TreeD",5)) {
243       folder     = "Run/Event/Data";
244       folderName = "Digits" ; 
245     }
246     if (!strncmp(tree->GetName(),"TreeS",5)) {
247       folder     = "RunMC/Event/Data/SDigits";
248       folderName = "SDigits" ; 
249     }
250     if (!strncmp(tree->GetName(),"TreeR",5)) folder = "Run/Event/RecData";
251
252     if (folder) {
253       if (GetDebug())
254           printf("%15s: Publishing %s to %s\n",ClassName(),name,folder);
255       Publish(folder,address, folderName.Data());
256     }  
257     return branch;
258 }
259
260 //_______________________________________________________________________
261 void AliDetector::Browse(TBrowser *b)
262 {
263   //
264   // Insert Detector objects in the list of objects to be browsed
265   //
266   char name[64];
267   if( fHits == 0) return;
268   TObject *obj;
269   Int_t i, nobjects;
270   //
271   nobjects = fHits->GetEntries();
272   for (i=0;i<nobjects;i++) {
273     obj = fHits->At(i);
274     sprintf(name,"%s_%d",obj->GetName(),i);
275     b->Add(obj, &name[0]);
276   }
277 }
278
279 //_______________________________________________________________________
280 void AliDetector::Copy(AliDetector &) const
281 {
282   //
283   // Copy *this onto det -- not implemented
284   //
285   Fatal("Copy","Not implemented\n");
286 }
287
288 //_______________________________________________________________________
289 void AliDetector::FinishRun()
290 {
291   //
292   // Procedure called at the end of a run.
293   //
294 }
295
296 //_______________________________________________________________________
297 void AliDetector::RemapTrackReferencesIDs(Int_t *map)
298 {
299   // 
300   // Remapping track reference
301   // Called at finish primary
302   //
303   if (!fTrackReferences) return;
304   for (Int_t i=0;i<fTrackReferences->GetEntries();i++){
305     AliTrackReference * ref = dynamic_cast<AliTrackReference*>(fTrackReferences->UncheckedAt(i));
306     if (ref) {
307       Int_t newID = map[ref->GetTrack()];
308       if (newID>=0) ref->SetTrack(newID);
309       else ref->SetTrack(-1);
310       
311     }
312   }
313 }
314
315 //_______________________________________________________________________
316 AliHit* AliDetector::FirstHit(Int_t track)
317 {
318   //
319   // Initialise the hit iterator
320   // Return the address of the first hit for track
321   // If track>=0 the track is read from disk
322   // while if track<0 the first hit of the current
323   // track is returned
324   // 
325   if(track>=0) {
326     gAlice->ResetHits();
327     gAlice->TreeH()->GetEvent(track);
328   }
329   //
330   sMaxIterHit=fHits->GetEntriesFast();
331   sCurIterHit=0;
332   if(sMaxIterHit) return dynamic_cast<AliHit*>(fHits->UncheckedAt(0));
333   else            return 0;
334 }
335
336
337 //_______________________________________________________________________
338 AliTrackReference* AliDetector::FirstTrackReference(Int_t track)
339 {
340   //
341   // Initialise the hit iterator
342   // Return the address of the first hit for track
343   // If track>=0 the track is read from disk
344   // while if track<0 the first hit of the current
345   // track is returned
346   // 
347   if(track>=0) {
348     gAlice->ResetTrackReferences();
349     gAlice->TreeTR()->GetEvent(track);
350   }
351   //
352   fMaxIterTrackRef     = fTrackReferences->GetEntriesFast();
353   fCurrentIterTrackRef = 0;
354   if(fMaxIterTrackRef) return dynamic_cast<AliTrackReference*>(fTrackReferences->UncheckedAt(0));
355   else            return 0;
356 }
357
358 //_______________________________________________________________________
359 AliHit* AliDetector::NextHit()
360 {
361   //
362   // Return the next hit for the current track
363   //
364   if(sMaxIterHit) {
365     if(++sCurIterHit<sMaxIterHit) 
366       return dynamic_cast<AliHit*>(fHits->UncheckedAt(sCurIterHit));
367     else        
368       return 0;
369   } else {
370     printf("* AliDetector::NextHit * Hit Iterator called without calling FistHit before\n");
371     return 0;
372   }
373 }
374
375 //_______________________________________________________________________
376 AliTrackReference* AliDetector::NextTrackReference()
377 {
378   //
379   // Return the next hit for the current track
380   //
381   if(fMaxIterTrackRef) {
382     if(++fCurrentIterTrackRef<fMaxIterTrackRef) 
383       return dynamic_cast<AliTrackReference*>(fTrackReferences->UncheckedAt(fCurrentIterTrackRef));
384     else        
385       return 0;
386   } else {
387     printf("* AliDetector::NextTrackReference * TrackReference  Iterator called without calling FistTrackReference before\n");
388     return 0;
389   }
390 }
391
392 //_______________________________________________________________________
393 void AliDetector::LoadPoints(Int_t)
394 {
395   //
396   // Store x, y, z of all hits in memory
397   //
398   if (fHits == 0) return;
399   //
400   Int_t nhits = fHits->GetEntriesFast();
401   if (nhits == 0) return;
402   Int_t tracks = gAlice->GetNtrack();
403   if (fPoints == 0) fPoints = new TObjArray(tracks);
404   AliHit *ahit;
405   //
406   Int_t *ntrk=new Int_t[tracks];
407   Int_t *limi=new Int_t[tracks];
408   Float_t **coor=new Float_t*[tracks];
409   for(Int_t i=0;i<tracks;i++) {
410     ntrk[i]=0;
411     coor[i]=0;
412     limi[i]=0;
413   }
414   //
415   AliPoints *points = 0;
416   Float_t *fp=0;
417   Int_t trk;
418   Int_t chunk=nhits/4+1;
419   //
420   // Loop over all the hits and store their position
421   for (Int_t hit=0;hit<nhits;hit++) {
422     ahit = dynamic_cast<AliHit*>(fHits->UncheckedAt(hit));
423     trk=ahit->GetTrack();
424     assert(trk<=tracks);
425     if(ntrk[trk]==limi[trk]) {
426       //
427       // Initialise a new track
428       fp=new Float_t[3*(limi[trk]+chunk)];
429       if(coor[trk]) {
430         memcpy(fp,coor[trk],sizeof(Float_t)*3*limi[trk]);
431         delete [] coor[trk];
432       }
433       limi[trk]+=chunk;
434       coor[trk] = fp;
435     } else {
436       fp = coor[trk];
437     }
438     fp[3*ntrk[trk]  ] = ahit->X();
439     fp[3*ntrk[trk]+1] = ahit->Y();
440     fp[3*ntrk[trk]+2] = ahit->Z();
441     ntrk[trk]++;
442   }
443   //
444   for(trk=0; trk<tracks; ++trk) {
445     if(ntrk[trk]) {
446       points = new AliPoints();
447       points->SetMarkerColor(GetMarkerColor());
448       points->SetMarkerSize(GetMarkerSize());
449       points->SetDetector(this);
450       points->SetParticle(trk);
451       points->SetPolyMarker(ntrk[trk],coor[trk],GetMarkerStyle());
452       fPoints->AddAt(points,trk);
453       delete [] coor[trk];
454       coor[trk]=0;
455     }
456   }
457   delete [] coor;
458   delete [] ntrk;
459   delete [] limi;
460 }
461
462 //_______________________________________________________________________
463 void AliDetector::MakeBranch(Option_t *option, const char *file)
464 {
465   //
466   // Create a new branch in the current Root Tree
467   // The branch of fHits is automatically split
468   //
469  
470   char branchname[10];
471   sprintf(branchname,"%s",GetName());
472   //
473   // Get the pointer to the header
474   const char *cH = strstr(option,"H");
475   //
476   if (fHits && gAlice->TreeH() && cH) {
477     MakeBranchInTree(gAlice->TreeH(), 
478                      branchname, &fHits, fBufferSize, file) ;              
479   }     
480   
481   const char *cD = strstr(option,"D");
482
483   if (cD) {
484     if (file) {
485        fDigitsFile = new char[strlen (file)];
486        strcpy(fDigitsFile,file);
487     }
488   }
489 }
490 //_______________________________________________________________________
491 void AliDetector::MakeBranchTR(Option_t *option, const char *file)
492 {
493   //
494   // Create a new branch in the current Root Tree
495   // The branch of fHits is automatically split
496   //
497  
498   char branchname[10];
499   sprintf(branchname,"%s",GetName());
500   //
501   // Get the pointer to the header
502   const char *cTR = strstr(option,"T");
503   //
504   if (fTrackReferences && gAlice->TreeTR() && cTR) {
505     MakeBranchInTree(gAlice->TreeTR(), 
506                      branchname, &fTrackReferences, fBufferSize, file) ;              
507   }       
508 }
509
510 //_______________________________________________________________________
511 void AliDetector::ResetDigits()
512 {
513   //
514   // Reset number of digits and the digits array
515   //
516   fNdigits   = 0;
517   if (fDigits)   fDigits->Clear();
518 }
519
520 //_______________________________________________________________________
521 void AliDetector::ResetHits()
522 {
523   //
524   // Reset number of hits and the hits array
525   //
526   fNhits   = 0;
527   if (fHits)   fHits->Clear();
528 }
529
530 //_______________________________________________________________________
531 void AliDetector::ResetTrackReferences()
532 {
533   //
534   // Reset number of hits and the hits array
535   //
536   fMaxIterTrackRef   = 0;
537   if (fTrackReferences)   fTrackReferences->Clear();
538 }
539
540 //_______________________________________________________________________
541 void AliDetector::ResetPoints()
542 {
543   //
544   // Reset array of points
545   //
546   if (fPoints) {
547     fPoints->Delete();
548     delete fPoints;
549     fPoints = 0;
550   }
551 }
552
553 //_______________________________________________________________________
554 void AliDetector::SetTreeAddress()
555 {
556   //
557   // Set branch address for the Hits and Digits Trees
558   //
559   TBranch *branch;
560   char branchname[20];
561   sprintf(branchname,"%s",GetName());
562   //
563   // Branch address for hit tree
564   TTree *treeH = gAlice->TreeH();
565   if (treeH && fHits) {
566     branch = treeH->GetBranch(branchname);
567     if (branch) branch->SetAddress(&fHits);
568   }
569   //
570   // Branch address for digit tree
571   TTree *treeD = gAlice->TreeD();
572   if (treeD && fDigits) {
573     branch = treeD->GetBranch(branchname);
574     if (branch) branch->SetAddress(&fDigits);
575   }
576
577   // Branch address for tr  tree
578   TTree *treeTR = gAlice->TreeTR();
579   if (treeTR && fTrackReferences) {
580     branch = treeTR->GetBranch(branchname);
581     if (branch) branch->SetAddress(&fTrackReferences);
582   }
583 }
584
585