]>
Commit | Line | Data |
---|---|---|
a5556ea5 | 1 | #include "AliReader.h" |
2 | //_________________________________________________________________________ | |
3 | /////////////////////////////////////////////////////////////////////////// | |
4 | // | |
5 | // class AliReader | |
6 | // | |
7 | // Reader Base class (reads particles and tracks and | |
8 | // puts it to the AliAODRun objects | |
9 | // | |
10 | // Provides functionality for both buffering and non-buffering reading | |
11 | // This can be switched on/off via method SetEventBuffering(bool) | |
12 | // The main method that inheriting classes need to implement is ReadNext() | |
13 | // that read next event in queue. | |
14 | // The others are: | |
15 | // Bool_t ReadsRec() const; specifies if reader is able to read simulated particles | |
16 | // Bool_t ReadsSim() const; specifies if reader is able to read reconstructed tracks | |
17 | // void Rewind(); rewind reading to the beginning | |
18 | // | |
19 | // reading of next event is triggered via method Next() | |
20 | // | |
21 | // This class provides full functionality for reading from many sources | |
22 | // User can provide TObjArray of TObjString (SetDirs method or via parameter | |
23 | // in constructor) which desribes paths of directories to search data in. | |
24 | // If none specified current directory is searched. | |
25 | // | |
26 | // Piotr.Skowronski@cern.ch | |
27 | /////////////////////////////////////////////////////////////////////////// | |
28 | ||
29 | #include <TString.h> | |
30 | #include <TObjString.h> | |
31 | #include <TObjArray.h> | |
32 | #include <TClass.h> | |
33 | #include <TRandom.h> | |
34 | #include <TH1.h> | |
35 | ||
36 | #include "AliAODParticleCut.h" | |
37 | #include "AliAOD.h" | |
38 | #include "AliAODRun.h" | |
39 | ||
40 | ClassImp(AliReader) | |
41 | //pure virtual | |
42 | ||
43 | /*************************************************************************************/ | |
44 | ||
45 | AliReader::AliReader(): | |
46 | fCuts(new TObjArray()), | |
47 | fDirs(0x0), | |
48 | fCurrentEvent(0), | |
49 | fCurrentDir(0), | |
50 | fNEventsRead(0), | |
51 | fEventRec(0x0), | |
52 | fEventSim(0x0), | |
53 | fRunSim(0x0), | |
54 | fRunRec(0x0), | |
55 | fIsRead(kFALSE), | |
56 | fBufferEvents(kFALSE), | |
57 | fBlend(kFALSE), | |
58 | fFirst(0), | |
59 | fLast(0), | |
60 | fTrackCounter(0x0) | |
61 | { | |
62 | //constructor | |
63 | } | |
64 | /*************************************************************************************/ | |
65 | ||
66 | AliReader::AliReader(TObjArray* dirs): | |
67 | fCuts(new TObjArray()), | |
68 | fDirs(dirs), | |
69 | fCurrentEvent(0), | |
70 | fCurrentDir(0), | |
71 | fNEventsRead(0), | |
72 | fEventRec(0x0), | |
73 | fEventSim(0x0), | |
74 | fRunSim(0x0), | |
75 | fRunRec(0x0), | |
76 | fIsRead(kFALSE), | |
77 | fBufferEvents(kFALSE), | |
78 | fBlend(kFALSE), | |
79 | fFirst(0), | |
80 | fLast(0), | |
81 | fTrackCounter(0x0) | |
82 | { | |
83 | //ctor with array of directories to read as parameter | |
84 | } | |
85 | /*************************************************************************************/ | |
86 | AliReader::AliReader(const AliReader& in): | |
87 | TNamed(in), | |
88 | fCuts((in.fCuts)?(TObjArray*)in.fCuts->Clone():0x0), | |
89 | fDirs((in.fDirs)?(TObjArray*)in.fDirs->Clone():0x0), | |
90 | fCurrentEvent(0), | |
91 | fCurrentDir(0), | |
92 | fNEventsRead(0), | |
93 | fEventRec(0x0), | |
94 | fEventSim(0x0), | |
95 | fRunSim(0x0), | |
96 | fRunRec(0x0), | |
97 | fIsRead(kFALSE), | |
98 | fBufferEvents(in.fBufferEvents), | |
99 | fBlend(in.fBlend), | |
100 | fFirst(in.fFirst), | |
101 | fLast(in.fLast), | |
102 | fTrackCounter(0x0) | |
103 | { | |
104 | //cpy constructor | |
105 | } | |
106 | ||
107 | AliReader::~AliReader() | |
108 | { | |
109 | //destructor | |
110 | if(fCuts) | |
111 | { | |
112 | fCuts->SetOwner(); | |
113 | delete fCuts; | |
114 | } | |
115 | delete fEventSim; | |
116 | delete fEventRec; | |
117 | delete fTrackCounter; | |
118 | } | |
119 | /*************************************************************************************/ | |
120 | ||
121 | AliReader& AliReader::operator=(const AliReader& in) | |
122 | { | |
123 | //Assigment operator | |
124 | if (this == &in) return *this; | |
125 | TNamed::operator=( (const TNamed&)in ); | |
126 | ||
127 | fCuts = (in.fCuts)?(TObjArray*)in.fCuts->Clone():0x0; | |
128 | fDirs = (in.fDirs)?(TObjArray*)in.fDirs->Clone():0x0; | |
129 | fCurrentEvent = 0; | |
130 | fCurrentDir = 0; | |
131 | fNEventsRead = 0; | |
132 | fEventRec = 0x0; | |
133 | fEventSim = 0x0; | |
134 | fRunSim = 0x0; | |
135 | fRunRec = 0x0; | |
136 | fIsRead = kFALSE; | |
137 | fBufferEvents = in.fBufferEvents; | |
138 | fBlend = in.fBlend; | |
139 | fFirst = in.fFirst; | |
140 | fLast = in.fLast; | |
141 | fTrackCounter = 0x0; | |
142 | return *this; | |
143 | } | |
144 | /*************************************************************************************/ | |
145 | ||
146 | Int_t AliReader::Next() | |
147 | { | |
148 | //moves to next event | |
149 | ||
150 | //if asked to read up to event nb. fLast, and it is overcome, report no more events | |
151 | if ((fNEventsRead > fLast) && (fLast > 0) ) return kTRUE; | |
152 | ||
153 | if (fTrackCounter == 0x0)//create Track Counter | |
154 | { | |
155 | fTrackCounter = new TH1I("trackcounter","Track Counter",20000,0,20000); | |
156 | fTrackCounter->SetDirectory(0x0); | |
157 | } | |
158 | ||
159 | do //if asked to read from event fFirst, rewind to it | |
160 | { | |
161 | if ( ReadNext() == kTRUE) //if no more evets, return it | |
162 | return kTRUE; | |
163 | }while (fNEventsRead < fFirst); | |
164 | ||
165 | //here we have event | |
166 | ||
167 | if (fBlend) Blend();//Mix particles order | |
168 | ||
169 | if (fBufferEvents)//store events if buffering is on | |
170 | { | |
171 | if ( ReadsRec() && fEventRec) | |
172 | fRunRec->SetEvent(fNEventsRead-1-fFirst,fEventRec); | |
173 | if ( ReadsSim() && fEventSim) | |
174 | fRunSim->SetEvent(fNEventsRead-1-fFirst,fEventSim); | |
175 | } | |
176 | return kFALSE; | |
177 | } | |
178 | /*************************************************************************************/ | |
179 | ||
180 | void AliReader::AddParticleCut(AliAODParticleCut* cut) | |
181 | { | |
182 | //sets the new cut. MAKES A COPY OF THE CUT !!!! | |
183 | ||
184 | if (!cut) //if cut is NULL return with error | |
185 | { | |
186 | Error("AddParticleType","NULL pointers are not accepted any more.\nIf You want to accept all particles of this type, set an empty cut "); | |
187 | return; | |
188 | } | |
189 | AliAODParticleCut *c = (AliAODParticleCut*)cut->Clone(); | |
190 | fCuts->Add(c); | |
191 | } | |
192 | /********************************************************************/ | |
193 | ||
194 | AliAOD* AliReader::GetEventSim(Int_t n) | |
195 | { | |
196 | //returns Nth event with simulated particles | |
197 | if (ReadsSim() == kFALSE) | |
198 | { | |
199 | Error("GetParticleEvent","This reader is not able to provide simulated particles."); | |
200 | return 0; | |
201 | } | |
202 | ||
203 | if (!fIsRead) | |
204 | { | |
205 | if (ReadsSim() && (fRunSim == 0x0)) fRunSim = new AliAODRun(); | |
206 | if (ReadsRec() && (fRunRec == 0x0)) fRunRec = new AliAODRun(); | |
207 | ||
208 | if (Read(fRunSim,fRunRec)) | |
209 | { | |
210 | Error("GetParticleEvent","Error in reading"); | |
211 | return 0x0; | |
212 | } | |
213 | else fIsRead = kTRUE; | |
214 | } | |
215 | return fRunSim->GetEvent(n); | |
216 | } | |
217 | /********************************************************************/ | |
218 | ||
219 | AliAOD* AliReader::GetEventRec(Int_t n) | |
220 | { | |
221 | //returns Nth event with reconstructed tracks | |
222 | if (ReadsRec() == kFALSE) | |
223 | { | |
224 | Error("GetTrackEvent","This reader is not able to provide recosntructed tracks."); | |
225 | return 0; | |
226 | } | |
227 | if (!fIsRead) | |
228 | { | |
229 | if (ReadsSim() && (fRunSim == 0x0)) fRunSim = new AliAODRun(); | |
230 | if (ReadsRec() && (fRunRec == 0x0)) fRunRec = new AliAODRun(); | |
231 | ||
232 | if(Read(fRunSim,fRunRec)) | |
233 | { | |
234 | Error("GetTrackEvent","Error in reading"); | |
235 | return 0x0; | |
236 | } | |
237 | else fIsRead = kTRUE; | |
238 | } | |
239 | return fRunRec->GetEvent(n); | |
240 | } | |
241 | /********************************************************************/ | |
242 | ||
243 | Int_t AliReader::GetNumberOfSimEvents() | |
244 | { | |
245 | //returns number of events of particles | |
246 | if (ReadsSim() == kFALSE) | |
247 | { | |
248 | Error("GetNumberOfPartEvents","This reader is not able to provide simulated particles."); | |
249 | return 0; | |
250 | } | |
251 | ||
252 | if (!fIsRead) | |
253 | { | |
254 | if (ReadsSim() && (fRunSim == 0x0)) fRunSim = new AliAODRun(); | |
255 | if (ReadsRec() && (fRunRec == 0x0)) fRunRec = new AliAODRun(); | |
256 | ||
257 | if (Read(fRunSim,fRunRec)) | |
258 | { | |
259 | Error("GetNumberOfPartEvents","Error in reading"); | |
260 | return 0; | |
261 | } | |
262 | else fIsRead = kTRUE; | |
263 | } | |
264 | return fRunSim->GetNumberOfEvents(); | |
265 | } | |
266 | /********************************************************************/ | |
267 | ||
268 | Int_t AliReader::GetNumberOfRecEvents() | |
269 | { | |
270 | //returns number of events of tracks | |
271 | if (ReadsRec() == kFALSE) | |
272 | { | |
273 | Error("GetNumberOfTrackEvents","This reader is not able to provide recosntructed tracks."); | |
274 | return 0; | |
275 | } | |
276 | if (!fIsRead) | |
277 | { | |
278 | if (ReadsSim() && (fRunSim == 0x0)) fRunSim = new AliAODRun(); | |
279 | if (ReadsRec() && (fRunRec == 0x0)) fRunRec = new AliAODRun(); | |
280 | ||
281 | if(Read(fRunSim,fRunRec)) | |
282 | { | |
283 | Error("GetNumberOfTrackEvents","Error in reading"); | |
284 | return 0; | |
285 | } | |
286 | else fIsRead = kTRUE; | |
287 | } | |
288 | return fRunRec->GetNumberOfEvents(); | |
289 | } | |
290 | /********************************************************************/ | |
291 | ||
292 | Int_t AliReader::Read(AliAODRun* particles, AliAODRun *tracks) | |
293 | { | |
294 | //reads data and puts put to the particles and tracks objects | |
295 | //reurns 0 if everything is OK | |
296 | // | |
297 | Info("Read",""); | |
298 | ||
299 | if ( ReadsSim() && (particles == 0x0) ) //check if an object is instatiated | |
300 | { | |
301 | Error("Read"," particles object must be instatiated before passing it to the reader"); | |
302 | return 1; | |
303 | } | |
304 | if ( ReadsRec() && (tracks == 0x0) ) //check if an object is instatiated | |
305 | { | |
306 | Error("Read"," tracks object must be instatiated before passing it to the reader"); | |
307 | return 1; | |
308 | } | |
309 | ||
310 | if (ReadsSim()) particles->Reset();//clear runs == delete all old events | |
311 | if (ReadsRec()) tracks->Reset(); | |
312 | ||
313 | Rewind(); | |
314 | ||
315 | Int_t i = 0; | |
316 | while(Next() == kFALSE) | |
317 | { | |
318 | if (ReadsRec()) tracks->SetEvent(i,fEventRec); | |
319 | if (ReadsSim()) particles->SetEvent(i,fEventSim); | |
320 | i++; | |
321 | } | |
322 | return 0; | |
323 | } | |
324 | /*************************************************************************************/ | |
325 | ||
afa8b37b | 326 | Bool_t AliReader::Pass(AliVAODParticle* p) |
a5556ea5 | 327 | { |
328 | //Method examines whether particle meets all cut and particle type criteria | |
329 | ||
330 | if(p==0x0)//of corse we not pass NULL pointers | |
331 | { | |
332 | Warning("Pass()","No Pasaran! We never accept NULL pointers"); | |
333 | return kTRUE; | |
334 | } | |
335 | //if no particle is specified, we pass all particles | |
336 | //excluding NULL pointers, of course | |
337 | if ( fCuts->GetEntriesFast() == 0 ) return kFALSE; //if no cut specified accept all particles | |
338 | for(Int_t i=0; i<fCuts->GetEntriesFast(); i++) | |
339 | { | |
340 | AliAODParticleCut &cut = *((AliAODParticleCut*)fCuts->At(i)); | |
341 | if(!cut.Pass(p)) return kFALSE; //accepted | |
342 | } | |
343 | ||
344 | return kTRUE;//not accepted | |
345 | } | |
346 | /*************************************************************************************/ | |
347 | ||
348 | Bool_t AliReader::Pass(Int_t pid) | |
349 | { | |
350 | //this method checks if any of existing cuts accepts this pid particles | |
351 | //or any cuts accepts all particles | |
352 | ||
353 | if(pid == 0) | |
354 | return kTRUE; | |
355 | ||
356 | if ( fCuts->GetEntriesFast() == 0 ) return kFALSE; //if no cut specified accept all particles | |
357 | ||
358 | for(Int_t i=0; i<fCuts->GetEntriesFast(); i++) | |
359 | { | |
360 | AliAODParticleCut &cut = *((AliAODParticleCut*)fCuts->At(i)); | |
361 | //if some of cuts accepts all particles or some accepts particles of this type, accept | |
362 | if ( (cut.GetPID() == 0) || (cut.GetPID() == pid) ) return kFALSE; | |
363 | } | |
364 | return kTRUE; | |
365 | } | |
366 | /*************************************************************************************/ | |
367 | ||
368 | TString& AliReader::GetDirName(Int_t entry) | |
369 | { | |
370 | //returns directory name of next one to read | |
371 | TString* retval;//return value | |
372 | if (fDirs == 0x0) | |
373 | { | |
374 | retval = new TString("."); | |
375 | return *retval; | |
376 | } | |
377 | ||
378 | if ( (entry>fDirs->GetEntries()) || (entry<0))//if out of bounds return empty string | |
379 | { //note that entry==0 is accepted even if array is empty (size=0) | |
380 | Error("GetDirName","Name out of bounds"); | |
381 | retval = new TString(); | |
382 | return *retval; | |
383 | } | |
384 | ||
385 | if (fDirs->GetEntries() == 0) | |
386 | { | |
387 | retval = new TString("."); | |
388 | return *retval; | |
389 | } | |
390 | ||
391 | TClass *objclass = fDirs->At(entry)->IsA(); | |
392 | TClass *stringclass = TObjString::Class(); | |
393 | ||
394 | TObjString *dir = (TObjString*)objclass->DynamicCast(stringclass,fDirs->At(entry)); | |
395 | ||
396 | if(dir == 0x0) | |
397 | { | |
398 | Error("GetDirName","Object in TObjArray is not a TObjString or its descendant"); | |
399 | retval = new TString(); | |
400 | return *retval; | |
401 | } | |
402 | if (gDebug > 0) Info("GetDirName","Returned ok %s",dir->String().Data()); | |
403 | return dir->String(); | |
404 | } | |
405 | /*************************************************************************************/ | |
406 | ||
407 | void AliReader::Blend() | |
408 | { | |
409 | //randomly change positions of the particles after reading | |
410 | //is used to check if some distributions (of many particle properties) | |
411 | //depend on the order of particles | |
412 | //(tracking gives particles Pt sorted) | |
413 | ||
414 | if (fEventSim == 0x0) return; | |
415 | ||
416 | for (Int_t i = 2; i < fEventSim->GetNumberOfParticles(); i++) | |
417 | { | |
418 | Int_t with = gRandom->Integer(i); | |
419 | fEventSim->SwapParticles(i,with); | |
420 | if (fEventRec) fEventRec->SwapParticles(i,with); | |
421 | } | |
422 | } | |
423 | /*************************************************************************************/ | |
424 | ||
425 | void AliReader::WriteTrackCounter() const | |
426 | { | |
427 | //writes the counter histogram | |
428 | ||
429 | if (fTrackCounter) fTrackCounter->Write(0,TObject::kOverwrite); | |
430 | else | |
431 | { | |
432 | Warning("WriteTrackCounter","Counter is NULL"); | |
433 | } | |
434 | } |