1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercial purposes is hereby granted *
9 * without fee, provided that the above copyright notice appears in all *
10 * copies and that both the copyright notice and this permission notice *
11 * appear in the supporting documentation. The authors make no claims *
12 * about the suitability of this software for any purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
17 ///////////////////////////////////////////////////////////////////////////
20 Origin: marian.ivanov@cern.ch
22 Generate complex MC information - used for Comparison later on
26 ---Usage outside of the analysis framework
28 gSystem->Load("libANALYSIS.so")
29 gSystem->Load("libPWG1.so")
30 AliGenInfoMaker *t = new AliGenInfoMaker("galice.root","genTracks.root",0,0)
35 #if !defined(__CINT__) || defined(__MAKECINT__)
43 #include "TStopwatch.h"
44 #include "TParticle.h"
47 #include "AliMCEvent.h"
48 #include "AliMCEventHandler.h"
52 #include "AliSimDigits.h"
53 #include "AliTPCParam.h"
55 #include "AliTPCLoader.h"
56 #include "AliTrackReference.h"
57 #include "AliTPCParamSR.h"
58 #include "AliTrackPointArray.h"
61 #include "AliMCInfo.h"
62 #include "AliGenV0Info.h"
63 #include "AliGenKinkInfo.h"
64 #include "AliGenInfoMaker.h"
68 ClassImp(AliGenInfoMaker)
76 ////////////////////////////////////////////////////////////////////////
77 AliGenInfoMaker::AliGenInfoMaker():
78 fGenTracksArray(0), //clones array with filtered particles
79 fGenKinkArray(0), //clones array with filtered Kinks
80 fGenV0Array(0), //clones array with filtered V0s
81 fDebug(0), //! debug flag
82 fEventNr(0), //! current event number
83 fLabel(0), //! track label
84 fNEvents(0), //! number of events to process
85 fFirstEventNr(0), //! first event to process
86 fNParticles(0), //! number of particles in TreeK
87 fTreeGenTracks(0), //! output tree with generated tracks
88 fTreeKinks(0), //! output tree with Kinks
89 fTreeV0(0), //! output tree with V0
90 fFileGenTracks(0), //! output file with stored fTreeGenTracks
91 fLoader(0), //! pointer to the run loader
92 fTreeD(0), //! current tree with digits
93 fTreeTR(0), //! current tree with TR
94 fStack(0), //! current stack
95 fGenInfo(0), //! array with pointers to gen info
96 fNInfos(0), //! number of tracks with infos
97 fParamTPC(0), //! AliTPCParam
98 fTPCPtCut(0.1), // TPC pt cut
99 fITSPtCut(0.1), // ITS pt cut
100 fTRDPtCut(0.1), // TRD pt cut
101 fTOFPtCut(0.1) // TOF pt cut
103 sprintf(fFnRes,"%s","genTracks.root");
109 ////////////////////////////////////////////////////////////////////////
110 AliGenInfoMaker::AliGenInfoMaker(const char * fnGalice, const char* fnRes,
111 Int_t nEvents, Int_t firstEvent):
112 fGenTracksArray(0), //clones array with filtered particles
113 fGenKinkArray(0), //clones array with filtered Kinks
114 fGenV0Array(0), //clones array with filtered V0s
115 fDebug(0), //! debug flag
116 fEventNr(0), //! current event number
117 fLabel(0), //! track label
118 fNEvents(0), //! number of events to process
119 fFirstEventNr(0), //! first event to process
120 fNParticles(0), //! number of particles in TreeK
121 fTreeGenTracks(0), //! output tree with generated tracks
122 fTreeKinks(0), //! output tree with Kinks
123 fTreeV0(0), //! output tree with V0
124 fFileGenTracks(0), //! output file with stored fTreeGenTracks
125 fLoader(0), //! pointer to the run loader
126 fTreeD(0), //! current tree with digits
127 fTreeTR(0), //! current tree with TR
128 fStack(0), //! current stack
129 fGenInfo(0), //! array with pointers to gen info
130 fNInfos(0), //! number of tracks with infos
131 fParamTPC(0), //! AliTPCParam
140 fFirstEventNr = firstEvent;
141 fEventNr = firstEvent;
143 sprintf(fFnRes,"%s",fnRes);
147 fLoader = AliRunLoader::Open(fnGalice);
149 delete AliRunLoader::Instance();
153 if (fLoader->LoadgAlice()){
154 cerr<<"Error occured while l"<<endl;
156 Int_t nall = fLoader->GetNumberOfEvents();
164 cerr<<"no events available"<<endl;
168 if (firstEvent+nEvents>nall) {
169 fEventNr = nall-firstEvent;
170 cerr<<"restricted number of events availaible"<<endl;
177 ////////////////////////////////////////////////////////////////////////
178 AliGenInfoMaker::~AliGenInfoMaker()
185 fLoader->UnloadgAlice();
192 AliMCInfo * AliGenInfoMaker::MakeInfo(UInt_t i)
195 // Make info structure for given particle index
198 if (fGenInfo[i]) return fGenInfo[i];
199 fGenInfo[i] = new AliMCInfo;
208 Int_t AliGenInfoMaker::ProcessEvent(AliMCEventHandler* mcinfo){
210 // Process MC info from the task
214 fParamTPC = GetTPCParam();
216 fStack = mcinfo->MCEvent()->Stack();
217 fTreeTR = mcinfo->TreeTR();
219 // array with preprocessedprocessed information
220 fGenTracksArray = new TObjArray;
221 fGenKinkArray = new TObjArray;
222 fGenV0Array = new TObjArray;
231 Int_t AliGenInfoMaker::ProcessEvent(){
235 fNParticles = fStack->GetNtrack();
237 fGenInfo = new AliMCInfo*[fNParticles];
238 for (UInt_t i = 0; i<fNParticles; i++) {
242 cout<<"Start to process event "<<fEventNr<<endl;
243 cout<<"\tfNParticles = "<<fNParticles<<endl;
244 if (fDebug>2) cout<<"\n\n\n\tStart loop over TreeTR"<<endl;
245 if (TreeTRLoop()>0) return 1;
247 if (fDebug>2) cout<<"\n\n\n\tStart loop over TreeD"<<endl;
248 if (TreeDLoop()>0) return 1;
250 if (fDebug>2) cout<<"\n\n\n\tStart loop over TreeK"<<endl;
251 if (TreeKLoop()>0) return 1;
252 if (fDebug>2) cout<<"\tEnd loop over TreeK"<<endl;
254 // if (BuildKinkInfo()>0) return 1;
255 if (BuildV0Info()>0) return 1;
256 if (fDebug>2) cout<<"\tEnd loop over TreeK"<<endl;
258 for (UInt_t i = 0; i<fNParticles; i++) {
259 if (fGenInfo[i]) delete fGenInfo[i];
271 Int_t AliGenInfoMaker::SetIO()
274 // Set IO for given event
276 CreateTreeGenTracks();
277 if (!fTreeGenTracks) return 1;
279 fParamTPC = GetTPCParam();
284 ////////////////////////////////////////////////////////////////////////
285 Int_t AliGenInfoMaker::SetIO(Int_t eventNr)
290 fLoader->SetEventNumber(eventNr);
292 fLoader->LoadHeader();
293 fLoader->LoadKinematics();
294 fStack = fLoader->Stack();
296 fLoader->LoadTrackRefs();
298 fTreeTR = fLoader->TreeTR();
300 AliTPCLoader * tpcl = (AliTPCLoader*)fLoader->GetLoader("TPCLoader");
302 fTreeD = tpcl->TreeD();
306 Int_t AliGenInfoMaker::CloseIOEvent()
309 // Close IO for current event
311 fLoader->UnloadHeader();
312 fLoader->UnloadKinematics();
313 fLoader->UnloadTrackRefs();
314 AliTPCLoader * tpcl = (AliTPCLoader*)fLoader->GetLoader("TPCLoader");
315 tpcl->UnloadDigits();
319 Int_t AliGenInfoMaker::CloseIO()
321 fLoader->UnloadgAlice();
327 ////////////////////////////////////////////////////////////////////////
328 Int_t AliGenInfoMaker::Exec()
331 // Make a comparision MC tree
332 // Connect MC information -TPArticle - AliTrackRefernces ...
336 Int_t status =SetIO();
337 if (status>0) return status;
339 for (fEventNr = fFirstEventNr; fEventNr < fFirstEventNr+fNEvents;
348 cerr<<"Exec finished"<<endl;
359 ////////////////////////////////////////////////////////////////////////
360 void AliGenInfoMaker::CreateTreeGenTracks()
365 fFileGenTracks = TFile::Open(fFnRes,"RECREATE");
366 if (!fFileGenTracks) {
367 cerr<<"Error in CreateTreeGenTracks: cannot open file "<<fFnRes<<endl;
370 fTreeGenTracks = new TTree("genTracksTree","genTracksTree");
371 AliMCInfo * info = new AliMCInfo;
372 fTreeGenTracks->Branch("MC","AliMCInfo",&info);
375 AliGenKinkInfo *kinkinfo = new AliGenKinkInfo;
376 fTreeKinks = new TTree("genKinksTree","genKinksTree");
377 fTreeKinks->Branch("MC","AliGenKinkInfo",&kinkinfo);
380 AliGenV0Info *v0info = new AliGenV0Info;
381 fTreeV0 = new TTree("genV0Tree","genV0Tree");
382 fTreeV0->Branch("MC","AliGenV0Info",&v0info);
387 fTreeGenTracks->AutoSave();
388 fTreeKinks->AutoSave();
394 ////////////////////////////////////////////////////////////////////////
395 void AliGenInfoMaker::CloseOutputFile()
398 // Close Output files
400 if (!fFileGenTracks) {
401 cerr<<"File "<<fFnRes<<" not found as an open file."<<endl;
404 fFileGenTracks->cd();
405 fTreeGenTracks->Write();
406 delete fTreeGenTracks;
412 fFileGenTracks->Close();
413 delete fFileGenTracks;
417 ////////////////////////////////////////////////////////////////////////
418 Int_t AliGenInfoMaker::TreeKLoop()
421 // open the file with treeK
422 // loop over all entries there and save information about some tracks
425 AliStack * stack = fStack;
426 if (!stack) {cerr<<"Stack was not found!\n"; return 1;}
429 cout<<"There are "<<fNParticles<<" primary and secondary particles in event "
433 TParticlePDG *ppdg = 0;
434 // not all generators give primary vertex position. Take the vertex
435 // of the particle 0 as primary vertex.
436 TDatabasePDG pdg; //get pdg table
438 TParticle *particle = stack->ParticleFromTreeK(0);
439 fVPrim[0] = particle->Vx();
440 fVPrim[1] = particle->Vy();
441 fVPrim[2] = particle->Vz();
443 for (UInt_t iParticle = 0; iParticle < fNParticles; iParticle++) {
444 // load only particles with TR
445 AliMCInfo * info = GetInfo(iParticle);
447 //////////////////////////////////////////////////////////////////////
448 info->fLabel = iParticle;
450 info->fParticle = *(stack->Particle(iParticle));
451 info->fVDist[0] = info->fParticle.Vx()-fVPrim[0];
452 info->fVDist[1] = info->fParticle.Vy()-fVPrim[1];
453 info->fVDist[2] = info->fParticle.Vz()-fVPrim[2];
454 info->fVDist[3] = TMath::Sqrt(info->fVDist[0]*info->fVDist[0]+
455 info->fVDist[1]*info->fVDist[1]+info->fVDist[2]*info->fVDist[2]);
458 ipdg = info->fParticle.GetPdgCode();
460 ppdg = pdg.GetParticle(ipdg);
461 info->fEventNr = fEventNr;
463 if (fGenTracksArray){
464 fGenTracksArray->AddLast(info->Clone());
469 // write the results to the tree - if specified
471 TBranch * br = fTreeGenTracks->GetBranch("MC");
472 for (UInt_t iParticle = 0; iParticle < fNParticles; iParticle++) {
473 // load only particles with TR
474 AliMCInfo * info = GetInfo(iParticle);
476 //////////////////////////////////////////////////////////////////////
477 br->SetAddress(&info);
478 fTreeGenTracks->Fill();
480 fTreeGenTracks->AutoSave();
483 if (fDebug > 2) cerr<<"end of TreeKLoop"<<endl;
488 ////////////////////////////////////////////////////////////////////////////////////
489 Int_t AliGenInfoMaker::BuildKinkInfo()
492 // Build tree with Kink Information
494 AliStack * stack = fStack;
495 if (!stack) {cerr<<"Stack was not found!\n"; return 1;}
498 cout<<"There are "<<fNParticles<<" primary and secondary particles in event "
502 //TParticlePDG *ppdg = 0;
503 // not all generators give primary vertex position. Take the vertex
504 // of the particle 0 as primary vertex.
505 TDatabasePDG pdg; //get pdg table
506 //thank you very much root for this
507 TBranch * br = fTreeKinks->GetBranch("MC");
509 AliGenKinkInfo * kinkinfo = new AliGenKinkInfo;
511 for (UInt_t iParticle = 0; iParticle < fNParticles; iParticle++) {
512 // load only particles with TR
513 AliMCInfo * info = GetInfo(iParticle);
515 if (info->fCharge==0) continue;
516 if (info->fTRdecay.R()>500) continue; //R cut - decay outside barrel
517 TParticle & particle = info->fParticle;
518 Int_t first = particle.GetDaughter(0);
519 if (first ==0) continue;
521 Int_t last = particle.GetDaughter(1);
522 if (last ==0) last=first;
523 AliMCInfo * info2 = 0;
524 AliMCInfo * dinfo = 0;
526 for (Int_t id2=first;id2<=last;id2++){
527 info2 = GetInfo(id2);
528 if (!info2) continue;
529 TParticle &p2 = info2->fParticle;
530 Double_t r = TMath::Sqrt(p2.Vx()*p2.Vx()+p2.Vy()*p2.Vy());
531 if ( TMath::Abs(info->fTRdecay.R()-r)>0.01) continue;
532 if (!(p2.GetPDG())) continue;
535 kinkinfo->SetInfoMother(*info);
536 kinkinfo->SetInfoDaughter(*dinfo);
537 if (kinkinfo->GetMinus().GetParticle().GetPDG()==0 || kinkinfo->GetPlus().GetParticle().GetPDG()==0) continue;
539 br->SetAddress(&kinkinfo);
543 fTreeKinks->AutoSave();
544 if (fDebug > 2) cerr<<"end of Kink Loop"<<endl;
549 ////////////////////////////////////////////////////////////////////////////////////
550 Int_t AliGenInfoMaker::BuildV0Info()
553 // Build tree with V0 Information
555 AliStack * stack = fStack;
556 if (!stack) {cerr<<"Stack was not found!\n"; return 1;}
559 cout<<"There are "<<fNParticles<<" primary and secondary particles in event "
563 TDatabasePDG pdg; //get pdg table
564 //thank you very much root for this
565 TBranch * br = fTreeV0->GetBranch("MC");
567 AliGenV0Info * v0info = new AliGenV0Info;
570 for (UInt_t iParticle = 0; iParticle < fNParticles; iParticle++) {
571 TParticle * mParticle = fStack->Particle(iParticle);
572 if (!mParticle) continue;
573 if (mParticle->GetPDG()==0) continue;
574 if (mParticle->GetPDG()->Charge()!=0) continue; //only neutral particle
576 Int_t first = mParticle->GetDaughter(0);
577 Int_t last = mParticle->GetDaughter(1);
578 if (first-last==0) continue;
580 AliMCInfo * info0 = GetInfo(first);
581 AliMCInfo * info1 = GetInfo(first+1);
583 if (info0->GetPdg()==0) continue;
584 if (info1->GetPdg()==0) continue;
585 v0info->SetInfoP(*info0);
586 v0info->SetInfoM(*info1);
587 v0info->SetMother(*mParticle);
588 v0info->Update(fVPrim);
589 if (fGenV0Array) fGenV0Array->AddLast(v0info);
590 br->SetAddress(&v0info);
595 if (fDebug > 2) cerr<<"end of V0 Loop"<<endl;
603 ////////////////////////////////////////////////////////////////////////
604 Int_t AliGenInfoMaker::TreeDLoop()
607 // open the file with treeD
608 // loop over all entries there and save information about some tracks
610 if (!fTreeD) return 0;
611 Int_t nInnerSector = fParamTPC->GetNInnerSector();
613 Int_t zero=fParamTPC->GetZeroSup()+6;
616 AliSimDigits digitsAddress, *digits=&digitsAddress;
617 fTreeD->GetBranch("Segment")->SetAddress(&digits);
619 Int_t sectorsByRows=(Int_t)fTreeD->GetEntries();
620 if (fDebug > 1) cout<<"\tsectorsByRows = "<<sectorsByRows<<endl;
621 for (Int_t i=0; i<sectorsByRows; i++) {
622 if (!fTreeD->GetEvent(i)) continue;
624 fParamTPC->AdjustSectorRow(digits->GetID(),sec,row);
625 if (fDebug > 1) cout<<sec<<' '<<row<<" \r";
626 // here I expect that upper sectors follow lower sectors
627 if (sec > nInnerSector) rowShift = fParamTPC->GetNRowLow();
629 digits->ExpandTrackBuffer();
632 Int_t iRow=digits->CurrentRow();
633 Int_t iColumn=digits->CurrentColumn();
634 Short_t digitValue = digits->CurrentDigit();
635 if (digitValue >= zero) {
637 for (Int_t j = 0; j<3; j++) {
638 // label = digits->GetTrackID(iRow,iColumn,j);
639 label = digits->GetTrackIDFast(iRow,iColumn,j)-2;
640 if (label >= (Int_t)fNParticles) { //don't label from bakground event
643 if (label >= 0 && label <= (Int_t)fNParticles) {
645 cout<<"Inner loop: sector, iRow, iColumn, label, value, row "
647 <<iRow<<" "<<iColumn<<" "<<label<<" "<<digitValue
650 AliMCInfo * info = GetInfo(label);
652 info->fTPCRow.SetRow(row+rowShift);
657 } while (digits->Next());
660 if (fDebug > 2) cerr<<"end of TreeDLoop"<<endl;
667 ////////////////////////////////////////////////////////////////////////
668 Int_t AliGenInfoMaker::TreeTRLoop()
671 // loop over TrackReferences and store the first one for each track
673 TTree * treeTR = fTreeTR;
674 Int_t nPrimaries = (Int_t) treeTR->GetEntries();
675 if (fDebug > 1) cout<<"There are "<<nPrimaries<<" entries in TreeTR"<<endl;
679 TClonesArray* runArrayTR = new TClonesArray("AliTrackReference");
681 if (treeTR->GetBranch("TrackReferences")) treeTR->GetBranch("TrackReferences")->SetAddress(&runArrayTR);
685 for (Int_t ipart = 0; ipart<fStack->GetNtrack(); ipart++) {
686 TParticle * part = fStack->Particle(ipart);
688 if (part->GetPDG()==0) continue;
689 if (part->GetPDG()->Charge()==0) continue;
691 if (part->Pt()<fITSPtCut) continue;
692 if (part->R()>250.) continue;
693 if (TMath::Abs(part->Vz())>250.) continue;
694 if (TMath::Abs(part->Pz()/part->Pt())>2.5) continue;
695 //AliMCInfo * info = ;
701 for (Int_t iPrimPart = 0; iPrimPart<nPrimaries; iPrimPart++) {
702 treeTR->GetEntry(iPrimPart);
704 // gettrack references
706 for (Int_t iTrackRef = 0; iTrackRef < runArrayTR->GetEntriesFast(); iTrackRef++) {
707 AliTrackReference *trackRef = (AliTrackReference*)runArrayTR->At(iTrackRef);
709 Int_t label = trackRef->GetTrack();
710 AliMCInfo * cinfo = GetInfo(label);
711 if (cinfo) cinfo->CalcTPCrows(runArrayTR);
715 if (trackRef->DetectorId()== AliTrackReference::kTPC){
717 Int_t label1 = trackRef->GetTrack();
718 AliMCInfo * info = GetInfo(label1);
719 if (!info && trackRef->Pt()<fTPCPtCut) continue;
721 info = MakeInfo(label1);
724 info->fPrimPart = iPrimPart;
725 TClonesArray & arr = *(info->fTPCReferences);
726 new (arr[arr.GetEntriesFast()]) AliTrackReference(*trackRef);
731 if (trackRef->DetectorId()== AliTrackReference::kITS){
733 Int_t label1 = trackRef->GetTrack();
734 AliMCInfo * info = GetInfo(label1);
735 if (!info && trackRef->Pt()<fITSPtCut) continue;
736 if (!info) info = MakeInfo(label1);
738 info->fPrimPart = iPrimPart;
739 TClonesArray & arr = *(info->fITSReferences);
740 new (arr[arr.GetEntriesFast()]) AliTrackReference(*trackRef);
745 if (trackRef->DetectorId()== AliTrackReference::kTRD){
747 Int_t label1 = trackRef->GetTrack();
748 AliMCInfo * info = GetInfo(label1);
749 if (!info&&trackRef->P()<fTRDPtCut) continue;
750 if (!info) info = MakeInfo(label1);
752 info->fPrimPart = iPrimPart;
753 TClonesArray & arr = *(info->fTRDReferences);
754 new (arr[arr.GetEntriesFast()]) AliTrackReference(*trackRef);
759 if (trackRef->DetectorId()== AliTrackReference::kTOF){
761 Int_t label1 = trackRef->GetTrack();
762 AliMCInfo * info = GetInfo(label1);
763 if (!info&&trackRef->P()<fTOFPtCut) continue;
764 if (!info) info = MakeInfo(label1);
766 info->fPrimPart = iPrimPart;
767 TClonesArray & arr = *(info->fTOFReferences);
768 new (arr[arr.GetEntriesFast()]) AliTrackReference(*trackRef);
773 if (trackRef->DetectorId()== AliTrackReference::kDisappeared){
775 AliMCInfo * info = GetInfo(label);
777 info->fTRdecay = *trackRef;
786 ////////////////////////////////////////////////////////////////////////
787 Float_t AliGenInfoMaker::TR2LocalX(AliTrackReference *trackRef,
788 AliTPCParam *paramTPC) const {
791 // Transformation from Gloabal to local tacking system
793 Float_t x[3] = { trackRef->X(),trackRef->Y(),trackRef->Z()};
795 paramTPC->Transform0to1(x,index);
796 paramTPC->Transform1to2(x,index);
799 ////////////////////////////////////////////////////////////////////////
803 AliTPCParam * AliGenInfoMaker::GetTPCParam(){
805 // create default TPC parameters
807 AliTPCParamSR * par = new AliTPCParamSR;