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->SetTriggerMask(esdIn->GetTriggerMask());
69 esdOut->SetMagneticField(esdIn->GetMagneticField());
72 const AliESDVertex * vtxIn = esdIn->GetVertex();
73 AliESDVertex * vtxOut = 0x0;
78 vtxIn->GetCovMatrix(cov);
80 vtxOut = new AliESDVertex(pos,cov,
82 vtxIn->GetNContributors());
85 vtxOut = new AliESDVertex();
87 esdOut->SetVertex(vtxOut);
90 void selectMiniESD(AliESD* esdIn, AliESD* &esdOut) {
91 // This function copies the general ESD information
92 // and selects the reconstructed tracks of interest
93 // The second argument is a reference to pointer since we
94 // want to operate not with the content, but with the pointer itself
96 printf("--------------------\n");
97 printf("Selecting data mini ESD\n");
101 // Create the new output ESD
102 esdOut = new AliESD();
104 // Copy the general information
105 copyGeneralESDInfo(esdIn, esdOut);
108 Int_t ntrk = esdIn->GetNumberOfTracks();
111 for (Int_t itrk=0; itrk<ntrk; itrk++) {
113 AliESDtrack * trackIn = esdIn->GetTrack(itrk);
114 UInt_t status=trackIn->GetStatus();
116 //select only tracks with TPC or ITS refit
117 if ((status&AliESDtrack::kTPCrefit)==0
118 && (status&AliESDtrack::kITSrefit)==0
121 //select only tracks with "combined" PID
122 if ((status&AliESDtrack::kESDpid)==0) continue;
124 AliESDtrack * trackOut = new AliESDtrack(*trackIn);
126 // Reset most of the redundant information
127 trackOut->MakeMiniESDtrack();
129 esdOut->AddTrack(trackOut);
131 // Do not delete trackIn, it is a second pointer to an
132 // object belonging to the TClonesArray
136 printf("%d tracks selected\n",esdOut->GetNumberOfTracks());
140 printf("--------------------\n");
143 void fixMotherDaughter(TClonesArray& particles, Int_t * map) {
144 // Fix mother-daughter relationship
145 // using the map of old to new indexes
146 Int_t nkeep = particles.GetEntriesFast();
147 for (Int_t i=0; i<nkeep; i++) {
148 TParticle * part = (TParticle*)particles[i];
151 Int_t mum = part->GetFirstMother();
152 if (mum>-1 && i>map[mum])
153 part->SetFirstMother(map[mum]);
156 Int_t fd = part->GetFirstDaughter();
157 Int_t ld = part->GetLastDaughter();
159 // invalidate daughters
160 part->SetFirstDaughter(-1);
161 part->SetLastDaughter(-1);
163 for (Int_t id=TMath::Max(fd,0); id<=ld; id++) {
165 // this daughter was selected
166 if(part->GetFirstDaughter() < 0) part->SetFirstDaughter(map[id]);
167 part->SetLastDaughter(map[id]);
173 void checkConsistency(TClonesArray& particles) {
175 // each mother is before the daughters,
176 // each daughter from the mother's list
177 // points back to its mother
178 Int_t nkeep = particles.GetEntriesFast();
179 for (Int_t i=0; i<nkeep; i++) {
180 TParticle * part = (TParticle*)particles[i];
182 Int_t mum = part->GetFirstMother();
184 if (mum>-1 && i<mum) {
185 cout << "Problem: mother " << mum << " after daughter " << i << endl;
188 Int_t fd = part->GetFirstDaughter();
189 Int_t ld = part->GetLastDaughter();
191 if (fd > ld ) cout << "Problem " << fd << " > " << ld << endl;
192 if (fd > -1 && fd < i )
193 cout << "Problem: mother " << i << " after daughter " << ld << endl;
195 for (Int_t id=TMath::Max(fd,0); id<=ld; id++) {
196 TParticle * daughter = (TParticle*)particles[id];
197 if (daughter->GetFirstMother() != i) {
198 cout << "Problem "<< i << " not "
199 << daughter->GetFirstMother() << endl;
208 void kinetree(AliRunLoader* runLoader, AliESD* &esdOut, TClonesArray* &ministack) {
210 printf("--------------------\n");
211 printf("Selecting MC mini ESD\n");
215 // Particle properties
216 TDatabasePDG * pdgdb = TDatabasePDG::Instance();
219 AliStack * stack = runLoader->Stack();
221 Int_t npart = stack->GetNtrack();
223 // Particle momentum and vertex. Will be extracted from TParticle
224 TLorentzVector momentum;
226 runLoader->GetHeader()->GenEventHeader()->PrimaryVertex(vertex);
229 // Counter for selected particles
232 Int_t * select = new Int_t[npart];
234 // Loop on particles: physics selection
235 for (Int_t ipart=0; ipart<npart; ipart++) {
239 TParticle * part = stack->Particle(ipart);
242 cout << "Empty particle " << ipart << endl;
246 // Particle momentum and vertex of origin
247 part->Momentum(momentum);
248 dvertex.SetXYZ(part->Vx()-vertex[0],
249 part->Vy()-vertex[1],
250 part->Vz()-vertex[2]);
252 // Now select only the "interesting" particles
254 // Select all particles from the primary vertex:
255 // resonances and products of resonance decays
256 if(dvertex.Mag()<0.0001) {
262 // Reject particles born outside ITS
263 if (dvertex.Perp()>100) continue;
265 // Reject particles with too low momentum, they don't rich TPC
266 if (part->Pt()<0.1) continue;
268 // Reject "final state" neutral particles
269 // This has to be redone after this loop
270 Int_t pdgcode = part->GetPdgCode();
271 TParticlePDG * pdgpart = pdgdb->GetParticle(pdgcode);
273 if (pdgpart) charge = (Int_t) pdgpart->Charge();
276 if (part->GetFirstDaughter()<0) continue;
279 // Select the rest of particles except the case
280 // particle -> particle + X
281 // for example bremstrahlung and delta electrons
282 Int_t mumid = part->GetFirstMother();
283 TParticle * mother = stack->Particle(mumid);
285 Int_t mumpdg = mother->GetPdgCode();
286 Bool_t skip = kFALSE;
287 for (Int_t id=mother->GetFirstDaughter();
288 id<=mother->GetLastDaughter();
290 TParticle * daughter=stack->Particle(id);
291 if (!daughter) continue;
292 if (mumpdg == daughter->GetPdgCode()) {
300 // All the criteria are OK, this particle is selected
304 } // end loop over particles in the current event
306 selectRecTracks(esdOut, select, nkeep0);
308 // Container for the selected particles
309 TClonesArray * pparticles = new TClonesArray("TParticle",nkeep0);
310 TClonesArray &particles = *pparticles;
312 // Map of the indexes in the stack and the new ones in the TClonesArray
313 Int_t * map = new Int_t[npart];
315 // Counter for selected particles
318 for (Int_t ipart=0; ipart<npart; ipart++) {
320 map[ipart] = -99; // Reset the map
324 TParticle * part = stack->Particle(ipart);
326 new(particles[nkeep++]) TParticle(*part);
331 if (nkeep0 != nkeep) printf("Strange %d is not %d\n",nkeep0,nkeep);
335 // Fix mother-daughter relationship
336 fixMotherDaughter(particles,map);
339 checkConsistency(particles);
341 // Now remove the "final state" neutral particles
343 TClonesArray * particles1 = new TClonesArray("TParticle",nkeep);
344 Int_t * map1 = new Int_t[nkeep];
348 for (Int_t ipart=0; ipart<nkeep; ipart++) {
350 map1[ipart] = -99; // Reset the map
352 TParticle * part = (TParticle*)particles[ipart];
354 // Reject "final state" neutral particles
355 // This has to be redone after this loop
356 Int_t pdgcode = part->GetPdgCode();
357 TParticlePDG * pdgpart = pdgdb->GetParticle(pdgcode);
359 if (pdgpart) charge = (Int_t) pdgpart->Charge();
362 if (part->GetFirstDaughter()<0) continue;
365 // Select the particle
366 map1[ipart] = nkeep1;
367 TClonesArray &ar = *particles1;
368 new(ar[nkeep1++]) TParticle(*part);
373 cout << nkeep1 << " particles finally selected" << endl;
375 fixMotherDaughter(*particles1,map1);
376 checkConsistency(*particles1);
378 // Remap the labels of reconstructed tracks
380 AliESD * esdNew = new AliESD();
382 copyGeneralESDInfo(esdOut,esdNew);
385 Int_t nrec = esdOut->GetNumberOfTracks();
386 for(Int_t irec=0; irec<nrec; irec++) {
387 AliESDtrack * track = esdOut->GetTrack(irec);
388 UInt_t label = TMath::Abs(track->GetLabel());
389 if (label<10000000) { // Signal event. 10000000 is the
390 // value of fkMASKSTEP in AliRunDigitizer
391 track->SetLabel(TMath::Sign(map1[map[label]],track->GetLabel()));
393 esdNew->AddTrack(track);
399 ministack = particles1;
408 printf("--------------------\n");
413 AliTPCtrack * particle2track(TParticle * part) {
415 // Converts TParticle to AliTPCtrack
423 // Calculate alpha: the rotation angle of the corresponding TPC sector
424 alpha = part->Phi()*180./TMath::Pi();
425 if (alpha<0) alpha+= 360.;
426 if (alpha>360) alpha -= 360.;
428 Int_t sector = (Int_t)(alpha/20.);
429 alpha = 10. + 20.*sector;
431 alpha *= TMath::Pi();
434 // Reset the covariance matrix
435 for (Int_t i=0; i<15; i++) cc[i]=0.;
439 index = part->GetUniqueID();
441 // Get the vertex of origin and the momentum
442 TVector3 ver(part->Vx(),part->Vy(),part->Vz());
443 TVector3 mom(part->Px(),part->Py(),part->Pz());
446 // Rotate to the local (sector) coordinate system
450 // X of the referense plane
454 // fX = xref X-coordinate of this track (reference plane)
455 // fAlpha = Alpha Rotation angle the local (TPC sector)
456 // fP0 = YL Y-coordinate of a track
457 // fP1 = ZG Z-coordinate of a track
458 // fP2 = C*x0 x0 is center x in rotated frame
459 // fP3 = Tgl tangent of the track momentum dip angle
460 // fP4 = C track curvature
463 TVector3 field(0.,0.,1/AliKalmanTrack::GetConvConst());
464 // radius [cm] of track projection in (x,y)
466 mom.Pt()*AliKalmanTrack::GetConvConst();
469 TDatabasePDG::Instance()->GetParticle(part->GetPdgCode())->Charge();
472 if (TMath::Abs(charge) < 0.9) charge=1.e-9;
474 TVector3 vrho = mom.Cross(field);
479 Double_t x0 = vrho.X();
483 xx[3] = mom.Pz()/mom.Pt();
486 // if (TMath::Abs(charge) < 0.9) xx[4] = 0;
488 AliTPCtrack * tr = new AliTPCtrack(index, xx, cc, xref, alpha);
496 printf("====================\n");
497 printf("Main program\n");
501 // Input "data" file, tree, and branch
502 TFile * fIn = TFile::Open("AliESDs.root");
503 TTree * tIn = (TTree*)fIn->Get("esdTree");
504 TBranch * bIn = tIn->GetBranch("ESD");
506 AliESD * esdIn = 0; // The input ESD object is put here
507 bIn->SetAddress(&esdIn);
509 // Output file, trees, and branches.
510 // Both "data" and MC are stored in the same file
511 TFile * fOut = TFile::Open("miniesd.root","recreate");
512 TTree * tOut = new TTree("esdTree","esdTree");
513 TTree * tMC = new TTree("mcStackTree","mcStackTree");
515 AliESD * esdOut = 0; // The newly created ESD object
516 TBranch * bOut = tOut->Branch("ESD","AliESD",&esdOut);
517 bOut->SetCompressionLevel(9);
519 // TParticles are stored in TClonesArray
520 // The corresponding branch is created using "dummy" TClonesArray
521 TClonesArray * ministack = new TClonesArray("TParticle");
522 TBranch * bMC = tMC->Branch("Stack",&ministack);
525 bMC->SetCompressionLevel(9);
528 AliHeader * headerIn = 0; //
529 TBranch * bHeader = tMC->Branch("Header","AliHeader",&headerIn);
530 bHeader->SetCompressionLevel(9);
533 AliRunLoader* runLoader = AliRunLoader::Open("galice.root");
536 runLoader->LoadgAlice();
537 gAlice = runLoader->GetAliRun();
540 AliTracker::SetFieldMap(gAlice->Field(),1); // 1 means uniform magnetic field
542 // Now load kinematics and event header
543 runLoader->LoadKinematics();
544 runLoader->LoadHeader();
546 // Loop on events: check that MC and data contain the same number of events
547 Int_t nevESD = bIn->GetEntries();
548 Int_t nevMC = (Int_t)runLoader->GetNumberOfEvents();
551 cout << "Inconsistent number of ESD("<<nevESD<<") and MC("<<nevMC<<") events" << endl;
554 Int_t nev=TMath::Min(nevMC,nevESD);
556 cout << nev << " events to process" << endl;
558 for (Int_t iev=0; iev<nev; iev++) {
559 cout << "Event " << iev << endl;
564 selectMiniESD(esdIn,esdOut);
567 runLoader->GetEvent(iev);
570 kinetree(runLoader,esdOut,ministack);
571 bMC->SetAddress(&ministack); // needed because ministack has changed
574 headerIn = runLoader->GetHeader();
588 fOut = tOut->GetCurrentFile();
596 printf("====================\n");