New code from Piergiorgio added
[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$
8918e700 18Revision 1.5 1999/09/29 09:24:29 fca
19Introduction of the Copyright and cvs Log
20
4c039060 21*/
22
fe4da5cc 23///////////////////////////////////////////////////////////////////////////////
24// //
25// Base class for ALICE modules. Both sensitive modules (detectors) and //
26// non-sensitive ones are described by this base class. This class //
27// supports the hit and digit trees produced by the simulation and also //
28// the objects produced by the reconstruction. //
29// //
30// This class is also responsible for building the geometry of the //
31// detectors. //
32// //
33//Begin_Html
34/*
1439f98e 35<img src="picts/AliDetectorClass.gif">
fe4da5cc 36*/
37//End_Html
38// //
39///////////////////////////////////////////////////////////////////////////////
40#include "AliDetector.h"
41#include "AliRun.h"
42#include "AliHit.h"
43#include "AliPoints.h"
fe4da5cc 44#include <TClass.h>
45#include <TNode.h>
46#include <TRandom.h>
47
48// Static variables for the hit iterator routines
49static Int_t sMaxIterHit=0;
50static Int_t sCurIterHit=0;
51
52ClassImp(AliDetector)
53
54//_____________________________________________________________________________
55AliDetector::AliDetector()
56{
57 //
58 // Default constructor for the AliDetector class
59 //
60 fNhits = 0;
61 fNdigits = 0;
fe4da5cc 62 fPoints = 0;
63 fHits = 0;
64 fDigits = 0;
65 fTimeGate = 200.e-9;
fe4da5cc 66 fBufferSize = 16000;
67}
68
69//_____________________________________________________________________________
8494b010 70AliDetector::AliDetector(const char* name,const char *title):AliModule(name,title)
fe4da5cc 71{
72 //
73 // Normal constructor invoked by all Detectors.
74 // Create the list for detector specific histograms
75 // Add this Detector to the global list of Detectors in Run.
76 //
77
78 fTimeGate = 200.e-9;
79 fActive = kTRUE;
80 fNhits = 0;
81 fHits = 0;
82 fDigits = 0;
83 fNdigits = 0;
84 fPoints = 0;
85 fBufferSize = 16000;
fe4da5cc 86}
87
88//_____________________________________________________________________________
89AliDetector::~AliDetector()
90{
91 //
92 // Destructor
93 //
94 fNhits = 0;
95 fNdigits = 0;
fe4da5cc 96 //
97 // Delete space point structure
98 if (fPoints) fPoints->Delete();
99 delete fPoints;
100 fPoints = 0;
fe4da5cc 101}
102
103//_____________________________________________________________________________
104void AliDetector::Browse(TBrowser *b)
105{
106 //
107 // Insert Detector objects in the list of objects to be browsed
108 //
109 char name[64];
110 if( fHits == 0) return;
111 TObject *obj;
112 Int_t i, nobjects;
113 //
114 nobjects = fHits->GetEntries();
115 for (i=0;i<nobjects;i++) {
116 obj = fHits->At(i);
117 sprintf(name,"%s_%d",obj->GetName(),i);
118 b->Add(obj, &name[0]);
119 }
120}
121
122//_____________________________________________________________________________
8918e700 123void AliDetector::Copy(AliDetector &det) const
124{
125 //
126 // Copy *this onto det -- not implemented
127 //
128 Fatal("Copy","Not implemented~\n");
129}
130
131//_____________________________________________________________________________
fe4da5cc 132void AliDetector::FinishRun()
133{
134 //
135 // Procedure called at the end of a run.
136 //
137}
138
139//_____________________________________________________________________________
140AliHit* AliDetector::FirstHit(Int_t track)
141{
142 //
143 // Initialise the hit iterator
144 // Return the address of the first hit for track
145 // If track>=0 the track is read from disk
146 // while if track<0 the first hit of the current
147 // track is returned
148 //
149 if(track>=0) {
150 gAlice->ResetHits();
151 gAlice->TreeH()->GetEvent(track);
152 }
153 //
154 sMaxIterHit=fHits->GetEntriesFast();
155 sCurIterHit=0;
156 if(sMaxIterHit) return (AliHit*) fHits->UncheckedAt(0);
157 else return 0;
158}
159
160//_____________________________________________________________________________
161AliHit* AliDetector::NextHit()
162{
163 //
164 // Return the next hit for the current track
165 //
166 if(sMaxIterHit) {
167 if(++sCurIterHit<sMaxIterHit)
168 return (AliHit*) fHits->UncheckedAt(sCurIterHit);
169 else
170 return 0;
171 } else {
172 printf("* AliDetector::NextHit * Hit Iterator called without calling FistHit before\n");
173 return 0;
174 }
175}
176
177//_____________________________________________________________________________
178void AliDetector::LoadPoints(Int_t)
179{
180 //
181 // Store x, y, z of all hits in memory
182 //
183 if (fHits == 0) return;
184 //
fe4da5cc 185 Int_t nhits = fHits->GetEntriesFast();
186 if (nhits == 0) return;
080a67a1 187 Int_t tracks = gAlice->GetNtrack();
188 if (fPoints == 0) fPoints = new TObjArray(tracks);
fe4da5cc 189 AliHit *ahit;
190 //
080a67a1 191 Int_t *ntrk=new Int_t[tracks];
192 Int_t *limi=new Int_t[tracks];
193 Float_t **coor=new Float_t*[tracks];
194 for(Int_t i=0;i<tracks;i++) {
195 ntrk[i]=0;
196 coor[i]=0;
197 limi[i]=0;
198 }
199 //
fe4da5cc 200 AliPoints *points = 0;
080a67a1 201 Float_t *fp=0;
202 Int_t trk;
203 Int_t chunk=nhits/4+1;
fe4da5cc 204 //
205 // Loop over all the hits and store their position
206 for (Int_t hit=0;hit<nhits;hit++) {
207 ahit = (AliHit*)fHits->UncheckedAt(hit);
080a67a1 208 trk=ahit->GetTrack();
209 if(ntrk[trk]==limi[trk]) {
fe4da5cc 210 //
211 // Initialise a new track
080a67a1 212 fp=new Float_t[3*(limi[trk]+chunk)];
213 if(coor[trk]) {
214 memcpy(fp,coor[trk],sizeof(Float_t)*3*limi[trk]);
215 delete [] coor[trk];
216 }
217 limi[trk]+=chunk;
218 coor[trk] = fp;
219 } else {
220 fp = coor[trk];
221 }
222 fp[3*ntrk[trk] ] = ahit->fX;
223 fp[3*ntrk[trk]+1] = ahit->fY;
224 fp[3*ntrk[trk]+2] = ahit->fZ;
225 ntrk[trk]++;
226 }
227 //
228 for(trk=0; trk<tracks; ++trk) {
229 if(ntrk[trk]) {
230 points = new AliPoints();
fe4da5cc 231 points->SetMarkerColor(GetMarkerColor());
fe4da5cc 232 points->SetMarkerSize(GetMarkerSize());
233 points->SetDetector(this);
234 points->SetParticle(trk);
080a67a1 235 points->SetPolyMarker(ntrk[trk],coor[trk],GetMarkerStyle());
236 fPoints->AddAt(points,trk);
237 delete [] coor[trk];
238 coor[trk]=0;
fe4da5cc 239 }
fe4da5cc 240 }
080a67a1 241 delete [] coor;
242 delete [] ntrk;
243 delete [] limi;
fe4da5cc 244}
245
246//_____________________________________________________________________________
247void AliDetector::MakeBranch(Option_t *option)
248{
249 //
250 // Create a new branch in the current Root Tree
251 // The branch of fHits is automatically split
252 //
253 char branchname[10];
254 sprintf(branchname,"%s",GetName());
255 //
256 // Get the pointer to the header
8918e700 257 char *cH = strstr(option,"H");
fe4da5cc 258 //
8918e700 259 if (fHits && gAlice->TreeH() && cH) {
fe4da5cc 260 gAlice->TreeH()->Branch(branchname,&fHits, fBufferSize);
261 printf("* AliDetector::MakeBranch * Making Branch %s for hits\n",branchname);
262 }
263}
264
265//_____________________________________________________________________________
fe4da5cc 266void AliDetector::ResetDigits()
267{
268 //
269 // Reset number of digits and the digits array
270 //
271 fNdigits = 0;
272 if (fDigits) fDigits->Clear();
273}
274
275//_____________________________________________________________________________
276void AliDetector::ResetHits()
277{
278 //
279 // Reset number of hits and the hits array
280 //
281 fNhits = 0;
282 if (fHits) fHits->Clear();
283}
284
285//_____________________________________________________________________________
286void AliDetector::ResetPoints()
287{
288 //
289 // Reset array of points
290 //
291 if (fPoints) {
292 fPoints->Delete();
293 delete fPoints;
294 fPoints = 0;
295 }
296}
297
fe4da5cc 298//_____________________________________________________________________________
299void AliDetector::SetTreeAddress()
300{
301 //
302 // Set branch address for the Hits and Digits Trees
303 //
304 TBranch *branch;
305 char branchname[20];
306 sprintf(branchname,"%s",GetName());
307 //
308 // Branch address for hit tree
309 TTree *treeH = gAlice->TreeH();
310 if (treeH && fHits) {
311 branch = treeH->GetBranch(branchname);
312 if (branch) branch->SetAddress(&fHits);
313 }
314 //
315 // Branch address for digit tree
316 TTree *treeD = gAlice->TreeD();
317 if (treeD && fDigits) {
318 branch = treeD->GetBranch(branchname);
319 if (branch) branch->SetAddress(&fDigits);
320 }
321}
322
323//_____________________________________________________________________________
324void AliDetector::Streamer(TBuffer &R__b)
325{
326 //
327 // Stream an object of class Detector.
328 //
329 if (R__b.IsReading()) {
330 Version_t R__v = R__b.ReadVersion(); if (R__v) { }
331 TNamed::Streamer(R__b);
332 TAttLine::Streamer(R__b);
333 TAttMarker::Streamer(R__b);
8494b010 334 AliModule::Streamer(R__b);
fe4da5cc 335 R__b >> fTimeGate;
fe4da5cc 336 R__b >> fIshunt;
337 //R__b >> fNhits;
fe4da5cc 338 //
339 // Stream the pointers but not the TClonesArrays
fe4da5cc 340 R__b >> fHits; // diff
341 R__b >> fDigits; // diff
342
343 } else {
344 R__b.WriteVersion(AliDetector::IsA());
345 TNamed::Streamer(R__b);
346 TAttLine::Streamer(R__b);
347 TAttMarker::Streamer(R__b);
8494b010 348 AliModule::Streamer(R__b);
fe4da5cc 349 R__b << fTimeGate;
fe4da5cc 350 R__b << fIshunt;
351 //R__b << fNhits;
fe4da5cc 352 //
353 // Stream the pointers but not the TClonesArrays
fe4da5cc 354 R__b << fHits; // diff
355 R__b << fDigits; // diff
356 }
357}
358