]> git.uio.no Git - u/mrichter/AliRoot.git/blame - HBTAN/AliHBTReaderInternal.cxx
Bug correction
[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)
4d1d0fe9 129 {
e4cbdf6b 130 Info("ReadNext","Found %d tracks in total.",fTrackBuffer->GetEntries());
131 Info("ReadNext","Found %d particles in total.",fPartBuffer->GetEntries());
bed069a4 132 for(i = 0; i < fPartBuffer->GetEntries(); i++)
133 {
134 tpart = dynamic_cast<AliHBTParticle*>(fPartBuffer->At(i));
135 ttrack = dynamic_cast<AliHBTParticle*>(fTrackBuffer->At(i));
136
137 if( tpart == 0x0 ) continue; //if returned pointer is NULL
4d1d0fe9 138 if( ttrack == 0x0 ) continue; //if returned pointer is NULL
e4cbdf6b 139
140 if (AliHBTParticle::GetDebug() > 9)
141 {
142 Info("ReadNext","Particle:");
143 tpart->Print();
144 Info("ReadNext","Track:");
145 ttrack->Print();
146 }
bed069a4 147 if (ttrack->GetUID() != tpart->GetUID())
9d09c308 148 {
bed069a4 149 Error("ReadNext","Sth. is wrong: Track and Particle has different UID.");
150 Error("ReadNext","They probobly do not correspond to each other.");
151 }
e4cbdf6b 152
4d1d0fe9 153 for (Int_t s = 0; s < ttrack->GetNumberOfPids(); s++)
bed069a4 154 {
e4cbdf6b 155 //check if we are intersted with particles of this type
156 //if not take next partilce
157 if( Pass(ttrack->GetNthPid(s)) )
158 {
159 if (AliHBTParticle::GetDebug() > 9)
160 Info("ReadNext","Track Incarnation %d did not pass PID cut.",ttrack->GetNthPid(s));
161 continue;
162 }
4d1d0fe9 163 TParticlePDG* pdgp = pdgdb->GetParticle(ttrack->GetNthPid(s));
164 if (pdgp == 0x0)//PDG part corresponding to new incarnation
165 {
166 Error("ReadNext","Particle code unknown to PDG DB.");
167 continue;
168 }
7889564f 169
170 AliHBTParticle* track = new AliHBTParticle(*ttrack);
171
172 //apart of setting PDG code of an incarnation
173 //it is necessary tu recalculate energy on the basis of
174 //new PDG code (mass) hypothesis
4d1d0fe9 175 Double_t mass = pdgp->Mass();//mass of new incarnation
176 Double_t tEtot = TMath::Sqrt( ttrack->Px()*ttrack->Px() +
177 ttrack->Py()*ttrack->Py() +
178 ttrack->Pz()*ttrack->Pz() +
179 mass*mass);//total energy of the new incarnation
180 //update Energy and Calc Mass
181 track->SetMomentum(ttrack->Px(),ttrack->Py(),ttrack->Pz(),tEtot);
182 track->SetCalcMass(mass);
183 track->SetPdgCode(ttrack->GetNthPid(s),ttrack->GetNthPidProb(s));
184
185 if( Pass(track) )
27bb2a54 186 {
e4cbdf6b 187 if (AliHBTParticle::GetDebug() > 9)
188 Info("ReadNext","Track Incarnation %d did not pass cut.",ttrack->GetNthPid(s));
4d1d0fe9 189 delete track;
bed069a4 190 continue;
27bb2a54 191 }
4d1d0fe9 192 AliHBTParticle* part = new AliHBTParticle(*tpart);//particle has only 1 incarnation (real)
bed069a4 193
194 fParticlesEvent->AddParticle(part);
195 fTracksEvent->AddParticle(track);
196
197 counter++;
198 }
199 }
200 Info("ReadNext"," Read: %d particles and tracks.",counter);
201 }
202 else
203 {
204 if (fPartBranch)
205 {
206 Info("ReadNext","Found %d particles in total.",fPartBuffer->GetEntries());
207 for(i = 0; i < fPartBuffer->GetEntries(); i++)
208 {
209 tpart = dynamic_cast<AliHBTParticle*>(fPartBuffer->At(i));
210 if(tpart == 0x0) continue; //if returned pointer is NULL
211
212 for (Int_t s = 0; s < tpart->GetNumberOfPids(); s++)
213 {
214 if( pdgdb->GetParticle(tpart->GetNthPid(s)) == 0x0 ) continue; //if particle has crazy PDG code (not known to our database)
215 if( Pass(tpart->GetNthPid(s)) ) continue; //check if we are intersted with particles of this type
bed069a4 216 AliHBTParticle* part = new AliHBTParticle(*tpart);
217 part->SetPdgCode(tpart->GetNthPid(s),tpart->GetNthPidProb(s));
218 if( Pass(part) )
27bb2a54 219 {
bed069a4 220 delete part;
221 continue;
27bb2a54 222 }
bed069a4 223 fParticlesEvent->AddParticle(part);
224 counter++;
225 }
226 }
227 Info("ReadNext"," Read: %d particles.",counter);
228 }
229 else Info("ReadNext"," Read: 0 particles.");
230
231 if (fTrackBranch)
232 {
e4cbdf6b 233 Info("ReadNext","Found %d tracks in total.",fTrackBuffer->GetEntries());
bed069a4 234 for(i = 0; i < fTrackBuffer->GetEntries(); i++)
235 {
4d1d0fe9 236 ttrack = dynamic_cast<AliHBTParticle*>(fTrackBuffer->At(i));
237 if( ttrack == 0x0 ) continue; //if returned pointer is NULL
bed069a4 238
4d1d0fe9 239 for (Int_t s = 0; s < ttrack->GetNumberOfPids(); s++)
240 {
4d1d0fe9 241 if( Pass(ttrack->GetNthPid(s)) ) continue; //check if we are intersted with particles of this type
7889564f 242 //if not take next partilce
4d1d0fe9 243 TParticlePDG* pdgp = pdgdb->GetParticle(ttrack->GetNthPid(s));
244 if (pdgp == 0x0)//PDG part corresponding to new incarnation
245 {
246 Error("ReadNext","Particle code unknown to PDG DB.");
247 continue;
248 }
7889564f 249 AliHBTParticle* track = new AliHBTParticle(*ttrack);
250
251 //apart of setting PDG code of an incarnation
252 //it is necessary tu recalculate energy on the basis of
253 //new PDG code (mass) hypothesis
4d1d0fe9 254 Double_t mass = pdgp->Mass();//mass of new incarnation
255 Double_t tEtot = TMath::Sqrt( ttrack->Px()*ttrack->Px() +
256 ttrack->Py()*ttrack->Py() +
257 ttrack->Pz()*ttrack->Pz() +
258 mass*mass);//total energy of the new incarnation
259 //update Energy and Calc Mass
260 track->SetMomentum(ttrack->Px(),ttrack->Py(),ttrack->Pz(),tEtot);
261 track->SetCalcMass(mass);
262 track->SetPdgCode(ttrack->GetNthPid(s),ttrack->GetNthPidProb(s));
263
264 if( Pass(track) )
27bb2a54 265 {
4d1d0fe9 266 delete track;
bed069a4 267 continue;
27bb2a54 268 }
4d1d0fe9 269 fTracksEvent->AddParticle(track);
270
bed069a4 271 counter++;
272 }
273 }
274 Info("ReadNext"," Read: %d tracks",counter);
275 }
276 else Info("ReadNext"," Read: 0 tracks.");
277 }
d7f24a3d 278
279 if (fPartBuffer) fPartBuffer->Delete();
280 if (fTrackBuffer) fTrackBuffer->Delete();
bed069a4 281
282 fCurrentEvent++;
283 fNEventsRead++;
284
285 return 0;
9d09c308 286
4d91c73a 287 }while(fCurrentDir < GetNumberOfDirs());
91923a38 288
bed069a4 289 return 1;//no more directories to read
91923a38 290}
91923a38 291/********************************************************************/
292
bed069a4 293Int_t AliHBTReaderInternal::OpenNextFile()
91923a38 294{
bed069a4 295 //open file in current directory
4d91c73a 296 const TString& dirname = GetDirName(fCurrentDir);
91923a38 297 if (dirname == "")
298 {
bed069a4 299 Error("OpenNextFile","Can not get directory name");
91923a38 300 return 4;
301 }
302
303 TString filename = dirname +"/"+ fFileName;
bed069a4 304 fFile = TFile::Open(filename.Data());
305 if ( fFile == 0x0 )
91923a38 306 {
bed069a4 307 Error("OpenNextFile","Can't open file named %s",filename.Data());
91923a38 308 return 1;
309 }
bed069a4 310 if (fFile->IsOpen() == kFALSE)
91923a38 311 {
bed069a4 312 Error("OpenNextFile","Can't open filenamed %s",filename.Data());
91923a38 313 return 1;
314 }
bed069a4 315
316 fTree = (TTree*)fFile->Get("data");
317 if (fTree == 0x0)
318 {
319 Error("OpenNextFile","Can not get the tree.");
320 return 1;
321 }
322
323
324 fTrackBranch = fTree->GetBranch("tracks");//get the branch with tracks
325 if (fTrackBranch == 0x0) ////check if we got the branch
326 {//if not return with error
327 Info("OpenNextFile","Can't find a branch with tracks !\n");
328 }
329 else
330 {
331 if (fTrackBuffer == 0x0)
332 fTrackBuffer = new TClonesArray("AliHBTParticle",15000);
333 fTrackBranch->SetAddress(&fTrackBuffer);
334 }
335
336 fPartBranch = fTree->GetBranch("particles");//get the branch with particles
337 if (fPartBranch == 0x0) ////check if we got the branch
338 {//if not return with error
339 Info("OpenNextFile","Can't find a branch with particles !\n");
340 }
341 else
342 {
343 if (fPartBuffer == 0x0)
344 fPartBuffer = new TClonesArray("AliHBTParticle",15000);
345 fPartBranch->SetAddress(&fPartBuffer);
346 }
347
348 Info("OpenNextFile","________________________________________________________");
349 Info("OpenNextFile","Found %d event(s) in directory %s",
18c3af34 350 (Int_t)fTree->GetEntries(),GetDirName(fCurrentDir).Data());
bed069a4 351
352 fCurrentEvent = 0;
353
91923a38 354 return 0;
355}
f701e886 356/********************************************************************/
91923a38 357
27bb2a54 358Int_t AliHBTReaderInternal::Write(AliHBTReader* reader,const char* outfile, Bool_t multcheck)
91923a38 359 {
f701e886 360 //reads tracks from reader and writes runs to file
27bb2a54 361 //reader - provides data for writing in internal format
362 //name of output file
363 //multcheck - switches of checking if particle was stored with other incarnation
364 // usefull e.g. when using kine data, where all particles have 100% pid prob.and saves a lot of time
365
f701e886 366 Int_t i,j;
91923a38 367
f701e886 368 ::Info("AliHBTReaderInternal::Write","________________________________________________________");
369 ::Info("AliHBTReaderInternal::Write","________________________________________________________");
370 ::Info("AliHBTReaderInternal::Write","________________________________________________________");
9d09c308 371
91923a38 372 TFile *histoOutput = TFile::Open(outfile,"recreate");
373
374 if (!histoOutput->IsOpen())
375 {
f701e886 376 ::Error("AliHBTReaderInternal::Write","File is not opened");
91923a38 377 return 1;
378 }
379
91923a38 380 TTree *tracktree = new TTree("data","Tree with tracks");
381
382 TClonesArray* pbuffer = new TClonesArray("AliHBTParticle",15000);
383 TClonesArray* tbuffer = new TClonesArray("AliHBTParticle",15000);
91923a38 384
385 TClonesArray &particles = *pbuffer;
386 TClonesArray &tracks = *tbuffer;
387
388 TString name("Tracks");
91923a38 389
27bb2a54 390 Int_t nt = reader->GetNumberOfTrackEvents();
391 Int_t np = reader->GetNumberOfPartEvents();
91923a38 392
27bb2a54 393 if (AliHBTParticle::GetDebug() > 0)
394 ::Info("Write","Reader has %d track events and %d particles events.",nt,np);
395
396 Bool_t trck = (nt > 0) ? kTRUE : kFALSE;
397 Bool_t part = (np > 0) ? kTRUE : kFALSE;
91923a38 398
399 TBranch *trackbranch = 0x0, *partbranch = 0x0;
400
d4c1d7b6 401 if (trck) trackbranch = tracktree->Branch("tracks",&tbuffer,32000,0);
402 if (part) partbranch = tracktree->Branch("particles",&pbuffer,32000,0);
91923a38 403
27bb2a54 404 if ( (trck) && (part) && (np != nt))
91923a38 405 {
f701e886 406 ::Warning("AliHBTReaderInternal::Write","Number of track and particle events is different");
91923a38 407 }
408
27bb2a54 409 Int_t n;
410 if (nt >= np ) n = nt; else n = np;
411
412 if (AliHBTParticle::GetDebug() > 0)
413 ::Info("Write","Will loop over %d events",n);
91923a38 414
27bb2a54 415 for ( i =0;i< n; i++)
91923a38 416 {
f701e886 417 ::Info("AliHBTReaderInternal::Write","Event %d",i+1);
27bb2a54 418 Int_t counter = 0;
419 if (trck && (i<=nt))
420 {
91923a38 421 AliHBTEvent* trackev = reader->GetTrackEvent(i);
422 for ( j = 0; j< trackev->GetNumberOfParticles();j++)
423 {
27bb2a54 424 const AliHBTParticle& t = *(trackev->GetParticle(j));
425 if (multcheck)
426 {
427 if (FindIndex(tbuffer,t.GetUID()))
428 {
429 if (AliHBTParticle::GetDebug()>4)
430 {
431 ::Info("Write","Track with Event UID %d already stored",t.GetUID());
432 }
433 continue; //not to write the same particles with other incarnations
434 }
435 }
436 new (tracks[counter++]) AliHBTParticle(t);
91923a38 437 }
27bb2a54 438 ::Info("AliHBTReaderInternal::Write"," Tracks: %d",tracks.GetEntries());
f701e886 439 }else ::Info("AliHBTReaderInternal::Write","NO TRACKS");
91923a38 440
27bb2a54 441 counter = 0;
442 if (part && (i<=np))
91923a38 443 {
27bb2a54 444// ::Warning("AliHBTReaderInternal::Write","Find index switched off!!!");
445
91923a38 446 AliHBTEvent* partev = reader->GetParticleEvent(i);
91923a38 447 for ( j = 0; j< partev->GetNumberOfParticles();j++)
448 {
9d09c308 449 const AliHBTParticle& part= *(partev->GetParticle(j));
27bb2a54 450 if (multcheck)
451 {
452 if (FindIndex(pbuffer,part.GetUID()))
453 {
454 if (AliHBTParticle::GetDebug()>4)
455 {
456 ::Info("Write","Particle with Event UID %d already stored",part.GetUID());
457 }
458 continue; //not to write the same particles with other incarnations
459 }
460 }
461 new (particles[counter++]) AliHBTParticle(part);
91923a38 462 }
27bb2a54 463 ::Info("AliHBTReaderInternal::Write"," Particles: %d",particles.GetEntries());
f701e886 464 }else ::Info("AliHBTReaderInternal::Write","NO PARTICLES");
91923a38 465
91923a38 466 histoOutput->cd();
467 tracktree->Fill();
9d09c308 468 tracktree->AutoSave();
91923a38 469 tbuffer->Delete();
470 pbuffer->Delete();
91923a38 471 }
472
91923a38 473 histoOutput->cd();
91923a38 474 tracktree->Write(0,TObject::kOverwrite);
9d09c308 475 delete tracktree;
91923a38 476
9d09c308 477 tbuffer->SetOwner();
478 pbuffer->SetOwner();
479 delete pbuffer;
480 delete tbuffer;
481
482 histoOutput->Close();
91923a38 483 return 0;
484 }
27bb2a54 485/********************************************************************/
486
487Bool_t AliHBTReaderInternal::FindIndex(TClonesArray* arr,Int_t idx)
488{
489//Checks if in the array exists already partilce with Unique ID idx
490 if (arr == 0x0)
491 {
492 ::Error("FindIndex","Array is 0x0");
493 return kTRUE;
494 }
495 TIter next(arr);
496 AliHBTParticle* p;
497 while (( p = (AliHBTParticle*)next()))
498 {
499 if (p->GetUID() == idx) return kTRUE;
500 }
501 return kFALSE;
502}