1 #include "AliHBTReaderInternal.h"
2 //_________________________________________________________________________
3 ///////////////////////////////////////////////////////////////////////////
5 // class AliHBTReaderInternal //
7 // Multi file reader for Internal Data Format //
9 // This reader reads data created by itself //
10 // (method AliHBTReaderInternal::Write) //
11 // Data are stored in form of tree of TClonesArray of AliHBTParticle's //
13 // Piotr.Skowronski@cern.ch //
14 // more info: http://alisoft.cern.ch/people/skowron/analyzer/index.html //
16 ///////////////////////////////////////////////////////////////////////////
20 #include <TParticle.h>
25 #include "AliHBTRun.h"
26 #include "AliHBTEvent.h"
27 #include "AliHBTParticle.h"
28 #include "AliHBTParticleCut.h"
30 AliHBTReaderInternal test;
32 ClassImp(AliHBTReaderInternal)
33 /********************************************************************/
35 AliHBTReaderInternal::AliHBTReaderInternal():
46 /********************************************************************/
48 AliHBTReaderInternal::AliHBTReaderInternal(const char *filename):
58 //filename - name of file to open
60 /********************************************************************/
62 AliHBTReaderInternal::AliHBTReaderInternal(TObjArray* dirs, const char *filename):
73 //dirs contains strings with directories to look data in
74 //filename - name of file to open
76 /********************************************************************/
78 AliHBTReaderInternal::~AliHBTReaderInternal()
84 /********************************************************************/
86 void AliHBTReaderInternal::Rewind()
95 /********************************************************************/
97 Int_t AliHBTReaderInternal::ReadNext()
99 //reads data and puts put to the particles and tracks objects
100 //reurns 0 if everything is OK
103 Int_t i; //iterator and some temprary values
105 AliHBTParticle* tpart = 0x0, *ttrack = 0x0;
107 TDatabasePDG* pdgdb = TDatabasePDG::Instance();
110 Error("ReadNext","Can not get PDG Particles Data Base");
114 if (fParticlesEvent == 0x0) fParticlesEvent = new AliHBTEvent();
115 if (fTracksEvent == 0x0) fTracksEvent = new AliHBTEvent();
117 fParticlesEvent->Reset();
118 fTracksEvent->Reset();
120 do //do{}while; is OK even if 0 dirs specified. In that case we try to read from "./"
123 if( (i=OpenNextFile()) )
125 Error("ReadNext","Skipping directory due to problems with opening files. Errorcode %d",i);
129 if (fCurrentEvent == (Int_t)fTree->GetEntries())
140 /***************************/
141 /***************************/
142 /***************************/
145 Info("ReadNext","Event %d",fCurrentEvent);
146 fTree->GetEvent(fCurrentEvent);
149 if (fPartBranch && fTrackBranch)
151 Info("ReadNext","Found %d tracks in total.",fTrackBuffer->GetEntries());
152 Info("ReadNext","Found %d particles in total.",fPartBuffer->GetEntries());
153 for(i = 0; i < fPartBuffer->GetEntries(); i++)
155 tpart = dynamic_cast<AliHBTParticle*>(fPartBuffer->At(i));
156 ttrack = dynamic_cast<AliHBTParticle*>(fTrackBuffer->At(i));
158 if( tpart == 0x0 ) continue; //if returned pointer is NULL
159 if( ttrack == 0x0 ) continue; //if returned pointer is NULL
161 if (AliHBTParticle::GetDebug() > 9)
163 Info("ReadNext","Particle:");
165 Info("ReadNext","Track:");
168 if (ttrack->GetUID() != tpart->GetUID())
170 Error("ReadNext","Sth. is wrong: Track and Particle has different UID.");
171 Error("ReadNext","They probobly do not correspond to each other.");
174 for (Int_t s = 0; s < ttrack->GetNumberOfPids(); s++)
176 //check if we are intersted with particles of this type
177 //if not take next partilce
178 if( Pass(ttrack->GetNthPid(s)) )
180 if (AliHBTParticle::GetDebug() > 9)
181 Info("ReadNext","Track Incarnation %d did not pass PID cut.",ttrack->GetNthPid(s));
184 TParticlePDG* pdgp = pdgdb->GetParticle(ttrack->GetNthPid(s));
185 if (pdgp == 0x0)//PDG part corresponding to new incarnation
187 Error("ReadNext","Particle code unknown to PDG DB.");
191 AliHBTParticle* track = new AliHBTParticle(*ttrack);
193 //apart of setting PDG code of an incarnation
194 //it is necessary tu recalculate energy on the basis of
195 //new PDG code (mass) hypothesis
196 Double_t mass = pdgp->Mass();//mass of new incarnation
197 Double_t tEtot = TMath::Sqrt( ttrack->Px()*ttrack->Px() +
198 ttrack->Py()*ttrack->Py() +
199 ttrack->Pz()*ttrack->Pz() +
200 mass*mass);//total energy of the new incarnation
201 //update Energy and Calc Mass
202 track->SetMomentum(ttrack->Px(),ttrack->Py(),ttrack->Pz(),tEtot);
203 track->SetCalcMass(mass);
204 track->SetPdgCode(ttrack->GetNthPid(s),ttrack->GetNthPidProb(s));
208 if (AliHBTParticle::GetDebug() > 9)
209 Info("ReadNext","Track Incarnation %d did not pass cut.",ttrack->GetNthPid(s));
213 AliHBTParticle* part = new AliHBTParticle(*tpart);//particle has only 1 incarnation (real)
215 fParticlesEvent->AddParticle(part);
216 fTracksEvent->AddParticle(track);
221 Info("ReadNext"," Read: %d particles and tracks.",counter);
227 Info("ReadNext","Found %d particles in total.",fPartBuffer->GetEntries());
228 for(i = 0; i < fPartBuffer->GetEntries(); i++)
230 tpart = dynamic_cast<AliHBTParticle*>(fPartBuffer->At(i));
231 if(tpart == 0x0) continue; //if returned pointer is NULL
233 for (Int_t s = 0; s < tpart->GetNumberOfPids(); s++)
235 if( pdgdb->GetParticle(tpart->GetNthPid(s)) == 0x0 ) continue; //if particle has crazy PDG code (not known to our database)
236 if( Pass(tpart->GetNthPid(s)) ) continue; //check if we are intersted with particles of this type
237 AliHBTParticle* part = new AliHBTParticle(*tpart);
238 part->SetPdgCode(tpart->GetNthPid(s),tpart->GetNthPidProb(s));
244 fParticlesEvent->AddParticle(part);
248 Info("ReadNext"," Read: %d particles.",counter);
250 else Info("ReadNext"," Read: 0 particles.");
254 Info("ReadNext","Found %d tracks in total.",fTrackBuffer->GetEntries());
255 for(i = 0; i < fTrackBuffer->GetEntries(); i++)
257 ttrack = dynamic_cast<AliHBTParticle*>(fTrackBuffer->At(i));
258 if( ttrack == 0x0 ) continue; //if returned pointer is NULL
260 for (Int_t s = 0; s < ttrack->GetNumberOfPids(); s++)
262 if( Pass(ttrack->GetNthPid(s)) ) continue; //check if we are intersted with particles of this type
263 //if not take next partilce
264 TParticlePDG* pdgp = pdgdb->GetParticle(ttrack->GetNthPid(s));
265 if (pdgp == 0x0)//PDG part corresponding to new incarnation
267 Error("ReadNext","Particle code unknown to PDG DB.");
270 AliHBTParticle* track = new AliHBTParticle(*ttrack);
272 //apart of setting PDG code of an incarnation
273 //it is necessary tu recalculate energy on the basis of
274 //new PDG code (mass) hypothesis
275 Double_t mass = pdgp->Mass();//mass of new incarnation
276 Double_t tEtot = TMath::Sqrt( ttrack->Px()*ttrack->Px() +
277 ttrack->Py()*ttrack->Py() +
278 ttrack->Pz()*ttrack->Pz() +
279 mass*mass);//total energy of the new incarnation
280 //update Energy and Calc Mass
281 track->SetMomentum(ttrack->Px(),ttrack->Py(),ttrack->Pz(),tEtot);
282 track->SetCalcMass(mass);
283 track->SetPdgCode(ttrack->GetNthPid(s),ttrack->GetNthPidProb(s));
290 fTracksEvent->AddParticle(track);
295 Info("ReadNext"," Read: %d tracks",counter);
297 else Info("ReadNext"," Read: 0 tracks.");
300 if (fPartBuffer) fPartBuffer->Delete();
301 if (fTrackBuffer) fTrackBuffer->Delete();
308 }while(fCurrentDir < GetNumberOfDirs());
310 return 1;//no more directories to read
312 /********************************************************************/
314 Int_t AliHBTReaderInternal::OpenNextFile()
316 //open file in current directory
317 const TString& dirname = GetDirName(fCurrentDir);
320 Error("OpenNextFile","Can not get directory name");
324 TString filename = dirname +"/"+ fFileName;
325 fFile = TFile::Open(filename.Data());
328 Error("OpenNextFile","Can't open file named %s",filename.Data());
331 if (fFile->IsOpen() == kFALSE)
333 Error("OpenNextFile","Can't open filenamed %s",filename.Data());
337 fTree = (TTree*)fFile->Get("data");
340 Error("OpenNextFile","Can not get the tree.");
345 fTrackBranch = fTree->GetBranch("tracks");//get the branch with tracks
346 if (fTrackBranch == 0x0) ////check if we got the branch
347 {//if not return with error
348 Info("OpenNextFile","Can't find a branch with tracks !\n");
352 if (fTrackBuffer == 0x0)
353 fTrackBuffer = new TClonesArray("AliHBTParticle",15000);
354 fTrackBranch->SetAddress(&fTrackBuffer);
357 fPartBranch = fTree->GetBranch("particles");//get the branch with particles
358 if (fPartBranch == 0x0) ////check if we got the branch
359 {//if not return with error
360 Info("OpenNextFile","Can't find a branch with particles !\n");
364 if (fPartBuffer == 0x0)
365 fPartBuffer = new TClonesArray("AliHBTParticle",15000);
366 fPartBranch->SetAddress(&fPartBuffer);
369 Info("OpenNextFile","________________________________________________________");
370 Info("OpenNextFile","Found %d event(s) in directory %s",
371 (Int_t)fTree->GetEntries(),GetDirName(fCurrentDir).Data());
377 /********************************************************************/
379 Int_t AliHBTReaderInternal::Write(AliHBTReader* reader,const char* outfile, Bool_t multcheck)
381 //reads tracks from reader and writes runs to file
382 //reader - provides data for writing in internal format
383 //name of output file
384 //multcheck - switches of checking if particle was stored with other incarnation
385 // usefull e.g. when using kine data, where all particles have 100% pid prob.and saves a lot of time
389 ::Info("AliHBTReaderInternal::Write","________________________________________________________");
390 ::Info("AliHBTReaderInternal::Write","________________________________________________________");
391 ::Info("AliHBTReaderInternal::Write","________________________________________________________");
393 TFile *histoOutput = TFile::Open(outfile,"recreate");
395 if (!histoOutput->IsOpen())
397 ::Error("AliHBTReaderInternal::Write","File is not opened");
401 TTree *tracktree = new TTree("data","Tree with tracks");
403 TClonesArray* pbuffer = new TClonesArray("AliHBTParticle",15000);
404 TClonesArray* tbuffer = new TClonesArray("AliHBTParticle",15000);
406 TClonesArray &particles = *pbuffer;
407 TClonesArray &tracks = *tbuffer;
409 TString name("Tracks");
411 Int_t nt = reader->GetNumberOfTrackEvents();
412 Int_t np = reader->GetNumberOfPartEvents();
414 if (AliHBTParticle::GetDebug() > 0)
415 ::Info("Write","Reader has %d track events and %d particles events.",nt,np);
417 Bool_t trck = (nt > 0) ? kTRUE : kFALSE;
418 Bool_t part = (np > 0) ? kTRUE : kFALSE;
420 TBranch *trackbranch = 0x0, *partbranch = 0x0;
422 if (trck) trackbranch = tracktree->Branch("tracks",&tbuffer,32000,0);
423 if (part) partbranch = tracktree->Branch("particles",&pbuffer,32000,0);
425 if ( (trck) && (part) && (np != nt))
427 ::Warning("AliHBTReaderInternal::Write","Number of track and particle events is different");
431 if (nt >= np ) n = nt; else n = np;
433 if (AliHBTParticle::GetDebug() > 0)
434 ::Info("Write","Will loop over %d events",n);
436 for ( i =0;i< n; i++)
438 ::Info("AliHBTReaderInternal::Write","Event %d",i+1);
442 AliHBTEvent* trackev = reader->GetTrackEvent(i);
443 for ( j = 0; j< trackev->GetNumberOfParticles();j++)
445 const AliHBTParticle& t = *(trackev->GetParticle(j));
448 if (FindIndex(tbuffer,t.GetUID()))
450 if (AliHBTParticle::GetDebug()>4)
452 ::Info("Write","Track with Event UID %d already stored",t.GetUID());
454 continue; //not to write the same particles with other incarnations
457 new (tracks[counter++]) AliHBTParticle(t);
459 ::Info("AliHBTReaderInternal::Write"," Tracks: %d",tracks.GetEntries());
460 }else ::Info("AliHBTReaderInternal::Write","NO TRACKS");
465 // ::Warning("AliHBTReaderInternal::Write","Find index switched off!!!");
467 AliHBTEvent* partev = reader->GetParticleEvent(i);
468 for ( j = 0; j< partev->GetNumberOfParticles();j++)
470 const AliHBTParticle& part= *(partev->GetParticle(j));
473 if (FindIndex(pbuffer,part.GetUID()))
475 if (AliHBTParticle::GetDebug()>4)
477 ::Info("Write","Particle with Event UID %d already stored",part.GetUID());
479 continue; //not to write the same particles with other incarnations
482 new (particles[counter++]) AliHBTParticle(part);
484 ::Info("AliHBTReaderInternal::Write"," Particles: %d",particles.GetEntries());
485 }else ::Info("AliHBTReaderInternal::Write","NO PARTICLES");
489 tracktree->AutoSave();
495 tracktree->Write(0,TObject::kOverwrite);
503 histoOutput->Close();
506 /********************************************************************/
508 Bool_t AliHBTReaderInternal::FindIndex(TClonesArray* arr,Int_t idx)
510 //Checks if in the array exists already partilce with Unique ID idx
513 ::Error("FindIndex","Array is 0x0");
518 while (( p = (AliHBTParticle*)next()))
520 if (p->GetUID() == idx) return kTRUE;