1 // This is an example macro which loops on all ESD events in a file set
2 // and stores the selected information in form of mini ESD tree.
3 // It also extracts the corresponding MC information in a parallel MC tree.
5 // Input: galice.root,Kinematics.root,AliESDs.root (read only)
6 // Output: miniesd.root (recreate)
8 // Usage: faster if compiled
9 // gSystem->SetIncludePath("-I$ROOTSYS/include -I$ALICE_ROOT/include -I$ALICE_ROOT/TPC");
10 // gROOT->LoadMacro("mcminiesd.C+");
13 // Author: Peter Hristov
14 // e-mail: Peter.Hristov@cern.ch
16 #if !defined(__CINT__) || defined(__MAKECINT__)
19 #include <Riostream.h>
23 #include <TStopwatch.h>
25 #include <TParticle.h>
26 #include <TLorentzVector.h>
29 #include <TDatabasePDG.h>
30 #include <TParticlePDG.h>
32 // AliRoot include files
34 #include "AliESDVertex.h"
35 #include "AliRunLoader.h"
38 #include "AliHeader.h"
39 #include "AliGenEventHeader.h"
40 #include "AliTPCtrack.h"
41 #include "AliTracker.h"
45 void selectRecTracks(AliESD* esdOut, Int_t * select, Int_t &nkeep0) {
46 // Select also all the reconstructed tracks in case it was not done before
47 Int_t nrec = esdOut->GetNumberOfTracks();
48 for(Int_t irec=0; irec<nrec; irec++) {
49 AliESDtrack * track = esdOut->GetTrack(irec);
50 UInt_t label = TMath::Abs(track->GetLabel());
51 if (label>=10000000) continue; // Underlying event. 10000000 is the
52 // value of fkMASKSTEP in AliRunDigitizer
60 void copyGeneralESDInfo(AliESD* esdIn, AliESD* esdOut) {
61 // Event and run number
62 esdOut->SetEventNumber(esdIn->GetEventNumber());
63 esdOut->SetRunNumber(esdIn->GetRunNumber());
66 esdOut->SetTrigger(esdIn->GetTrigger());
69 esdOut->SetMagneticField(esdIn->GetMagneticField());
72 const AliESDVertex * vtxIn = esdIn->GetVertex();
76 vtxIn->GetCovMatrix(cov);
78 AliESDVertex * vtxOut = new AliESDVertex(pos,cov,
80 vtxIn->GetNContributors());
82 vtxIn->GetTruePos(tp);
83 vtxOut->SetTruePos(tp);
85 esdOut->SetVertex(vtxOut);
88 void selectMiniESD(AliESD* esdIn, AliESD* &esdOut) {
89 // This function copies the general ESD information
90 // and selects the reconstructed tracks of interest
91 // The second argument is a reference to pointer since we
92 // want to operate not with the content, but with the pointer itself
94 printf("--------------------\n");
95 printf("Selecting data mini ESD\n");
99 // Create the new output ESD
100 esdOut = new AliESD();
102 // Copy the general information
103 copyGeneralESDInfo(esdIn, esdOut);
106 Int_t ntrk = esdIn->GetNumberOfTracks();
109 for (Int_t itrk=0; itrk<ntrk; itrk++) {
111 AliESDtrack * trackIn = esdIn->GetTrack(itrk);
112 UInt_t status=trackIn->GetStatus();
114 //select only tracks with TPC or ITS refit
115 if ((status&AliESDtrack::kTPCrefit)==0
116 && (status&AliESDtrack::kITSrefit)==0
119 //select only tracks with "combined" PID
120 if ((status&AliESDtrack::kESDpid)==0) continue;
122 AliESDtrack * trackOut = new AliESDtrack(*trackIn);
124 // Reset most of the redundant information
125 trackOut->MakeMiniESDtrack();
127 esdOut->AddTrack(trackOut);
129 // Do not delete trackIn, it is a second pointer to an
130 // object belonging to the TClonesArray
134 printf("%d tracks selected\n",esdOut->GetNumberOfTracks());
138 printf("--------------------\n");
141 void fixMotherDaughter(TClonesArray& particles, Int_t * map) {
142 // Fix mother-daughter relationship
143 // using the map of old to new indexes
144 Int_t nkeep = particles.GetEntriesFast();
145 for (Int_t i=0; i<nkeep; i++) {
146 TParticle * part = (TParticle*)particles[i];
149 Int_t mum = part->GetFirstMother();
150 if (mum>-1 && i>map[mum])
151 part->SetFirstMother(map[mum]);
154 Int_t fd = part->GetFirstDaughter();
155 Int_t ld = part->GetLastDaughter();
157 // invalidate daughters
158 part->SetFirstDaughter(-1);
159 part->SetLastDaughter(-1);
161 for (Int_t id=TMath::Max(fd,0); id<=ld; id++) {
163 // this daughter was selected
164 if(part->GetFirstDaughter() < 0) part->SetFirstDaughter(map[id]);
165 part->SetLastDaughter(map[id]);
171 void checkConsistency(TClonesArray& particles) {
173 // each mother is before the daughters,
174 // each daughter from the mother's list
175 // points back to its mother
176 Int_t nkeep = particles.GetEntriesFast();
177 for (Int_t i=0; i<nkeep; i++) {
178 TParticle * part = (TParticle*)particles[i];
180 Int_t mum = part->GetFirstMother();
182 if (mum>-1 && i<mum) {
183 cout << "Problem: mother " << mum << " after daughter " << i << endl;
186 Int_t fd = part->GetFirstDaughter();
187 Int_t ld = part->GetLastDaughter();
189 if (fd > ld ) cout << "Problem " << fd << " > " << ld << endl;
190 if (fd > -1 && fd < i )
191 cout << "Problem: mother " << i << " after daughter " << ld << endl;
193 for (Int_t id=TMath::Max(fd,0); id<=ld; id++) {
194 TParticle * daughter = (TParticle*)particles[id];
195 if (daughter->GetFirstMother() != i) {
196 cout << "Problem "<< i << " not "
197 << daughter->GetFirstMother() << endl;
206 void kinetree(AliRunLoader* runLoader, AliESD* &esdOut, TClonesArray* &ministack) {
208 printf("--------------------\n");
209 printf("Selecting MC mini ESD\n");
213 // Particle properties
214 TDatabasePDG * pdgdb = TDatabasePDG::Instance();
217 AliStack * stack = runLoader->Stack();
219 Int_t npart = stack->GetNtrack();
221 // Particle momentum and vertex. Will be extracted from TParticle
222 TLorentzVector momentum;
224 runLoader->GetHeader()->GenEventHeader()->PrimaryVertex(vertex);
227 // Counter for selected particles
230 Int_t * select = new Int_t[npart];
232 // Loop on particles: physics selection
233 for (Int_t ipart=0; ipart<npart; ipart++) {
237 TParticle * part = stack->Particle(ipart);
240 cout << "Empty particle " << ipart << endl;
244 // Particle momentum and vertex of origin
245 part->Momentum(momentum);
246 dvertex.SetXYZ(part->Vx()-vertex[0],
247 part->Vy()-vertex[1],
248 part->Vz()-vertex[2]);
250 // Now select only the "interesting" particles
252 // Select all particles from the primary vertex:
253 // resonances and products of resonance decays
254 if(dvertex.Mag()<0.0001) {
260 // Reject particles born outside ITS
261 if (dvertex.Perp()>100) continue;
263 // Reject particles with too low momentum, they don't rich TPC
264 if (part->Pt()<0.1) continue;
266 // Reject "final state" neutral particles
267 // This has to be redone after this loop
268 Int_t pdgcode = part->GetPdgCode();
269 TParticlePDG * pdgpart = pdgdb->GetParticle(pdgcode);
271 if (pdgpart) charge = (Int_t) pdgpart->Charge();
274 if (part->GetFirstDaughter()<0) continue;
277 // Select the rest of particles except the case
278 // particle -> particle + X
279 // for example bremstrahlung and delta electrons
280 Int_t mumid = part->GetFirstMother();
281 TParticle * mother = stack->Particle(mumid);
283 Int_t mumpdg = mother->GetPdgCode();
284 Bool_t skip = kFALSE;
285 for (Int_t id=mother->GetFirstDaughter();
286 id<=mother->GetLastDaughter();
288 TParticle * daughter=stack->Particle(id);
289 if (!daughter) continue;
290 if (mumpdg == daughter->GetPdgCode()) {
298 // All the criteria are OK, this particle is selected
302 } // end loop over particles in the current event
304 selectRecTracks(esdOut, select, nkeep0);
306 // Container for the selected particles
307 TClonesArray * pparticles = new TClonesArray("TParticle",nkeep0);
308 TClonesArray &particles = *pparticles;
310 // Map of the indexes in the stack and the new ones in the TClonesArray
311 Int_t * map = new Int_t[npart];
313 // Counter for selected particles
316 for (Int_t ipart=0; ipart<npart; ipart++) {
318 map[ipart] = -99; // Reset the map
322 TParticle * part = stack->Particle(ipart);
324 new(particles[nkeep++]) TParticle(*part);
329 if (nkeep0 != nkeep) printf("Strange %d is not %d\n",nkeep0,nkeep);
333 // Fix mother-daughter relationship
334 fixMotherDaughter(particles,map);
337 checkConsistency(particles);
339 // Now remove the "final state" neutral particles
341 TClonesArray * particles1 = new TClonesArray("TParticle",nkeep);
342 Int_t * map1 = new Int_t[nkeep];
346 for (Int_t ipart=0; ipart<nkeep; ipart++) {
348 map1[ipart] = -99; // Reset the map
350 TParticle * part = (TParticle*)particles[ipart];
352 // Reject "final state" neutral particles
353 // This has to be redone after this loop
354 Int_t pdgcode = part->GetPdgCode();
355 TParticlePDG * pdgpart = pdgdb->GetParticle(pdgcode);
357 if (pdgpart) charge = (Int_t) pdgpart->Charge();
360 if (part->GetFirstDaughter()<0) continue;
363 // Select the particle
364 map1[ipart] = nkeep1;
365 TClonesArray &ar = *particles1;
366 new(ar[nkeep1++]) TParticle(*part);
371 cout << nkeep1 << " particles finally selected" << endl;
373 fixMotherDaughter(*particles1,map1);
374 checkConsistency(*particles1);
376 // Remap the labels of reconstructed tracks
378 AliESD * esdNew = new AliESD();
380 copyGeneralESDInfo(esdOut,esdNew);
383 Int_t nrec = esdOut->GetNumberOfTracks();
384 for(Int_t irec=0; irec<nrec; irec++) {
385 AliESDtrack * track = esdOut->GetTrack(irec);
386 UInt_t label = TMath::Abs(track->GetLabel());
387 if (label<10000000) { // Signal event. 10000000 is the
388 // value of fkMASKSTEP in AliRunDigitizer
389 track->SetLabel(TMath::Sign(map1[map[label]],track->GetLabel()));
391 esdNew->AddTrack(track);
397 ministack = particles1;
406 printf("--------------------\n");
411 AliTPCtrack * particle2track(TParticle * part) {
413 // Converts TParticle to AliTPCtrack
421 // Calculate alpha: the rotation angle of the corresponding TPC sector
422 alpha = part->Phi()*180./TMath::Pi();
423 if (alpha<0) alpha+= 360.;
424 if (alpha>360) alpha -= 360.;
426 Int_t sector = (Int_t)(alpha/20.);
427 alpha = 10. + 20.*sector;
429 alpha *= TMath::Pi();
432 // Reset the covariance matrix
433 for (Int_t i=0; i<15; i++) cc[i]=0.;
437 index = part->GetUniqueID();
439 // Get the vertex of origin and the momentum
440 TVector3 ver(part->Vx(),part->Vy(),part->Vz());
441 TVector3 mom(part->Px(),part->Py(),part->Pz());
444 // Rotate to the local (sector) coordinate system
448 // X of the referense plane
452 // fX = xref X-coordinate of this track (reference plane)
453 // fAlpha = Alpha Rotation angle the local (TPC sector)
454 // fP0 = YL Y-coordinate of a track
455 // fP1 = ZG Z-coordinate of a track
456 // fP2 = C*x0 x0 is center x in rotated frame
457 // fP3 = Tgl tangent of the track momentum dip angle
458 // fP4 = C track curvature
461 TVector3 field(0.,0.,1/AliKalmanTrack::GetConvConst());
462 // radius [cm] of track projection in (x,y)
464 mom.Pt()*AliKalmanTrack::GetConvConst();
467 TDatabasePDG::Instance()->GetParticle(part->GetPdgCode())->Charge();
470 if (TMath::Abs(charge) < 0.9) charge=1.e-9;
472 TVector3 vrho = mom.Cross(field);
477 Double_t x0 = vrho.X();
481 xx[3] = mom.Pz()/mom.Pt();
484 // if (TMath::Abs(charge) < 0.9) xx[4] = 0;
486 AliTPCtrack * tr = new AliTPCtrack(index, xx, cc, xref, alpha);
494 printf("====================\n");
495 printf("Main program\n");
499 // Input "data" file, tree, and branch
500 TFile * fIn = TFile::Open("AliESDs.root");
501 TTree * tIn = (TTree*)fIn->Get("esdTree");
502 TBranch * bIn = tIn->GetBranch("ESD");
504 AliESD * esdIn = 0; // The input ESD object is put here
505 bIn->SetAddress(&esdIn);
507 // Output file, trees, and branches.
508 // Both "data" and MC are stored in the same file
509 TFile * fOut = TFile::Open("miniesd.root","recreate");
510 TTree * tOut = new TTree("esdTree","esdTree");
511 TTree * tMC = new TTree("mcStackTree","mcStackTree");
513 AliESD * esdOut = 0; // The newly created ESD object
514 TBranch * bOut = tOut->Branch("ESD","AliESD",&esdOut);
515 bOut->SetCompressionLevel(9);
517 // TParticles are stored in TClonesArray
518 // The corresponding branch is created using "dummy" TClonesArray
519 TClonesArray * ministack = new TClonesArray("TParticle");
520 TBranch * bMC = tMC->Branch("Stack",&ministack);
523 bMC->SetCompressionLevel(9);
526 AliHeader * headerIn = 0; //
527 TBranch * bHeader = tMC->Branch("Header","AliHeader",&headerIn);
528 bHeader->SetCompressionLevel(9);
531 AliRunLoader* runLoader = AliRunLoader::Open("galice.root");
534 runLoader->LoadgAlice();
535 gAlice = runLoader->GetAliRun();
538 AliTracker::SetFieldMap(gAlice->Field(),1); // 1 means uniform magnetic field
540 // Now load kinematics and event header
541 runLoader->LoadKinematics();
542 runLoader->LoadHeader();
544 // Loop on events: check that MC and data contain the same number of events
545 Int_t nevESD = bIn->GetEntries();
546 Int_t nevMC = (Int_t)runLoader->GetNumberOfEvents();
549 cout << "Inconsistent number of ESD("<<nevESD<<") and MC("<<nevMC<<") events" << endl;
552 Int_t nev=TMath::Min(nevMC,nevESD);
554 cout << nev << " events to process" << endl;
556 for (Int_t iev=0; iev<nev; iev++) {
557 cout << "Event " << iev << endl;
562 selectMiniESD(esdIn,esdOut);
565 runLoader->GetEvent(iev);
568 kinetree(runLoader,esdOut,ministack);
569 bMC->SetAddress(&ministack); // needed because ministack has changed
572 headerIn = runLoader->GetHeader();
586 fOut = tOut->GetCurrentFile();
594 printf("====================\n");