]> git.uio.no Git - u/mrichter/AliRoot.git/blame - HBTAN/AliHBTReaderInternal.cxx
Mixing informative pritout corrected
[u/mrichter/AliRoot.git] / HBTAN / AliHBTReaderInternal.cxx
CommitLineData
91923a38 1#include "AliHBTReaderInternal.h"
2
91923a38 3#include <TTree.h>
4#include <TFile.h>
5#include <TParticle.h>
f701e886 6#include <TError.h>
91923a38 7#include <AliRun.h>
8#include <AliMagF.h>
9
10#include "AliHBTRun.h"
11#include "AliHBTEvent.h"
12#include "AliHBTParticle.h"
13#include "AliHBTParticleCut.h"
14
bed069a4 15AliHBTReaderInternal test;
91923a38 16
91923a38 17ClassImp(AliHBTReaderInternal)
18/********************************************************************/
19
bed069a4 20AliHBTReaderInternal::AliHBTReaderInternal():
21 fFileName(),
22 fPartBranch(0x0),
23 fTrackBranch(0x0),
24 fTree(0x0),
25 fFile(0x0)
91923a38 26{
f701e886 27//Defalut constructor
91923a38 28}
29/********************************************************************/
30
bed069a4 31AliHBTReaderInternal::AliHBTReaderInternal(const char *filename):
32 fFileName(filename),
33 fPartBranch(0x0),
34 fTrackBranch(0x0),
35 fTree(0x0),
36 fFile(0x0)
91923a38 37{
f701e886 38//constructor
39//filename - name of file to open
91923a38 40}
41/********************************************************************/
f701e886 42
91923a38 43AliHBTReaderInternal::AliHBTReaderInternal(TObjArray* dirs, const char *filename):
bed069a4 44 AliHBTReader(dirs),
45 fFileName(filename),
46 fPartBranch(0x0),
47 fTrackBranch(0x0),
48 fTree(0x0),
49 fFile(0x0)
91923a38 50{
f701e886 51//ctor
52//dirs contains strings with directories to look data in
53//filename - name of file to open
91923a38 54}
f701e886 55/********************************************************************/
56
91923a38 57AliHBTReaderInternal::~AliHBTReaderInternal()
58 {
59 //desctructor
bed069a4 60 delete fTree;
61 delete fFile;
91923a38 62 }
63/********************************************************************/
bed069a4 64
65void AliHBTReaderInternal::Rewind()
66{
67 delete fTree;
68 fTree = 0x0;
69 delete fFile;
70 fFile = 0x0;
71 fCurrentDir = 0;
72 fNEventsRead= 0;
73}
91923a38 74/********************************************************************/
75
bed069a4 76Int_t AliHBTReaderInternal::ReadNext()
91923a38 77 {
78 //reads data and puts put to the particles and tracks objects
79 //reurns 0 if everything is OK
80 //
bed069a4 81 Info("ReadNext","");
91923a38 82 Int_t i; //iterator and some temprary values
f701e886 83 Int_t counter;
9d09c308 84 AliHBTParticle* tpart = 0x0, *ttrack = 0x0;
bed069a4 85
27bb2a54 86 TDatabasePDG* pdgdb = TDatabasePDG::Instance();
87 if (pdgdb == 0x0)
88 {
bed069a4 89 Error("ReadNext","Can not get PDG Particles Data Base");
27bb2a54 90 return 1;
91923a38 91 }
bed069a4 92
93 if (fParticlesEvent == 0x0) fParticlesEvent = new AliHBTEvent();
94 if (fTracksEvent == 0x0) fTracksEvent = new AliHBTEvent();
95
96 fParticlesEvent->Reset();
97 fTracksEvent->Reset();
d4c1d7b6 98
91923a38 99 do //do{}while; is OK even if 0 dirs specified. In that case we try to read from "./"
100 {
bed069a4 101 if (fTree == 0x0)
102 if( (i=OpenNextFile()) )
103 {
4d91c73a 104 Error("ReadNext","Skipping directory due to problems with opening files. Errorcode %d",i);
bed069a4 105 fCurrentDir++;
106 continue;
107 }
108 if (fCurrentEvent == (Int_t)fTree->GetEntries())
91923a38 109 {
bed069a4 110 delete fTree;
111 fTree = 0x0;
112 delete fFile;
113 fFile = 0x0;
114 fPartBranch = 0x0;
115 fTrackBranch= 0x0;
116 fCurrentDir++;
27bb2a54 117 continue;
91923a38 118 }
119 /***************************/
120 /***************************/
121 /***************************/
122
bed069a4 123
124 Info("ReadNext","Event %d",fCurrentEvent);
125 fTree->GetEvent(fCurrentEvent);
91923a38 126
bed069a4 127 counter = 0;
128 if (fPartBranch && fTrackBranch)
129 {
130 for(i = 0; i < fPartBuffer->GetEntries(); i++)
131 {
132 tpart = dynamic_cast<AliHBTParticle*>(fPartBuffer->At(i));
133 ttrack = dynamic_cast<AliHBTParticle*>(fTrackBuffer->At(i));
134
135 if( tpart == 0x0 ) continue; //if returned pointer is NULL
136
137 if (ttrack->GetUID() != tpart->GetUID())
9d09c308 138 {
bed069a4 139 Error("ReadNext","Sth. is wrong: Track and Particle has different UID.");
140 Error("ReadNext","They probobly do not correspond to each other.");
141 }
9d09c308 142
bed069a4 143 for (Int_t s = 0; s < tpart->GetNumberOfPids(); s++)
144 {
145 if( pdgdb->GetParticle(tpart->GetNthPid(s)) == 0x0 ) continue; //if particle has crazy PDG code (not known to our database)
146 if( Pass(tpart->GetNthPid(s)) ) continue; //check if we are intersted with particles of this type
147 //if not take next partilce
148 AliHBTParticle* part = new AliHBTParticle(*tpart);
149 part->SetPdgCode(tpart->GetNthPid(s),tpart->GetNthPidProb(s));
150 if( Pass(part) )
27bb2a54 151 {
bed069a4 152 delete part;
153 continue;
27bb2a54 154 }
bed069a4 155 AliHBTParticle* track = new AliHBTParticle(*ttrack);
156
157 fParticlesEvent->AddParticle(part);
158 fTracksEvent->AddParticle(track);
159
160 counter++;
161 }
162 }
163 Info("ReadNext"," Read: %d particles and tracks.",counter);
164 }
165 else
166 {
167 if (fPartBranch)
168 {
169 Info("ReadNext","Found %d particles in total.",fPartBuffer->GetEntries());
170 for(i = 0; i < fPartBuffer->GetEntries(); i++)
171 {
172 tpart = dynamic_cast<AliHBTParticle*>(fPartBuffer->At(i));
173 if(tpart == 0x0) continue; //if returned pointer is NULL
174
175 for (Int_t s = 0; s < tpart->GetNumberOfPids(); s++)
176 {
177 if( pdgdb->GetParticle(tpart->GetNthPid(s)) == 0x0 ) continue; //if particle has crazy PDG code (not known to our database)
178 if( Pass(tpart->GetNthPid(s)) ) continue; //check if we are intersted with particles of this type
179
180 AliHBTParticle* part = new AliHBTParticle(*tpart);
181 part->SetPdgCode(tpart->GetNthPid(s),tpart->GetNthPidProb(s));
182 if( Pass(part) )
27bb2a54 183 {
bed069a4 184 delete part;
185 continue;
27bb2a54 186 }
bed069a4 187 fParticlesEvent->AddParticle(part);
188 counter++;
189 }
190 }
191 Info("ReadNext"," Read: %d particles.",counter);
192 }
193 else Info("ReadNext"," Read: 0 particles.");
194
195 if (fTrackBranch)
196 {
197 for(i = 0; i < fTrackBuffer->GetEntries(); i++)
198 {
199 tpart = dynamic_cast<AliHBTParticle*>(fTrackBuffer->At(i));
200 if(tpart == 0x0) continue; //if returned pointer is NULL
201 for (Int_t s = 0; s < tpart->GetNumberOfPids(); s++)
202 {
203 if( pdgdb->GetParticle(tpart->GetNthPid(s)) == 0x0 ) continue; //if particle has crazy PDG code (not known to our database)
204 if( Pass(tpart->GetNthPid(s)) ) continue; //check if we are intersted with particles of this type
205
206 AliHBTParticle* part = new AliHBTParticle(*tpart);
207 part->SetPdgCode(tpart->GetNthPid(s),tpart->GetNthPidProb(s));
208 if( Pass(part) )
27bb2a54 209 {
bed069a4 210 delete part;
211 continue;
27bb2a54 212 }
bed069a4 213 fTracksEvent->AddParticle(part);
214 counter++;
215 }
216 }
217 Info("ReadNext"," Read: %d tracks",counter);
218 }
219 else Info("ReadNext"," Read: 0 tracks.");
220 }
d7f24a3d 221
222 if (fPartBuffer) fPartBuffer->Delete();
223 if (fTrackBuffer) fTrackBuffer->Delete();
bed069a4 224
225 fCurrentEvent++;
226 fNEventsRead++;
227
228 return 0;
9d09c308 229
4d91c73a 230 }while(fCurrentDir < GetNumberOfDirs());
91923a38 231
bed069a4 232 return 1;//no more directories to read
91923a38 233}
91923a38 234/********************************************************************/
235
bed069a4 236Int_t AliHBTReaderInternal::OpenNextFile()
91923a38 237{
bed069a4 238 //open file in current directory
4d91c73a 239 const TString& dirname = GetDirName(fCurrentDir);
91923a38 240 if (dirname == "")
241 {
bed069a4 242 Error("OpenNextFile","Can not get directory name");
91923a38 243 return 4;
244 }
245
246 TString filename = dirname +"/"+ fFileName;
bed069a4 247 fFile = TFile::Open(filename.Data());
248 if ( fFile == 0x0 )
91923a38 249 {
bed069a4 250 Error("OpenNextFile","Can't open file named %s",filename.Data());
91923a38 251 return 1;
252 }
bed069a4 253 if (fFile->IsOpen() == kFALSE)
91923a38 254 {
bed069a4 255 Error("OpenNextFile","Can't open filenamed %s",filename.Data());
91923a38 256 return 1;
257 }
bed069a4 258
259 fTree = (TTree*)fFile->Get("data");
260 if (fTree == 0x0)
261 {
262 Error("OpenNextFile","Can not get the tree.");
263 return 1;
264 }
265
266
267 fTrackBranch = fTree->GetBranch("tracks");//get the branch with tracks
268 if (fTrackBranch == 0x0) ////check if we got the branch
269 {//if not return with error
270 Info("OpenNextFile","Can't find a branch with tracks !\n");
271 }
272 else
273 {
274 if (fTrackBuffer == 0x0)
275 fTrackBuffer = new TClonesArray("AliHBTParticle",15000);
276 fTrackBranch->SetAddress(&fTrackBuffer);
277 }
278
279 fPartBranch = fTree->GetBranch("particles");//get the branch with particles
280 if (fPartBranch == 0x0) ////check if we got the branch
281 {//if not return with error
282 Info("OpenNextFile","Can't find a branch with particles !\n");
283 }
284 else
285 {
286 if (fPartBuffer == 0x0)
287 fPartBuffer = new TClonesArray("AliHBTParticle",15000);
288 fPartBranch->SetAddress(&fPartBuffer);
289 }
290
291 Info("OpenNextFile","________________________________________________________");
292 Info("OpenNextFile","Found %d event(s) in directory %s",
293 (Int_t)fTree->GetEntries(),GetDirName(fCurrentEvent).Data());
294
295 fCurrentEvent = 0;
296
91923a38 297 return 0;
298}
f701e886 299/********************************************************************/
91923a38 300
27bb2a54 301Int_t AliHBTReaderInternal::Write(AliHBTReader* reader,const char* outfile, Bool_t multcheck)
91923a38 302 {
f701e886 303 //reads tracks from reader and writes runs to file
27bb2a54 304 //reader - provides data for writing in internal format
305 //name of output file
306 //multcheck - switches of checking if particle was stored with other incarnation
307 // usefull e.g. when using kine data, where all particles have 100% pid prob.and saves a lot of time
308
f701e886 309 Int_t i,j;
91923a38 310
f701e886 311 ::Info("AliHBTReaderInternal::Write","________________________________________________________");
312 ::Info("AliHBTReaderInternal::Write","________________________________________________________");
313 ::Info("AliHBTReaderInternal::Write","________________________________________________________");
9d09c308 314
91923a38 315 TFile *histoOutput = TFile::Open(outfile,"recreate");
316
317 if (!histoOutput->IsOpen())
318 {
f701e886 319 ::Error("AliHBTReaderInternal::Write","File is not opened");
91923a38 320 return 1;
321 }
322
91923a38 323 TTree *tracktree = new TTree("data","Tree with tracks");
324
325 TClonesArray* pbuffer = new TClonesArray("AliHBTParticle",15000);
326 TClonesArray* tbuffer = new TClonesArray("AliHBTParticle",15000);
91923a38 327
328 TClonesArray &particles = *pbuffer;
329 TClonesArray &tracks = *tbuffer;
330
331 TString name("Tracks");
91923a38 332
27bb2a54 333 Int_t nt = reader->GetNumberOfTrackEvents();
334 Int_t np = reader->GetNumberOfPartEvents();
91923a38 335
27bb2a54 336 if (AliHBTParticle::GetDebug() > 0)
337 ::Info("Write","Reader has %d track events and %d particles events.",nt,np);
338
339 Bool_t trck = (nt > 0) ? kTRUE : kFALSE;
340 Bool_t part = (np > 0) ? kTRUE : kFALSE;
91923a38 341
342 TBranch *trackbranch = 0x0, *partbranch = 0x0;
343
d4c1d7b6 344 if (trck) trackbranch = tracktree->Branch("tracks",&tbuffer,32000,0);
345 if (part) partbranch = tracktree->Branch("particles",&pbuffer,32000,0);
91923a38 346
27bb2a54 347 if ( (trck) && (part) && (np != nt))
91923a38 348 {
f701e886 349 ::Warning("AliHBTReaderInternal::Write","Number of track and particle events is different");
91923a38 350 }
351
27bb2a54 352 Int_t n;
353 if (nt >= np ) n = nt; else n = np;
354
355 if (AliHBTParticle::GetDebug() > 0)
356 ::Info("Write","Will loop over %d events",n);
91923a38 357
27bb2a54 358 for ( i =0;i< n; i++)
91923a38 359 {
f701e886 360 ::Info("AliHBTReaderInternal::Write","Event %d",i+1);
27bb2a54 361 Int_t counter = 0;
362 if (trck && (i<=nt))
363 {
91923a38 364 AliHBTEvent* trackev = reader->GetTrackEvent(i);
365 for ( j = 0; j< trackev->GetNumberOfParticles();j++)
366 {
27bb2a54 367 const AliHBTParticle& t = *(trackev->GetParticle(j));
368 if (multcheck)
369 {
370 if (FindIndex(tbuffer,t.GetUID()))
371 {
372 if (AliHBTParticle::GetDebug()>4)
373 {
374 ::Info("Write","Track with Event UID %d already stored",t.GetUID());
375 }
376 continue; //not to write the same particles with other incarnations
377 }
378 }
379 new (tracks[counter++]) AliHBTParticle(t);
91923a38 380 }
27bb2a54 381 ::Info("AliHBTReaderInternal::Write"," Tracks: %d",tracks.GetEntries());
f701e886 382 }else ::Info("AliHBTReaderInternal::Write","NO TRACKS");
91923a38 383
27bb2a54 384 counter = 0;
385 if (part && (i<=np))
91923a38 386 {
27bb2a54 387// ::Warning("AliHBTReaderInternal::Write","Find index switched off!!!");
388
91923a38 389 AliHBTEvent* partev = reader->GetParticleEvent(i);
91923a38 390 for ( j = 0; j< partev->GetNumberOfParticles();j++)
391 {
9d09c308 392 const AliHBTParticle& part= *(partev->GetParticle(j));
27bb2a54 393 if (multcheck)
394 {
395 if (FindIndex(pbuffer,part.GetUID()))
396 {
397 if (AliHBTParticle::GetDebug()>4)
398 {
399 ::Info("Write","Particle with Event UID %d already stored",part.GetUID());
400 }
401 continue; //not to write the same particles with other incarnations
402 }
403 }
404 new (particles[counter++]) AliHBTParticle(part);
91923a38 405 }
27bb2a54 406 ::Info("AliHBTReaderInternal::Write"," Particles: %d",particles.GetEntries());
f701e886 407 }else ::Info("AliHBTReaderInternal::Write","NO PARTICLES");
91923a38 408
91923a38 409 histoOutput->cd();
410 tracktree->Fill();
9d09c308 411 tracktree->AutoSave();
91923a38 412 tbuffer->Delete();
413 pbuffer->Delete();
91923a38 414 }
415
91923a38 416 histoOutput->cd();
91923a38 417 tracktree->Write(0,TObject::kOverwrite);
9d09c308 418 delete tracktree;
91923a38 419
9d09c308 420 tbuffer->SetOwner();
421 pbuffer->SetOwner();
422 delete pbuffer;
423 delete tbuffer;
424
425 histoOutput->Close();
91923a38 426 return 0;
427 }
27bb2a54 428/********************************************************************/
429
430Bool_t AliHBTReaderInternal::FindIndex(TClonesArray* arr,Int_t idx)
431{
432//Checks if in the array exists already partilce with Unique ID idx
433 if (arr == 0x0)
434 {
435 ::Error("FindIndex","Array is 0x0");
436 return kTRUE;
437 }
438 TIter next(arr);
439 AliHBTParticle* p;
440 while (( p = (AliHBTParticle*)next()))
441 {
442 if (p->GetUID() == idx) return kTRUE;
443 }
444 return kFALSE;
445}