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