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():
29 /********************************************************************/
31 AliHBTReaderInternal::AliHBTReaderInternal(const char *filename):
39 //filename - name of file to open
41 /********************************************************************/
43 AliHBTReaderInternal::AliHBTReaderInternal(TObjArray* dirs, const char *filename):
52 //dirs contains strings with directories to look data in
53 //filename - name of file to open
55 /********************************************************************/
57 AliHBTReaderInternal::~AliHBTReaderInternal()
63 /********************************************************************/
65 void AliHBTReaderInternal::Rewind()
74 /********************************************************************/
76 Int_t AliHBTReaderInternal::ReadNext()
78 //reads data and puts put to the particles and tracks objects
79 //reurns 0 if everything is OK
82 Int_t i; //iterator and some temprary values
84 AliHBTParticle* tpart = 0x0, *ttrack = 0x0;
86 TDatabasePDG* pdgdb = TDatabasePDG::Instance();
89 Error("ReadNext","Can not get PDG Particles Data Base");
93 if (fParticlesEvent == 0x0) fParticlesEvent = new AliHBTEvent();
94 if (fTracksEvent == 0x0) fTracksEvent = new AliHBTEvent();
96 fParticlesEvent->Reset();
97 fTracksEvent->Reset();
99 do //do{}while; is OK even if 0 dirs specified. In that case we try to read from "./"
102 if( (i=OpenNextFile()) )
104 Error("ReadNext","Skipping directory due to problems with opening files. Errorcode %d",i);
108 if (fCurrentEvent == (Int_t)fTree->GetEntries())
119 /***************************/
120 /***************************/
121 /***************************/
124 Info("ReadNext","Event %d",fCurrentEvent);
125 fTree->GetEvent(fCurrentEvent);
128 if (fPartBranch && fTrackBranch)
130 for(i = 0; i < fPartBuffer->GetEntries(); i++)
132 tpart = dynamic_cast<AliHBTParticle*>(fPartBuffer->At(i));
133 ttrack = dynamic_cast<AliHBTParticle*>(fTrackBuffer->At(i));
135 if( tpart == 0x0 ) continue; //if returned pointer is NULL
136 if( ttrack == 0x0 ) continue; //if returned pointer is NULL
138 if (ttrack->GetUID() != tpart->GetUID())
140 Error("ReadNext","Sth. is wrong: Track and Particle has different UID.");
141 Error("ReadNext","They probobly do not correspond to each other.");
144 for (Int_t s = 0; s < ttrack->GetNumberOfPids(); s++)
146 if( pdgdb->GetParticle(ttrack->GetNthPid(s)) == 0x0 ) continue; //if particle has crazy PDG code (not known to our database)
147 if( Pass(ttrack->GetNthPid(s)) ) continue; //check if we are intersted with particles of this type
148 //if not take next partilce
150 AliHBTParticle* track = new AliHBTParticle(*ttrack);
152 //apart of setting PDG code of an incarnation
153 //it is necessary tu recalculate energy on the basis of
154 //new PDG code (mass) hypothesis
155 TParticlePDG* pdgp = pdgdb->GetParticle(ttrack->GetNthPid(s));
156 if (pdgp == 0x0)//PDG part corresponding to new incarnation
158 Error("ReadNext","Particle code unknown to PDG DB.");
161 Double_t mass = pdgp->Mass();//mass of new incarnation
162 Double_t tEtot = TMath::Sqrt( ttrack->Px()*ttrack->Px() +
163 ttrack->Py()*ttrack->Py() +
164 ttrack->Pz()*ttrack->Pz() +
165 mass*mass);//total energy of the new incarnation
166 //update Energy and Calc Mass
167 track->SetMomentum(ttrack->Px(),ttrack->Py(),ttrack->Pz(),tEtot);
168 track->SetCalcMass(mass);
169 track->SetPdgCode(ttrack->GetNthPid(s),ttrack->GetNthPidProb(s));
176 AliHBTParticle* part = new AliHBTParticle(*tpart);//particle has only 1 incarnation (real)
178 fParticlesEvent->AddParticle(part);
179 fTracksEvent->AddParticle(track);
184 Info("ReadNext"," Read: %d particles and tracks.",counter);
190 Info("ReadNext","Found %d particles in total.",fPartBuffer->GetEntries());
191 for(i = 0; i < fPartBuffer->GetEntries(); i++)
193 tpart = dynamic_cast<AliHBTParticle*>(fPartBuffer->At(i));
194 if(tpart == 0x0) continue; //if returned pointer is NULL
196 for (Int_t s = 0; s < tpart->GetNumberOfPids(); s++)
198 if( pdgdb->GetParticle(tpart->GetNthPid(s)) == 0x0 ) continue; //if particle has crazy PDG code (not known to our database)
199 if( Pass(tpart->GetNthPid(s)) ) continue; //check if we are intersted with particles of this type
200 AliHBTParticle* part = new AliHBTParticle(*tpart);
201 part->SetPdgCode(tpart->GetNthPid(s),tpart->GetNthPidProb(s));
207 fParticlesEvent->AddParticle(part);
211 Info("ReadNext"," Read: %d particles.",counter);
213 else Info("ReadNext"," Read: 0 particles.");
217 for(i = 0; i < fTrackBuffer->GetEntries(); i++)
219 ttrack = dynamic_cast<AliHBTParticle*>(fTrackBuffer->At(i));
220 if( ttrack == 0x0 ) continue; //if returned pointer is NULL
222 for (Int_t s = 0; s < ttrack->GetNumberOfPids(); s++)
224 if( pdgdb->GetParticle(ttrack->GetNthPid(s)) == 0x0 ) continue; //if particle has crazy PDG code (not known to our database)
225 if( Pass(ttrack->GetNthPid(s)) ) continue; //check if we are intersted with particles of this type
226 //if not take next partilce
227 AliHBTParticle* track = new AliHBTParticle(*ttrack);
229 //apart of setting PDG code of an incarnation
230 //it is necessary tu recalculate energy on the basis of
231 //new PDG code (mass) hypothesis
232 TParticlePDG* pdgp = pdgdb->GetParticle(ttrack->GetNthPid(s));
233 if (pdgp == 0x0)//PDG part corresponding to new incarnation
235 Error("ReadNext","Particle code unknown to PDG DB.");
238 Double_t mass = pdgp->Mass();//mass of new incarnation
239 Double_t tEtot = TMath::Sqrt( ttrack->Px()*ttrack->Px() +
240 ttrack->Py()*ttrack->Py() +
241 ttrack->Pz()*ttrack->Pz() +
242 mass*mass);//total energy of the new incarnation
243 //update Energy and Calc Mass
244 track->SetMomentum(ttrack->Px(),ttrack->Py(),ttrack->Pz(),tEtot);
245 track->SetCalcMass(mass);
246 track->SetPdgCode(ttrack->GetNthPid(s),ttrack->GetNthPidProb(s));
253 fTracksEvent->AddParticle(track);
258 Info("ReadNext"," Read: %d tracks",counter);
260 else Info("ReadNext"," Read: 0 tracks.");
263 if (fPartBuffer) fPartBuffer->Delete();
264 if (fTrackBuffer) fTrackBuffer->Delete();
271 }while(fCurrentDir < GetNumberOfDirs());
273 return 1;//no more directories to read
275 /********************************************************************/
277 Int_t AliHBTReaderInternal::OpenNextFile()
279 //open file in current directory
280 const TString& dirname = GetDirName(fCurrentDir);
283 Error("OpenNextFile","Can not get directory name");
287 TString filename = dirname +"/"+ fFileName;
288 fFile = TFile::Open(filename.Data());
291 Error("OpenNextFile","Can't open file named %s",filename.Data());
294 if (fFile->IsOpen() == kFALSE)
296 Error("OpenNextFile","Can't open filenamed %s",filename.Data());
300 fTree = (TTree*)fFile->Get("data");
303 Error("OpenNextFile","Can not get the tree.");
308 fTrackBranch = fTree->GetBranch("tracks");//get the branch with tracks
309 if (fTrackBranch == 0x0) ////check if we got the branch
310 {//if not return with error
311 Info("OpenNextFile","Can't find a branch with tracks !\n");
315 if (fTrackBuffer == 0x0)
316 fTrackBuffer = new TClonesArray("AliHBTParticle",15000);
317 fTrackBranch->SetAddress(&fTrackBuffer);
320 fPartBranch = fTree->GetBranch("particles");//get the branch with particles
321 if (fPartBranch == 0x0) ////check if we got the branch
322 {//if not return with error
323 Info("OpenNextFile","Can't find a branch with particles !\n");
327 if (fPartBuffer == 0x0)
328 fPartBuffer = new TClonesArray("AliHBTParticle",15000);
329 fPartBranch->SetAddress(&fPartBuffer);
332 Info("OpenNextFile","________________________________________________________");
333 Info("OpenNextFile","Found %d event(s) in directory %s",
334 (Int_t)fTree->GetEntries(),GetDirName(fCurrentEvent).Data());
340 /********************************************************************/
342 Int_t AliHBTReaderInternal::Write(AliHBTReader* reader,const char* outfile, Bool_t multcheck)
344 //reads tracks from reader and writes runs to file
345 //reader - provides data for writing in internal format
346 //name of output file
347 //multcheck - switches of checking if particle was stored with other incarnation
348 // usefull e.g. when using kine data, where all particles have 100% pid prob.and saves a lot of time
352 ::Info("AliHBTReaderInternal::Write","________________________________________________________");
353 ::Info("AliHBTReaderInternal::Write","________________________________________________________");
354 ::Info("AliHBTReaderInternal::Write","________________________________________________________");
356 TFile *histoOutput = TFile::Open(outfile,"recreate");
358 if (!histoOutput->IsOpen())
360 ::Error("AliHBTReaderInternal::Write","File is not opened");
364 TTree *tracktree = new TTree("data","Tree with tracks");
366 TClonesArray* pbuffer = new TClonesArray("AliHBTParticle",15000);
367 TClonesArray* tbuffer = new TClonesArray("AliHBTParticle",15000);
369 TClonesArray &particles = *pbuffer;
370 TClonesArray &tracks = *tbuffer;
372 TString name("Tracks");
374 Int_t nt = reader->GetNumberOfTrackEvents();
375 Int_t np = reader->GetNumberOfPartEvents();
377 if (AliHBTParticle::GetDebug() > 0)
378 ::Info("Write","Reader has %d track events and %d particles events.",nt,np);
380 Bool_t trck = (nt > 0) ? kTRUE : kFALSE;
381 Bool_t part = (np > 0) ? kTRUE : kFALSE;
383 TBranch *trackbranch = 0x0, *partbranch = 0x0;
385 if (trck) trackbranch = tracktree->Branch("tracks",&tbuffer,32000,0);
386 if (part) partbranch = tracktree->Branch("particles",&pbuffer,32000,0);
388 if ( (trck) && (part) && (np != nt))
390 ::Warning("AliHBTReaderInternal::Write","Number of track and particle events is different");
394 if (nt >= np ) n = nt; else n = np;
396 if (AliHBTParticle::GetDebug() > 0)
397 ::Info("Write","Will loop over %d events",n);
399 for ( i =0;i< n; i++)
401 ::Info("AliHBTReaderInternal::Write","Event %d",i+1);
405 AliHBTEvent* trackev = reader->GetTrackEvent(i);
406 for ( j = 0; j< trackev->GetNumberOfParticles();j++)
408 const AliHBTParticle& t = *(trackev->GetParticle(j));
411 if (FindIndex(tbuffer,t.GetUID()))
413 if (AliHBTParticle::GetDebug()>4)
415 ::Info("Write","Track with Event UID %d already stored",t.GetUID());
417 continue; //not to write the same particles with other incarnations
420 new (tracks[counter++]) AliHBTParticle(t);
422 ::Info("AliHBTReaderInternal::Write"," Tracks: %d",tracks.GetEntries());
423 }else ::Info("AliHBTReaderInternal::Write","NO TRACKS");
428 // ::Warning("AliHBTReaderInternal::Write","Find index switched off!!!");
430 AliHBTEvent* partev = reader->GetParticleEvent(i);
431 for ( j = 0; j< partev->GetNumberOfParticles();j++)
433 const AliHBTParticle& part= *(partev->GetParticle(j));
436 if (FindIndex(pbuffer,part.GetUID()))
438 if (AliHBTParticle::GetDebug()>4)
440 ::Info("Write","Particle with Event UID %d already stored",part.GetUID());
442 continue; //not to write the same particles with other incarnations
445 new (particles[counter++]) AliHBTParticle(part);
447 ::Info("AliHBTReaderInternal::Write"," Particles: %d",particles.GetEntries());
448 }else ::Info("AliHBTReaderInternal::Write","NO PARTICLES");
452 tracktree->AutoSave();
458 tracktree->Write(0,TObject::kOverwrite);
466 histoOutput->Close();
469 /********************************************************************/
471 Bool_t AliHBTReaderInternal::FindIndex(TClonesArray* arr,Int_t idx)
473 //Checks if in the array exists already partilce with Unique ID idx
476 ::Error("FindIndex","Array is 0x0");
481 while (( p = (AliHBTParticle*)next()))
483 if (p->GetUID() == idx) return kTRUE;