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( Pass(ttrack->GetNthPid(s)) ) continue; //check if we are intersted with particles of this type
147 //if not take next partilce
148 TParticlePDG* pdgp = pdgdb->GetParticle(ttrack->GetNthPid(s));
149 if (pdgp == 0x0)//PDG part corresponding to new incarnation
151 Error("ReadNext","Particle code unknown to PDG DB.");
155 AliHBTParticle* track = new AliHBTParticle(*ttrack);
157 //apart of setting PDG code of an incarnation
158 //it is necessary tu recalculate energy on the basis of
159 //new PDG code (mass) hypothesis
160 Double_t mass = pdgp->Mass();//mass of new incarnation
161 Double_t tEtot = TMath::Sqrt( ttrack->Px()*ttrack->Px() +
162 ttrack->Py()*ttrack->Py() +
163 ttrack->Pz()*ttrack->Pz() +
164 mass*mass);//total energy of the new incarnation
165 //update Energy and Calc Mass
166 track->SetMomentum(ttrack->Px(),ttrack->Py(),ttrack->Pz(),tEtot);
167 track->SetCalcMass(mass);
168 track->SetPdgCode(ttrack->GetNthPid(s),ttrack->GetNthPidProb(s));
175 AliHBTParticle* part = new AliHBTParticle(*tpart);//particle has only 1 incarnation (real)
177 fParticlesEvent->AddParticle(part);
178 fTracksEvent->AddParticle(track);
183 Info("ReadNext"," Read: %d particles and tracks.",counter);
189 Info("ReadNext","Found %d particles in total.",fPartBuffer->GetEntries());
190 for(i = 0; i < fPartBuffer->GetEntries(); i++)
192 tpart = dynamic_cast<AliHBTParticle*>(fPartBuffer->At(i));
193 if(tpart == 0x0) continue; //if returned pointer is NULL
195 for (Int_t s = 0; s < tpart->GetNumberOfPids(); s++)
197 if( pdgdb->GetParticle(tpart->GetNthPid(s)) == 0x0 ) continue; //if particle has crazy PDG code (not known to our database)
198 if( Pass(tpart->GetNthPid(s)) ) continue; //check if we are intersted with particles of this type
199 AliHBTParticle* part = new AliHBTParticle(*tpart);
200 part->SetPdgCode(tpart->GetNthPid(s),tpart->GetNthPidProb(s));
206 fParticlesEvent->AddParticle(part);
210 Info("ReadNext"," Read: %d particles.",counter);
212 else Info("ReadNext"," Read: 0 particles.");
216 for(i = 0; i < fTrackBuffer->GetEntries(); i++)
218 ttrack = dynamic_cast<AliHBTParticle*>(fTrackBuffer->At(i));
219 if( ttrack == 0x0 ) continue; //if returned pointer is NULL
221 for (Int_t s = 0; s < ttrack->GetNumberOfPids(); s++)
223 if( Pass(ttrack->GetNthPid(s)) ) continue; //check if we are intersted with particles of this type
224 //if not take next partilce
225 TParticlePDG* pdgp = pdgdb->GetParticle(ttrack->GetNthPid(s));
226 if (pdgp == 0x0)//PDG part corresponding to new incarnation
228 Error("ReadNext","Particle code unknown to PDG DB.");
231 AliHBTParticle* track = new AliHBTParticle(*ttrack);
233 //apart of setting PDG code of an incarnation
234 //it is necessary tu recalculate energy on the basis of
235 //new PDG code (mass) hypothesis
236 Double_t mass = pdgp->Mass();//mass of new incarnation
237 Double_t tEtot = TMath::Sqrt( ttrack->Px()*ttrack->Px() +
238 ttrack->Py()*ttrack->Py() +
239 ttrack->Pz()*ttrack->Pz() +
240 mass*mass);//total energy of the new incarnation
241 //update Energy and Calc Mass
242 track->SetMomentum(ttrack->Px(),ttrack->Py(),ttrack->Pz(),tEtot);
243 track->SetCalcMass(mass);
244 track->SetPdgCode(ttrack->GetNthPid(s),ttrack->GetNthPidProb(s));
251 fTracksEvent->AddParticle(track);
256 Info("ReadNext"," Read: %d tracks",counter);
258 else Info("ReadNext"," Read: 0 tracks.");
261 if (fPartBuffer) fPartBuffer->Delete();
262 if (fTrackBuffer) fTrackBuffer->Delete();
269 }while(fCurrentDir < GetNumberOfDirs());
271 return 1;//no more directories to read
273 /********************************************************************/
275 Int_t AliHBTReaderInternal::OpenNextFile()
277 //open file in current directory
278 const TString& dirname = GetDirName(fCurrentDir);
281 Error("OpenNextFile","Can not get directory name");
285 TString filename = dirname +"/"+ fFileName;
286 fFile = TFile::Open(filename.Data());
289 Error("OpenNextFile","Can't open file named %s",filename.Data());
292 if (fFile->IsOpen() == kFALSE)
294 Error("OpenNextFile","Can't open filenamed %s",filename.Data());
298 fTree = (TTree*)fFile->Get("data");
301 Error("OpenNextFile","Can not get the tree.");
306 fTrackBranch = fTree->GetBranch("tracks");//get the branch with tracks
307 if (fTrackBranch == 0x0) ////check if we got the branch
308 {//if not return with error
309 Info("OpenNextFile","Can't find a branch with tracks !\n");
313 if (fTrackBuffer == 0x0)
314 fTrackBuffer = new TClonesArray("AliHBTParticle",15000);
315 fTrackBranch->SetAddress(&fTrackBuffer);
318 fPartBranch = fTree->GetBranch("particles");//get the branch with particles
319 if (fPartBranch == 0x0) ////check if we got the branch
320 {//if not return with error
321 Info("OpenNextFile","Can't find a branch with particles !\n");
325 if (fPartBuffer == 0x0)
326 fPartBuffer = new TClonesArray("AliHBTParticle",15000);
327 fPartBranch->SetAddress(&fPartBuffer);
330 Info("OpenNextFile","________________________________________________________");
331 Info("OpenNextFile","Found %d event(s) in directory %s",
332 (Int_t)fTree->GetEntries(),GetDirName(fCurrentEvent).Data());
338 /********************************************************************/
340 Int_t AliHBTReaderInternal::Write(AliHBTReader* reader,const char* outfile, Bool_t multcheck)
342 //reads tracks from reader and writes runs to file
343 //reader - provides data for writing in internal format
344 //name of output file
345 //multcheck - switches of checking if particle was stored with other incarnation
346 // usefull e.g. when using kine data, where all particles have 100% pid prob.and saves a lot of time
350 ::Info("AliHBTReaderInternal::Write","________________________________________________________");
351 ::Info("AliHBTReaderInternal::Write","________________________________________________________");
352 ::Info("AliHBTReaderInternal::Write","________________________________________________________");
354 TFile *histoOutput = TFile::Open(outfile,"recreate");
356 if (!histoOutput->IsOpen())
358 ::Error("AliHBTReaderInternal::Write","File is not opened");
362 TTree *tracktree = new TTree("data","Tree with tracks");
364 TClonesArray* pbuffer = new TClonesArray("AliHBTParticle",15000);
365 TClonesArray* tbuffer = new TClonesArray("AliHBTParticle",15000);
367 TClonesArray &particles = *pbuffer;
368 TClonesArray &tracks = *tbuffer;
370 TString name("Tracks");
372 Int_t nt = reader->GetNumberOfTrackEvents();
373 Int_t np = reader->GetNumberOfPartEvents();
375 if (AliHBTParticle::GetDebug() > 0)
376 ::Info("Write","Reader has %d track events and %d particles events.",nt,np);
378 Bool_t trck = (nt > 0) ? kTRUE : kFALSE;
379 Bool_t part = (np > 0) ? kTRUE : kFALSE;
381 TBranch *trackbranch = 0x0, *partbranch = 0x0;
383 if (trck) trackbranch = tracktree->Branch("tracks",&tbuffer,32000,0);
384 if (part) partbranch = tracktree->Branch("particles",&pbuffer,32000,0);
386 if ( (trck) && (part) && (np != nt))
388 ::Warning("AliHBTReaderInternal::Write","Number of track and particle events is different");
392 if (nt >= np ) n = nt; else n = np;
394 if (AliHBTParticle::GetDebug() > 0)
395 ::Info("Write","Will loop over %d events",n);
397 for ( i =0;i< n; i++)
399 ::Info("AliHBTReaderInternal::Write","Event %d",i+1);
403 AliHBTEvent* trackev = reader->GetTrackEvent(i);
404 for ( j = 0; j< trackev->GetNumberOfParticles();j++)
406 const AliHBTParticle& t = *(trackev->GetParticle(j));
409 if (FindIndex(tbuffer,t.GetUID()))
411 if (AliHBTParticle::GetDebug()>4)
413 ::Info("Write","Track with Event UID %d already stored",t.GetUID());
415 continue; //not to write the same particles with other incarnations
418 new (tracks[counter++]) AliHBTParticle(t);
420 ::Info("AliHBTReaderInternal::Write"," Tracks: %d",tracks.GetEntries());
421 }else ::Info("AliHBTReaderInternal::Write","NO TRACKS");
426 // ::Warning("AliHBTReaderInternal::Write","Find index switched off!!!");
428 AliHBTEvent* partev = reader->GetParticleEvent(i);
429 for ( j = 0; j< partev->GetNumberOfParticles();j++)
431 const AliHBTParticle& part= *(partev->GetParticle(j));
434 if (FindIndex(pbuffer,part.GetUID()))
436 if (AliHBTParticle::GetDebug()>4)
438 ::Info("Write","Particle with Event UID %d already stored",part.GetUID());
440 continue; //not to write the same particles with other incarnations
443 new (particles[counter++]) AliHBTParticle(part);
445 ::Info("AliHBTReaderInternal::Write"," Particles: %d",particles.GetEntries());
446 }else ::Info("AliHBTReaderInternal::Write","NO PARTICLES");
450 tracktree->AutoSave();
456 tracktree->Write(0,TObject::kOverwrite);
464 histoOutput->Close();
467 /********************************************************************/
469 Bool_t AliHBTReaderInternal::FindIndex(TClonesArray* arr,Int_t idx)
471 //Checks if in the array exists already partilce with Unique ID idx
474 ::Error("FindIndex","Array is 0x0");
479 while (( p = (AliHBTParticle*)next()))
481 if (p->GetUID() == idx) return kTRUE;