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