]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliDetector.cxx
use AliLog message scheme
[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 <TBrowser.h>
37 #include <TTree.h>
38
39 #include "AliLog.h"
40 #include "AliConfig.h"
41 #include "AliDetector.h"
42 #include "AliHit.h"
43 #include "AliPoints.h"
44 #include "AliLoader.h"
45 #include "AliRun.h"
46 #include "AliMC.h"
47
48
49 ClassImp(AliDetector)
50  
51 //_______________________________________________________________________
52 AliDetector::AliDetector():
53   fTimeGate(200.e-9),
54   fIshunt(0),
55   fNhits(0),
56   fNdigits(0),
57   fBufferSize(1600),
58   fHits(0),
59   fDigits(0),
60   fPoints(0),
61   fLoader(0x0)
62 {
63   //
64   // Default constructor for the AliDetector class
65   //
66 }
67  
68 //_______________________________________________________________________
69 AliDetector::AliDetector(const AliDetector &det):
70   AliModule(det),
71   fTimeGate(200.e-9),
72   fIshunt(0),
73   fNhits(0),
74   fNdigits(0),
75   fBufferSize(1600),
76   fHits(0),
77   fDigits(0),
78   fPoints(0),
79   fLoader(0x0)
80 {
81   det.Copy(*this);
82 }
83
84 //_____________________________________________________________________________
85 AliDetector::AliDetector(const char* name,const char *title):
86   AliModule(name,title),
87   fTimeGate(200.e-9),
88   fIshunt(0),
89   fNhits(0),
90   fNdigits(0),
91   fBufferSize(1600),
92   fHits(0),
93   fDigits(0),
94   fPoints(0),
95   fLoader(0x0)
96 {
97   //
98   // Normal constructor invoked by all Detectors.
99   // Create the list for detector specific histograms
100   // Add this Detector to the global list of Detectors in Run.
101   //
102
103   fActive     = kTRUE;
104   AliConfig::Instance()->Add(this);
105
106 }
107  
108 //_______________________________________________________________________
109 AliDetector::~AliDetector()
110 {
111   //
112   // Destructor
113   //
114
115   // Delete space point structure
116   if (fPoints) {
117     fPoints->Delete();
118     delete fPoints;
119     fPoints     = 0;
120   }
121   // Delete digits structure
122   if (fDigits) {
123     fDigits->Delete();
124     delete fDigits;
125     fDigits     = 0;
126   }
127
128   if (fLoader)
129    {
130     fLoader->GetModulesFolder()->Remove(this);
131    }
132
133 }
134
135 //_______________________________________________________________________
136 void AliDetector::Publish(const char */*dir*/, void */*address*/, const char */*name*/) const
137 {
138 //
139 // Register pointer to detector objects. 
140 // 
141   MayNotUse("Publish");
142 }
143
144 //_______________________________________________________________________
145 TBranch* AliDetector::MakeBranchInTree(TTree *tree, const char* name, 
146                                        void* address, Int_t size,
147                                        const char *file)
148
149     return(MakeBranchInTree(tree,name,0,address,size,99,file));
150 }
151
152 //_______________________________________________________________________
153 TBranch* AliDetector::MakeBranchInTree(TTree *tree, const char* name, 
154                                        const char *classname, 
155                                        void* address,Int_t size, 
156                                        Int_t splitlevel, const char */*file*/)
157
158 //
159 // Makes branch in given tree and diverts them to a separate file
160 // 
161 //
162 //
163 // if (GetDebug()>1)
164  AliDebug(2,Form("Making Branch %s",name));
165  if (tree == 0x0) 
166   {
167    AliError(Form("Making Branch %s Tree is NULL",name));
168    return 0x0;
169   }
170  TBranch *branch = tree->GetBranch(name);
171  if (branch) 
172   {  
173     AliDebug(2,Form("Branch %s is already in tree.",name));
174     return branch;
175   }
176     
177  if (classname) 
178   {
179     branch = tree->Branch(name,classname,address,size,splitlevel);
180   } 
181  else 
182   {
183     branch = tree->Branch(name,address,size);
184   }
185  AliDebug(2,Form("Branch %s returning branch %#x",name,branch));
186  return branch;
187 }
188
189 //_______________________________________________________________________
190 void AliDetector::Browse(TBrowser *b)
191 {
192   //
193   // Insert Detector objects in the list of objects to be browsed
194   //
195   char name[64];
196   if( fHits == 0) return;
197   TObject *obj;
198   Int_t i, nobjects;
199   //
200   nobjects = fHits->GetEntries();
201   for (i=0;i<nobjects;i++) {
202     obj = fHits->At(i);
203     sprintf(name,"%s_%d",obj->GetName(),i);
204     b->Add(obj, &name[0]);
205   }
206 }
207
208 //_______________________________________________________________________
209 void AliDetector::Copy(TObject &) const
210 {
211   //
212   // Copy *this onto det -- not implemented
213   //
214   AliFatal("Not implemented");
215 }
216
217 //_______________________________________________________________________
218 void AliDetector::FinishRun()
219 {
220   //
221   // Procedure called at the end of a run.
222   //
223 }
224
225 //_______________________________________________________________________
226 AliHit* AliDetector::FirstHit(Int_t track)
227 {
228   //
229   // Initialise the hit iterator
230   // Return the address of the first hit for track
231   // If track>=0 the track is read from disk
232   // while if track<0 the first hit of the current
233   // track is returned
234   // 
235   if(track>=0) {
236     gAlice->ResetHits(); //stupid = if N detector this method is called N times
237     TreeH()->GetEvent(track); //skowron
238   }
239   //
240   fMaxIterHit=fHits->GetEntriesFast();
241   fCurIterHit=0;
242   if(fMaxIterHit) return dynamic_cast<AliHit*>(fHits->UncheckedAt(0));
243   else            return 0;
244 }
245
246 //_______________________________________________________________________
247 AliHit* AliDetector::NextHit()
248 {
249   //
250   // Return the next hit for the current track
251   //
252   if(fMaxIterHit) {
253     if(++fCurIterHit<fMaxIterHit) 
254       return dynamic_cast<AliHit*>(fHits->UncheckedAt(fCurIterHit));
255     else        
256       return 0;
257   } else {
258     AliWarning("Hit Iterator called without calling FistHit before");
259     return 0;
260   }
261 }
262
263 //_______________________________________________________________________
264 void AliDetector::LoadPoints(Int_t)
265 {
266   //
267   // Store x, y, z of all hits in memory
268   //
269   if (fHits == 0) 
270    {
271     AliError(Form("fHits == 0. Name is %s",GetName()));
272     return;
273    }
274   //
275   Int_t nhits = fHits->GetEntriesFast();
276   if (nhits == 0) 
277    {
278 //    Error("LoadPoints","nhits == 0. Name is %s",GetName());
279     return;
280    }
281   Int_t tracks = gAlice->GetMCApp()->GetNtrack();
282   if (fPoints == 0) fPoints = new TObjArray(tracks);
283   AliHit *ahit;
284   //
285   Int_t *ntrk=new Int_t[tracks];
286   Int_t *limi=new Int_t[tracks];
287   Float_t **coor=new Float_t*[tracks];
288   for(Int_t i=0;i<tracks;i++) {
289     ntrk[i]=0;
290     coor[i]=0;
291     limi[i]=0;
292   }
293   //
294   AliPoints *points = 0;
295   Float_t *fp=0;
296   Int_t trk;
297   Int_t chunk=nhits/4+1;
298   //
299   // Loop over all the hits and store their position
300   for (Int_t hit=0;hit<nhits;hit++) {
301     ahit = dynamic_cast<AliHit*>(fHits->UncheckedAt(hit));
302     trk=ahit->GetTrack();
303     if(trk>tracks) AliFatal(Form("Found track number %d, max track %d",trk, tracks));
304     if(ntrk[trk]==limi[trk])
305      {
306       //
307       // Initialise a new track
308       fp=new Float_t[3*(limi[trk]+chunk)];
309       if(coor[trk]) 
310        {
311           memcpy(fp,coor[trk],sizeof(Float_t)*3*limi[trk]);
312           delete [] coor[trk];
313        }
314       limi[trk]+=chunk;
315       coor[trk] = fp;
316      } 
317     else 
318      {
319       fp = coor[trk];
320      }
321     fp[3*ntrk[trk]  ] = ahit->X();
322     fp[3*ntrk[trk]+1] = ahit->Y();
323     fp[3*ntrk[trk]+2] = ahit->Z();
324     ntrk[trk]++;
325   }
326   //
327   for(trk=0; trk<tracks; ++trk) {
328     if(ntrk[trk]) {
329       points = new AliPoints();
330       points->SetMarkerColor(GetMarkerColor());
331       points->SetMarkerSize(GetMarkerSize());
332       points->SetDetector(this);
333       points->SetParticle(trk);
334       points->SetPolyMarker(ntrk[trk],coor[trk],GetMarkerStyle());
335       fPoints->AddAt(points,trk);
336       delete [] coor[trk];
337       coor[trk]=0;
338     }
339   }
340   delete [] coor;
341   delete [] ntrk;
342   delete [] limi;
343 }
344
345 //_______________________________________________________________________
346 void AliDetector::MakeBranch(Option_t *option)
347 {
348   //
349   // Create a new branch for this detector in its treeH
350   //
351
352   AliDebug(2,Form(" for %s",GetName()));
353   const char *cH = strstr(option,"H");
354
355   if (fHits && TreeH() && cH) 
356    {
357      MakeBranchInTree(TreeH(), GetName(), &fHits, fBufferSize, 0);
358    }    
359 }
360
361 //_______________________________________________________________________
362 void AliDetector::ResetDigits()
363 {
364   //
365   // Reset number of digits and the digits array
366   //
367   fNdigits   = 0;
368   if (fDigits) fDigits->Clear();
369 }
370
371 //_______________________________________________________________________
372 void AliDetector::ResetHits()
373 {
374   //
375   // Reset number of hits and the hits array
376   //
377   fNhits   = 0;
378   if (fHits) fHits->Clear();
379 }
380
381 //_______________________________________________________________________
382 void AliDetector::ResetPoints()
383 {
384   //
385   // Reset array of points
386   //
387   if (fPoints) {
388     fPoints->Delete();
389     delete fPoints;
390     fPoints = 0;
391   }
392 }
393
394 //_______________________________________________________________________
395 void AliDetector::SetTreeAddress()
396 {
397   //
398   // Set branch address for the Hits and Digits Trees
399   //
400   TBranch *branch;
401   //
402   // Branch address for hit tree
403   
404   TTree *tree = TreeH();
405   if (tree && fHits) {
406     branch = tree->GetBranch(GetName());
407     if (branch) 
408      {
409        AliDebug(2,Form("(%s) Setting for Hits",GetName()));
410        branch->SetAddress(&fHits);
411      }
412     else
413      { //can be invoked before branch creation
414        AliDebug(2,Form("(%s) Failed for Hits. Can not find branch in tree.",GetName()));
415      }
416   }
417   
418   //
419   // Branch address for digit tree
420   TTree *treeD = fLoader->TreeD();
421   if (treeD && fDigits) {
422     branch = treeD->GetBranch(GetName());
423     if (branch) branch->SetAddress(&fDigits);
424   }
425   
426   AliModule::SetTreeAddress();
427 }
428
429 //_______________________________________________________________________
430 void AliDetector::MakeTree(Option_t *option)
431  {
432  //makes a tree (container) for the data defined in option
433  //"H" - hits
434  //"D" - digits
435  //"S" - summable digits
436  //"R" - recontructed points and tracks
437  
438     AliLoader* loader = GetLoader();
439     if (loader == 0x0)
440      {
441        AliError(Form("Can not get loader for %s",GetName()));
442        return;
443      }
444     loader->MakeTree(option); //delegate this job to getter
445  }
446
447 //_______________________________________________________________________
448 AliLoader* AliDetector::MakeLoader(const char* topfoldername)
449
450 //builds standard getter (AliLoader type)
451 //if detector wants to use castomized getter, it must overload this method
452
453  AliDebug(1,Form("Creating standard getter for detector %s. Top folder is %s.",
454          GetName(),topfoldername));
455      
456  fLoader = new AliLoader(GetName(),topfoldername);
457  return fLoader;
458 }
459
460 //_______________________________________________________________________
461 TTree* AliDetector::TreeH() const
462 {
463 //Get the hits container from the folder
464   if (GetLoader() == 0x0) 
465     {
466     //sunstitude this with make getter when we can obtain the event folder name 
467      AliError("Can not get the getter");
468      return 0x0;
469     }
470  
471   TTree* tree = (TTree*)GetLoader()->TreeH();
472   return tree;
473 }
474