]> git.uio.no Git - u/mrichter/AliRoot.git/blame - HBTAN/AliHBTReaderInternal.cxx
Add support for 64 bits
[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),
2efd465d 25 fFile(0x0),
26 fPartBuffer(0x0),
27 fTrackBuffer(0x0)
91923a38 28{
f701e886 29//Defalut constructor
91923a38 30}
31/********************************************************************/
32
bed069a4 33AliHBTReaderInternal::AliHBTReaderInternal(const char *filename):
34 fFileName(filename),
35 fPartBranch(0x0),
36 fTrackBranch(0x0),
37 fTree(0x0),
2efd465d 38 fFile(0x0),
39 fPartBuffer(0x0),
40 fTrackBuffer(0x0)
91923a38 41{
f701e886 42//constructor
43//filename - name of file to open
91923a38 44}
45/********************************************************************/
f701e886 46
91923a38 47AliHBTReaderInternal::AliHBTReaderInternal(TObjArray* dirs, const char *filename):
bed069a4 48 AliHBTReader(dirs),
49 fFileName(filename),
50 fPartBranch(0x0),
51 fTrackBranch(0x0),
52 fTree(0x0),
2efd465d 53 fFile(0x0),
54 fPartBuffer(0x0),
55 fTrackBuffer(0x0)
91923a38 56{
f701e886 57//ctor
58//dirs contains strings with directories to look data in
59//filename - name of file to open
91923a38 60}
f701e886 61/********************************************************************/
62
91923a38 63AliHBTReaderInternal::~AliHBTReaderInternal()
64 {
65 //desctructor
bed069a4 66 delete fTree;
67 delete fFile;
91923a38 68 }
69/********************************************************************/
bed069a4 70
71void AliHBTReaderInternal::Rewind()
72{
73 delete fTree;
74 fTree = 0x0;
75 delete fFile;
76 fFile = 0x0;
77 fCurrentDir = 0;
78 fNEventsRead= 0;
79}
91923a38 80/********************************************************************/
81
bed069a4 82Int_t AliHBTReaderInternal::ReadNext()
91923a38 83 {
84 //reads data and puts put to the particles and tracks objects
85 //reurns 0 if everything is OK
86 //
bed069a4 87 Info("ReadNext","");
91923a38 88 Int_t i; //iterator and some temprary values
f701e886 89 Int_t counter;
9d09c308 90 AliHBTParticle* tpart = 0x0, *ttrack = 0x0;
bed069a4 91
27bb2a54 92 TDatabasePDG* pdgdb = TDatabasePDG::Instance();
93 if (pdgdb == 0x0)
94 {
bed069a4 95 Error("ReadNext","Can not get PDG Particles Data Base");
27bb2a54 96 return 1;
91923a38 97 }
bed069a4 98
99 if (fParticlesEvent == 0x0) fParticlesEvent = new AliHBTEvent();
100 if (fTracksEvent == 0x0) fTracksEvent = new AliHBTEvent();
101
102 fParticlesEvent->Reset();
103 fTracksEvent->Reset();
d4c1d7b6 104
91923a38 105 do //do{}while; is OK even if 0 dirs specified. In that case we try to read from "./"
106 {
bed069a4 107 if (fTree == 0x0)
108 if( (i=OpenNextFile()) )
109 {
4d91c73a 110 Error("ReadNext","Skipping directory due to problems with opening files. Errorcode %d",i);
bed069a4 111 fCurrentDir++;
112 continue;
113 }
114 if (fCurrentEvent == (Int_t)fTree->GetEntries())
91923a38 115 {
bed069a4 116 delete fTree;
117 fTree = 0x0;
118 delete fFile;
119 fFile = 0x0;
120 fPartBranch = 0x0;
121 fTrackBranch= 0x0;
122 fCurrentDir++;
27bb2a54 123 continue;
91923a38 124 }
125 /***************************/
126 /***************************/
127 /***************************/
128
bed069a4 129
130 Info("ReadNext","Event %d",fCurrentEvent);
131 fTree->GetEvent(fCurrentEvent);
91923a38 132
bed069a4 133 counter = 0;
134 if (fPartBranch && fTrackBranch)
4d1d0fe9 135 {
e4cbdf6b 136 Info("ReadNext","Found %d tracks in total.",fTrackBuffer->GetEntries());
137 Info("ReadNext","Found %d particles in total.",fPartBuffer->GetEntries());
bed069a4 138 for(i = 0; i < fPartBuffer->GetEntries(); i++)
139 {
140 tpart = dynamic_cast<AliHBTParticle*>(fPartBuffer->At(i));
141 ttrack = dynamic_cast<AliHBTParticle*>(fTrackBuffer->At(i));
142
143 if( tpart == 0x0 ) continue; //if returned pointer is NULL
4d1d0fe9 144 if( ttrack == 0x0 ) continue; //if returned pointer is NULL
e4cbdf6b 145
146 if (AliHBTParticle::GetDebug() > 9)
147 {
148 Info("ReadNext","Particle:");
149 tpart->Print();
150 Info("ReadNext","Track:");
151 ttrack->Print();
152 }
bed069a4 153 if (ttrack->GetUID() != tpart->GetUID())
9d09c308 154 {
bed069a4 155 Error("ReadNext","Sth. is wrong: Track and Particle has different UID.");
156 Error("ReadNext","They probobly do not correspond to each other.");
157 }
e4cbdf6b 158
4d1d0fe9 159 for (Int_t s = 0; s < ttrack->GetNumberOfPids(); s++)
bed069a4 160 {
e4cbdf6b 161 //check if we are intersted with particles of this type
162 //if not take next partilce
163 if( Pass(ttrack->GetNthPid(s)) )
164 {
165 if (AliHBTParticle::GetDebug() > 9)
166 Info("ReadNext","Track Incarnation %d did not pass PID cut.",ttrack->GetNthPid(s));
167 continue;
168 }
4d1d0fe9 169 TParticlePDG* pdgp = pdgdb->GetParticle(ttrack->GetNthPid(s));
170 if (pdgp == 0x0)//PDG part corresponding to new incarnation
171 {
172 Error("ReadNext","Particle code unknown to PDG DB.");
173 continue;
174 }
7889564f 175
176 AliHBTParticle* track = new AliHBTParticle(*ttrack);
177
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
4d1d0fe9 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));
190
191 if( Pass(track) )
27bb2a54 192 {
e4cbdf6b 193 if (AliHBTParticle::GetDebug() > 9)
194 Info("ReadNext","Track Incarnation %d did not pass cut.",ttrack->GetNthPid(s));
4d1d0fe9 195 delete track;
bed069a4 196 continue;
27bb2a54 197 }
4d1d0fe9 198 AliHBTParticle* part = new AliHBTParticle(*tpart);//particle has only 1 incarnation (real)
bed069a4 199
200 fParticlesEvent->AddParticle(part);
201 fTracksEvent->AddParticle(track);
202
203 counter++;
204 }
205 }
206 Info("ReadNext"," Read: %d particles and tracks.",counter);
207 }
208 else
209 {
210 if (fPartBranch)
211 {
212 Info("ReadNext","Found %d particles in total.",fPartBuffer->GetEntries());
213 for(i = 0; i < fPartBuffer->GetEntries(); i++)
214 {
215 tpart = dynamic_cast<AliHBTParticle*>(fPartBuffer->At(i));
216 if(tpart == 0x0) continue; //if returned pointer is NULL
217
218 for (Int_t s = 0; s < tpart->GetNumberOfPids(); s++)
219 {
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
bed069a4 222 AliHBTParticle* part = new AliHBTParticle(*tpart);
223 part->SetPdgCode(tpart->GetNthPid(s),tpart->GetNthPidProb(s));
224 if( Pass(part) )
27bb2a54 225 {
bed069a4 226 delete part;
227 continue;
27bb2a54 228 }
bed069a4 229 fParticlesEvent->AddParticle(part);
230 counter++;
231 }
232 }
233 Info("ReadNext"," Read: %d particles.",counter);
234 }
235 else Info("ReadNext"," Read: 0 particles.");
236
237 if (fTrackBranch)
238 {
e4cbdf6b 239 Info("ReadNext","Found %d tracks in total.",fTrackBuffer->GetEntries());
bed069a4 240 for(i = 0; i < fTrackBuffer->GetEntries(); i++)
241 {
4d1d0fe9 242 ttrack = dynamic_cast<AliHBTParticle*>(fTrackBuffer->At(i));
243 if( ttrack == 0x0 ) continue; //if returned pointer is NULL
bed069a4 244
4d1d0fe9 245 for (Int_t s = 0; s < ttrack->GetNumberOfPids(); s++)
246 {
4d1d0fe9 247 if( Pass(ttrack->GetNthPid(s)) ) continue; //check if we are intersted with particles of this type
7889564f 248 //if not take next partilce
4d1d0fe9 249 TParticlePDG* pdgp = pdgdb->GetParticle(ttrack->GetNthPid(s));
250 if (pdgp == 0x0)//PDG part corresponding to new incarnation
251 {
252 Error("ReadNext","Particle code unknown to PDG DB.");
253 continue;
254 }
7889564f 255 AliHBTParticle* track = new AliHBTParticle(*ttrack);
256
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
4d1d0fe9 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));
269
270 if( Pass(track) )
27bb2a54 271 {
4d1d0fe9 272 delete track;
bed069a4 273 continue;
27bb2a54 274 }
4d1d0fe9 275 fTracksEvent->AddParticle(track);
276
bed069a4 277 counter++;
278 }
279 }
280 Info("ReadNext"," Read: %d tracks",counter);
281 }
282 else Info("ReadNext"," Read: 0 tracks.");
283 }
d7f24a3d 284
285 if (fPartBuffer) fPartBuffer->Delete();
286 if (fTrackBuffer) fTrackBuffer->Delete();
bed069a4 287
288 fCurrentEvent++;
289 fNEventsRead++;
290
291 return 0;
9d09c308 292
4d91c73a 293 }while(fCurrentDir < GetNumberOfDirs());
91923a38 294
bed069a4 295 return 1;//no more directories to read
91923a38 296}
91923a38 297/********************************************************************/
298
bed069a4 299Int_t AliHBTReaderInternal::OpenNextFile()
91923a38 300{
bed069a4 301 //open file in current directory
4d91c73a 302 const TString& dirname = GetDirName(fCurrentDir);
91923a38 303 if (dirname == "")
304 {
bed069a4 305 Error("OpenNextFile","Can not get directory name");
91923a38 306 return 4;
307 }
308
309 TString filename = dirname +"/"+ fFileName;
bed069a4 310 fFile = TFile::Open(filename.Data());
311 if ( fFile == 0x0 )
91923a38 312 {
bed069a4 313 Error("OpenNextFile","Can't open file named %s",filename.Data());
91923a38 314 return 1;
315 }
bed069a4 316 if (fFile->IsOpen() == kFALSE)
91923a38 317 {
bed069a4 318 Error("OpenNextFile","Can't open filenamed %s",filename.Data());
91923a38 319 return 1;
320 }
bed069a4 321
322 fTree = (TTree*)fFile->Get("data");
323 if (fTree == 0x0)
324 {
325 Error("OpenNextFile","Can not get the tree.");
326 return 1;
327 }
328
329
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");
334 }
335 else
336 {
337 if (fTrackBuffer == 0x0)
338 fTrackBuffer = new TClonesArray("AliHBTParticle",15000);
339 fTrackBranch->SetAddress(&fTrackBuffer);
340 }
341
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");
346 }
347 else
348 {
349 if (fPartBuffer == 0x0)
350 fPartBuffer = new TClonesArray("AliHBTParticle",15000);
351 fPartBranch->SetAddress(&fPartBuffer);
352 }
353
354 Info("OpenNextFile","________________________________________________________");
355 Info("OpenNextFile","Found %d event(s) in directory %s",
18c3af34 356 (Int_t)fTree->GetEntries(),GetDirName(fCurrentDir).Data());
bed069a4 357
358 fCurrentEvent = 0;
359
91923a38 360 return 0;
361}
f701e886 362/********************************************************************/
91923a38 363
27bb2a54 364Int_t AliHBTReaderInternal::Write(AliHBTReader* reader,const char* outfile, Bool_t multcheck)
91923a38 365 {
f701e886 366 //reads tracks from reader and writes runs to file
27bb2a54 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
371
f701e886 372 Int_t i,j;
91923a38 373
f701e886 374 ::Info("AliHBTReaderInternal::Write","________________________________________________________");
375 ::Info("AliHBTReaderInternal::Write","________________________________________________________");
376 ::Info("AliHBTReaderInternal::Write","________________________________________________________");
9d09c308 377
91923a38 378 TFile *histoOutput = TFile::Open(outfile,"recreate");
379
380 if (!histoOutput->IsOpen())
381 {
f701e886 382 ::Error("AliHBTReaderInternal::Write","File is not opened");
91923a38 383 return 1;
384 }
385
91923a38 386 TTree *tracktree = new TTree("data","Tree with tracks");
387
388 TClonesArray* pbuffer = new TClonesArray("AliHBTParticle",15000);
389 TClonesArray* tbuffer = new TClonesArray("AliHBTParticle",15000);
91923a38 390
391 TClonesArray &particles = *pbuffer;
392 TClonesArray &tracks = *tbuffer;
393
394 TString name("Tracks");
91923a38 395
27bb2a54 396 Int_t nt = reader->GetNumberOfTrackEvents();
397 Int_t np = reader->GetNumberOfPartEvents();
91923a38 398
27bb2a54 399 if (AliHBTParticle::GetDebug() > 0)
400 ::Info("Write","Reader has %d track events and %d particles events.",nt,np);
401
402 Bool_t trck = (nt > 0) ? kTRUE : kFALSE;
403 Bool_t part = (np > 0) ? kTRUE : kFALSE;
91923a38 404
405 TBranch *trackbranch = 0x0, *partbranch = 0x0;
406
d4c1d7b6 407 if (trck) trackbranch = tracktree->Branch("tracks",&tbuffer,32000,0);
408 if (part) partbranch = tracktree->Branch("particles",&pbuffer,32000,0);
91923a38 409
27bb2a54 410 if ( (trck) && (part) && (np != nt))
91923a38 411 {
f701e886 412 ::Warning("AliHBTReaderInternal::Write","Number of track and particle events is different");
91923a38 413 }
414
27bb2a54 415 Int_t n;
416 if (nt >= np ) n = nt; else n = np;
417
418 if (AliHBTParticle::GetDebug() > 0)
419 ::Info("Write","Will loop over %d events",n);
91923a38 420
27bb2a54 421 for ( i =0;i< n; i++)
91923a38 422 {
f701e886 423 ::Info("AliHBTReaderInternal::Write","Event %d",i+1);
27bb2a54 424 Int_t counter = 0;
425 if (trck && (i<=nt))
426 {
91923a38 427 AliHBTEvent* trackev = reader->GetTrackEvent(i);
428 for ( j = 0; j< trackev->GetNumberOfParticles();j++)
429 {
27bb2a54 430 const AliHBTParticle& t = *(trackev->GetParticle(j));
431 if (multcheck)
432 {
433 if (FindIndex(tbuffer,t.GetUID()))
434 {
435 if (AliHBTParticle::GetDebug()>4)
436 {
437 ::Info("Write","Track with Event UID %d already stored",t.GetUID());
438 }
439 continue; //not to write the same particles with other incarnations
440 }
441 }
442 new (tracks[counter++]) AliHBTParticle(t);
91923a38 443 }
27bb2a54 444 ::Info("AliHBTReaderInternal::Write"," Tracks: %d",tracks.GetEntries());
f701e886 445 }else ::Info("AliHBTReaderInternal::Write","NO TRACKS");
91923a38 446
27bb2a54 447 counter = 0;
448 if (part && (i<=np))
91923a38 449 {
27bb2a54 450// ::Warning("AliHBTReaderInternal::Write","Find index switched off!!!");
451
91923a38 452 AliHBTEvent* partev = reader->GetParticleEvent(i);
91923a38 453 for ( j = 0; j< partev->GetNumberOfParticles();j++)
454 {
9d09c308 455 const AliHBTParticle& part= *(partev->GetParticle(j));
27bb2a54 456 if (multcheck)
457 {
458 if (FindIndex(pbuffer,part.GetUID()))
459 {
460 if (AliHBTParticle::GetDebug()>4)
461 {
462 ::Info("Write","Particle with Event UID %d already stored",part.GetUID());
463 }
464 continue; //not to write the same particles with other incarnations
465 }
466 }
467 new (particles[counter++]) AliHBTParticle(part);
91923a38 468 }
27bb2a54 469 ::Info("AliHBTReaderInternal::Write"," Particles: %d",particles.GetEntries());
f701e886 470 }else ::Info("AliHBTReaderInternal::Write","NO PARTICLES");
91923a38 471
91923a38 472 histoOutput->cd();
473 tracktree->Fill();
9d09c308 474 tracktree->AutoSave();
91923a38 475 tbuffer->Delete();
476 pbuffer->Delete();
91923a38 477 }
478
91923a38 479 histoOutput->cd();
91923a38 480 tracktree->Write(0,TObject::kOverwrite);
9d09c308 481 delete tracktree;
91923a38 482
9d09c308 483 tbuffer->SetOwner();
484 pbuffer->SetOwner();
485 delete pbuffer;
486 delete tbuffer;
487
488 histoOutput->Close();
91923a38 489 return 0;
490 }
27bb2a54 491/********************************************************************/
492
493Bool_t AliHBTReaderInternal::FindIndex(TClonesArray* arr,Int_t idx)
494{
495//Checks if in the array exists already partilce with Unique ID idx
496 if (arr == 0x0)
497 {
498 ::Error("FindIndex","Array is 0x0");
499 return kTRUE;
500 }
501 TIter next(arr);
502 AliHBTParticle* p;
503 while (( p = (AliHBTParticle*)next()))
504 {
505 if (p->GetUID() == idx) return kTRUE;
506 }
507 return kFALSE;
508}