1 #include "AliHBTReaderInternal.h"
10 #include "AliHBTRun.h"
11 #include "AliHBTEvent.h"
12 #include "AliHBTParticle.h"
13 #include "AliHBTParticleCut.h"
15 AliHBTReaderInternal test;
17 ClassImp(AliHBTReaderInternal)
18 /********************************************************************/
20 AliHBTReaderInternal::AliHBTReaderInternal():
31 /********************************************************************/
33 AliHBTReaderInternal::AliHBTReaderInternal(const char *filename):
43 //filename - name of file to open
45 /********************************************************************/
47 AliHBTReaderInternal::AliHBTReaderInternal(TObjArray* dirs, const char *filename):
58 //dirs contains strings with directories to look data in
59 //filename - name of file to open
61 /********************************************************************/
63 AliHBTReaderInternal::~AliHBTReaderInternal()
69 /********************************************************************/
71 void AliHBTReaderInternal::Rewind()
80 /********************************************************************/
82 Int_t AliHBTReaderInternal::ReadNext()
84 //reads data and puts put to the particles and tracks objects
85 //reurns 0 if everything is OK
88 Int_t i; //iterator and some temprary values
90 AliHBTParticle* tpart = 0x0, *ttrack = 0x0;
92 TDatabasePDG* pdgdb = TDatabasePDG::Instance();
95 Error("ReadNext","Can not get PDG Particles Data Base");
99 if (fParticlesEvent == 0x0) fParticlesEvent = new AliHBTEvent();
100 if (fTracksEvent == 0x0) fTracksEvent = new AliHBTEvent();
102 fParticlesEvent->Reset();
103 fTracksEvent->Reset();
105 do //do{}while; is OK even if 0 dirs specified. In that case we try to read from "./"
108 if( (i=OpenNextFile()) )
110 Error("ReadNext","Skipping directory due to problems with opening files. Errorcode %d",i);
114 if (fCurrentEvent == (Int_t)fTree->GetEntries())
125 /***************************/
126 /***************************/
127 /***************************/
130 Info("ReadNext","Event %d",fCurrentEvent);
131 fTree->GetEvent(fCurrentEvent);
134 if (fPartBranch && fTrackBranch)
136 Info("ReadNext","Found %d tracks in total.",fTrackBuffer->GetEntries());
137 Info("ReadNext","Found %d particles in total.",fPartBuffer->GetEntries());
138 for(i = 0; i < fPartBuffer->GetEntries(); i++)
140 tpart = dynamic_cast<AliHBTParticle*>(fPartBuffer->At(i));
141 ttrack = dynamic_cast<AliHBTParticle*>(fTrackBuffer->At(i));
143 if( tpart == 0x0 ) continue; //if returned pointer is NULL
144 if( ttrack == 0x0 ) continue; //if returned pointer is NULL
146 if (AliHBTParticle::GetDebug() > 9)
148 Info("ReadNext","Particle:");
150 Info("ReadNext","Track:");
153 if (ttrack->GetUID() != tpart->GetUID())
155 Error("ReadNext","Sth. is wrong: Track and Particle has different UID.");
156 Error("ReadNext","They probobly do not correspond to each other.");
159 for (Int_t s = 0; s < ttrack->GetNumberOfPids(); s++)
161 //check if we are intersted with particles of this type
162 //if not take next partilce
163 if( Pass(ttrack->GetNthPid(s)) )
165 if (AliHBTParticle::GetDebug() > 9)
166 Info("ReadNext","Track Incarnation %d did not pass PID cut.",ttrack->GetNthPid(s));
169 TParticlePDG* pdgp = pdgdb->GetParticle(ttrack->GetNthPid(s));
170 if (pdgp == 0x0)//PDG part corresponding to new incarnation
172 Error("ReadNext","Particle code unknown to PDG DB.");
176 AliHBTParticle* track = new AliHBTParticle(*ttrack);
178 //apart of setting PDG code of an incarnation
179 //it is necessary tu recalculate energy on the basis of
180 //new PDG code (mass) hypothesis
181 Double_t mass = pdgp->Mass();//mass of new incarnation
182 Double_t tEtot = TMath::Sqrt( ttrack->Px()*ttrack->Px() +
183 ttrack->Py()*ttrack->Py() +
184 ttrack->Pz()*ttrack->Pz() +
185 mass*mass);//total energy of the new incarnation
186 //update Energy and Calc Mass
187 track->SetMomentum(ttrack->Px(),ttrack->Py(),ttrack->Pz(),tEtot);
188 track->SetCalcMass(mass);
189 track->SetPdgCode(ttrack->GetNthPid(s),ttrack->GetNthPidProb(s));
193 if (AliHBTParticle::GetDebug() > 9)
194 Info("ReadNext","Track Incarnation %d did not pass cut.",ttrack->GetNthPid(s));
198 AliHBTParticle* part = new AliHBTParticle(*tpart);//particle has only 1 incarnation (real)
200 fParticlesEvent->AddParticle(part);
201 fTracksEvent->AddParticle(track);
206 Info("ReadNext"," Read: %d particles and tracks.",counter);
212 Info("ReadNext","Found %d particles in total.",fPartBuffer->GetEntries());
213 for(i = 0; i < fPartBuffer->GetEntries(); i++)
215 tpart = dynamic_cast<AliHBTParticle*>(fPartBuffer->At(i));
216 if(tpart == 0x0) continue; //if returned pointer is NULL
218 for (Int_t s = 0; s < tpart->GetNumberOfPids(); s++)
220 if( pdgdb->GetParticle(tpart->GetNthPid(s)) == 0x0 ) continue; //if particle has crazy PDG code (not known to our database)
221 if( Pass(tpart->GetNthPid(s)) ) continue; //check if we are intersted with particles of this type
222 AliHBTParticle* part = new AliHBTParticle(*tpart);
223 part->SetPdgCode(tpart->GetNthPid(s),tpart->GetNthPidProb(s));
229 fParticlesEvent->AddParticle(part);
233 Info("ReadNext"," Read: %d particles.",counter);
235 else Info("ReadNext"," Read: 0 particles.");
239 Info("ReadNext","Found %d tracks in total.",fTrackBuffer->GetEntries());
240 for(i = 0; i < fTrackBuffer->GetEntries(); i++)
242 ttrack = dynamic_cast<AliHBTParticle*>(fTrackBuffer->At(i));
243 if( ttrack == 0x0 ) continue; //if returned pointer is NULL
245 for (Int_t s = 0; s < ttrack->GetNumberOfPids(); s++)
247 if( Pass(ttrack->GetNthPid(s)) ) continue; //check if we are intersted with particles of this type
248 //if not take next partilce
249 TParticlePDG* pdgp = pdgdb->GetParticle(ttrack->GetNthPid(s));
250 if (pdgp == 0x0)//PDG part corresponding to new incarnation
252 Error("ReadNext","Particle code unknown to PDG DB.");
255 AliHBTParticle* track = new AliHBTParticle(*ttrack);
257 //apart of setting PDG code of an incarnation
258 //it is necessary tu recalculate energy on the basis of
259 //new PDG code (mass) hypothesis
260 Double_t mass = pdgp->Mass();//mass of new incarnation
261 Double_t tEtot = TMath::Sqrt( ttrack->Px()*ttrack->Px() +
262 ttrack->Py()*ttrack->Py() +
263 ttrack->Pz()*ttrack->Pz() +
264 mass*mass);//total energy of the new incarnation
265 //update Energy and Calc Mass
266 track->SetMomentum(ttrack->Px(),ttrack->Py(),ttrack->Pz(),tEtot);
267 track->SetCalcMass(mass);
268 track->SetPdgCode(ttrack->GetNthPid(s),ttrack->GetNthPidProb(s));
275 fTracksEvent->AddParticle(track);
280 Info("ReadNext"," Read: %d tracks",counter);
282 else Info("ReadNext"," Read: 0 tracks.");
285 if (fPartBuffer) fPartBuffer->Delete();
286 if (fTrackBuffer) fTrackBuffer->Delete();
293 }while(fCurrentDir < GetNumberOfDirs());
295 return 1;//no more directories to read
297 /********************************************************************/
299 Int_t AliHBTReaderInternal::OpenNextFile()
301 //open file in current directory
302 const TString& dirname = GetDirName(fCurrentDir);
305 Error("OpenNextFile","Can not get directory name");
309 TString filename = dirname +"/"+ fFileName;
310 fFile = TFile::Open(filename.Data());
313 Error("OpenNextFile","Can't open file named %s",filename.Data());
316 if (fFile->IsOpen() == kFALSE)
318 Error("OpenNextFile","Can't open filenamed %s",filename.Data());
322 fTree = (TTree*)fFile->Get("data");
325 Error("OpenNextFile","Can not get the tree.");
330 fTrackBranch = fTree->GetBranch("tracks");//get the branch with tracks
331 if (fTrackBranch == 0x0) ////check if we got the branch
332 {//if not return with error
333 Info("OpenNextFile","Can't find a branch with tracks !\n");
337 if (fTrackBuffer == 0x0)
338 fTrackBuffer = new TClonesArray("AliHBTParticle",15000);
339 fTrackBranch->SetAddress(&fTrackBuffer);
342 fPartBranch = fTree->GetBranch("particles");//get the branch with particles
343 if (fPartBranch == 0x0) ////check if we got the branch
344 {//if not return with error
345 Info("OpenNextFile","Can't find a branch with particles !\n");
349 if (fPartBuffer == 0x0)
350 fPartBuffer = new TClonesArray("AliHBTParticle",15000);
351 fPartBranch->SetAddress(&fPartBuffer);
354 Info("OpenNextFile","________________________________________________________");
355 Info("OpenNextFile","Found %d event(s) in directory %s",
356 (Int_t)fTree->GetEntries(),GetDirName(fCurrentDir).Data());
362 /********************************************************************/
364 Int_t AliHBTReaderInternal::Write(AliHBTReader* reader,const char* outfile, Bool_t multcheck)
366 //reads tracks from reader and writes runs to file
367 //reader - provides data for writing in internal format
368 //name of output file
369 //multcheck - switches of checking if particle was stored with other incarnation
370 // usefull e.g. when using kine data, where all particles have 100% pid prob.and saves a lot of time
374 ::Info("AliHBTReaderInternal::Write","________________________________________________________");
375 ::Info("AliHBTReaderInternal::Write","________________________________________________________");
376 ::Info("AliHBTReaderInternal::Write","________________________________________________________");
378 TFile *histoOutput = TFile::Open(outfile,"recreate");
380 if (!histoOutput->IsOpen())
382 ::Error("AliHBTReaderInternal::Write","File is not opened");
386 TTree *tracktree = new TTree("data","Tree with tracks");
388 TClonesArray* pbuffer = new TClonesArray("AliHBTParticle",15000);
389 TClonesArray* tbuffer = new TClonesArray("AliHBTParticle",15000);
391 TClonesArray &particles = *pbuffer;
392 TClonesArray &tracks = *tbuffer;
394 TString name("Tracks");
396 Int_t nt = reader->GetNumberOfTrackEvents();
397 Int_t np = reader->GetNumberOfPartEvents();
399 if (AliHBTParticle::GetDebug() > 0)
400 ::Info("Write","Reader has %d track events and %d particles events.",nt,np);
402 Bool_t trck = (nt > 0) ? kTRUE : kFALSE;
403 Bool_t part = (np > 0) ? kTRUE : kFALSE;
405 TBranch *trackbranch = 0x0, *partbranch = 0x0;
407 if (trck) trackbranch = tracktree->Branch("tracks",&tbuffer,32000,0);
408 if (part) partbranch = tracktree->Branch("particles",&pbuffer,32000,0);
410 if ( (trck) && (part) && (np != nt))
412 ::Warning("AliHBTReaderInternal::Write","Number of track and particle events is different");
416 if (nt >= np ) n = nt; else n = np;
418 if (AliHBTParticle::GetDebug() > 0)
419 ::Info("Write","Will loop over %d events",n);
421 for ( i =0;i< n; i++)
423 ::Info("AliHBTReaderInternal::Write","Event %d",i+1);
427 AliHBTEvent* trackev = reader->GetTrackEvent(i);
428 for ( j = 0; j< trackev->GetNumberOfParticles();j++)
430 const AliHBTParticle& t = *(trackev->GetParticle(j));
433 if (FindIndex(tbuffer,t.GetUID()))
435 if (AliHBTParticle::GetDebug()>4)
437 ::Info("Write","Track with Event UID %d already stored",t.GetUID());
439 continue; //not to write the same particles with other incarnations
442 new (tracks[counter++]) AliHBTParticle(t);
444 ::Info("AliHBTReaderInternal::Write"," Tracks: %d",tracks.GetEntries());
445 }else ::Info("AliHBTReaderInternal::Write","NO TRACKS");
450 // ::Warning("AliHBTReaderInternal::Write","Find index switched off!!!");
452 AliHBTEvent* partev = reader->GetParticleEvent(i);
453 for ( j = 0; j< partev->GetNumberOfParticles();j++)
455 const AliHBTParticle& part= *(partev->GetParticle(j));
458 if (FindIndex(pbuffer,part.GetUID()))
460 if (AliHBTParticle::GetDebug()>4)
462 ::Info("Write","Particle with Event UID %d already stored",part.GetUID());
464 continue; //not to write the same particles with other incarnations
467 new (particles[counter++]) AliHBTParticle(part);
469 ::Info("AliHBTReaderInternal::Write"," Particles: %d",particles.GetEntries());
470 }else ::Info("AliHBTReaderInternal::Write","NO PARTICLES");
474 tracktree->AutoSave();
480 tracktree->Write(0,TObject::kOverwrite);
488 histoOutput->Close();
491 /********************************************************************/
493 Bool_t AliHBTReaderInternal::FindIndex(TClonesArray* arr,Int_t idx)
495 //Checks if in the array exists already partilce with Unique ID idx
498 ::Error("FindIndex","Array is 0x0");
503 while (( p = (AliHBTParticle*)next()))
505 if (p->GetUID() == idx) return kTRUE;