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