3 #include "AliMultiplicityMCSelector.h"
13 #include <TParticle.h>
16 #include <TObjString.h>
22 #include <AliHeader.h>
23 #include <AliGenEventHeader.h>
24 #include <AliMultiplicity.h>
26 #include "esdTrackCuts/AliESDtrackCuts.h"
27 #include "AliPWG0Helper.h"
28 #include "dNdEta/AliMultiplicityCorrection.h"
29 #include "AliCorrection.h"
30 #include "AliCorrectionMatrix3D.h"
32 #define TPCMEASUREMENT
33 //#define ITSMEASUREMENT
35 ClassImp(AliMultiplicityMCSelector)
37 AliMultiplicityMCSelector::AliMultiplicityMCSelector() :
41 fSystSkipParticles(kFALSE),
42 fSelectProcessType(0),
47 // Constructor. Initialization of pointers
50 for (Int_t i = 0; i<4; i++)
51 fParticleCorrection[i] = 0;
54 AliMultiplicityMCSelector::~AliMultiplicityMCSelector()
60 // histograms are in the output list and deleted when the output
61 // list is deleted by the TSelector dtor
64 void AliMultiplicityMCSelector::Begin(TTree* tree)
68 AliSelectorRL::Begin(tree);
70 ReadUserObjects(tree);
73 void AliMultiplicityMCSelector::ReadUserObjects(TTree* tree)
75 // read the user objects, called from slavebegin and begin
77 if (!fEsdTrackCuts && fInput)
78 fEsdTrackCuts = dynamic_cast<AliESDtrackCuts*> (fInput->FindObject("AliESDtrackCuts"));
80 if (!fEsdTrackCuts && tree)
81 fEsdTrackCuts = dynamic_cast<AliESDtrackCuts*> (tree->GetUserInfo()->FindObject("AliESDtrackCuts"));
84 AliDebug(AliLog::kError, "ERROR: Could not read EsdTrackCuts from input list.");
86 if (!fPtSpectrum && fInput)
87 fPtSpectrum = dynamic_cast<TH1*> (fInput->FindObject("pt-spectrum"));
89 if (!fPtSpectrum && tree)
90 fPtSpectrum = dynamic_cast<TH1*> (tree->GetUserInfo()->FindObject("pt-spectrum"));
93 void AliMultiplicityMCSelector::SlaveBegin(TTree* tree)
95 // The SlaveBegin() function is called after the Begin() function.
96 // When running with PROOF SlaveBegin() is called on each slave server.
97 // The tree argument is deprecated (on PROOF 0 is passed).
99 AliSelector::SlaveBegin(tree);
101 ReadUserObjects(tree);
103 fMultiplicity = new AliMultiplicityCorrection("Multiplicity", "Multiplicity");
105 TString option(GetOption());
106 if (option.Contains("skip-particles"))
108 fSystSkipParticles = kTRUE;
109 AliInfo("WARNING: Systematic study enabled. Particles will be skipped.");
112 if (option.Contains("particle-efficiency"))
113 for (Int_t i = 0; i<4; i++)
114 fParticleCorrection[i] = new AliCorrection(Form("correction_%d", i), Form("correction_%d", i));
116 if (option.Contains("only-process-type-nd"))
117 fSelectProcessType = 1;
119 if (option.Contains("only-process-type-sd"))
120 fSelectProcessType = 2;
122 if (option.Contains("only-process-type-dd"))
123 fSelectProcessType = 3;
125 if (fSelectProcessType != 0)
126 AliInfo(Form("WARNING: Systematic study enabled. Only considering process type %d", fSelectProcessType));
128 if (option.Contains("pt-spectrum-hist"))
130 TFile* file = TFile::Open("ptspectrum_fit.root");
133 TString subStr(option(option.Index("pt-spectrum")+17, 3));
134 TString histName(Form("ptspectrum_%s", subStr.Data()));
135 AliInfo(Form("Pt-Spectrum modification. Using %s.", histName.Data()));
136 fPtSpectrum = (TH1*) file->Get(histName);
138 AliError("Histogram not found");
141 AliError("Could not open ptspectrum_fit.root. Pt Spectrum study could not be enabled.");
144 AliInfo("WARNING: Systematic study enabled. Pt spectrum will be modified");
147 if (option.Contains("pt-spectrum-func"))
151 Printf("Using function from input list for systematic p_t study");
155 fPtSpectrum = new TH1F("fPtSpectrum", "fPtSpectrum", 1, 0, 100);
156 fPtSpectrum->SetBinContent(1, 1);
160 AliInfo("WARNING: Systematic study enabled. Pt spectrum will be modified");
163 if (option.Contains("particle-species"))
164 fParticleSpecies = new TNtuple("fParticleSpecies", "fParticleSpecies", "vtx:Pi_True:K_True:p_True:o_True:Pi_Rec:K_Rec:p_Rec:o_Rec:nolabel:doublePrim:doubleCount");
166 // TODO set seed for random generator
169 void AliMultiplicityMCSelector::Init(TTree* tree)
171 // read the user objects
173 AliSelectorRL::Init(tree);
175 // enable only the needed branches
178 tree->SetBranchStatus("*", 0);
179 tree->SetBranchStatus("fTriggerMask", 1);
180 tree->SetBranchStatus("fSPDVertex*", 1);
182 #ifdef ITSMEASUREMENT
183 tree->SetBranchStatus("fSPDMult*", 1);
186 #ifdef TPCMEASUREMENT
187 AliESDtrackCuts::EnableNeededBranches(tree);
188 tree->SetBranchStatus("fTracks.fLabel", 1);
191 tree->SetCacheSize(0);
195 Bool_t AliMultiplicityMCSelector::Process(Long64_t entry)
197 // The Process() function is called for each entry in the tree (or possibly
198 // keyed object in the case of PROOF) to be processed. The entry argument
199 // specifies which entry in the currently loaded tree is to be processed.
200 // It can be passed to either TTree::GetEntry() or TBranch::GetEntry()
201 // to read either all or the required parts of the data. When processing
202 // keyed objects with PROOF, the object is already loaded and is available
203 // via the fObject pointer.
205 // This function should contain the "body" of the analysis. It can contain
206 // simple or elaborate selection criteria, run algorithms on the data
207 // of the event and typically fill histograms.
209 // WARNING when a selector is used with a TChain, you must use
210 // the pointer to the current TTree to call GetEntry(entry).
211 // The entry is always the local entry number in the current tree.
212 // Assuming that fTree is the pointer to the TChain being processed,
213 // use fTree->GetTree()->GetEntry(entry).
215 if (AliSelectorRL::Process(entry) == kFALSE)
218 // Check prerequisites
221 AliDebug(AliLog::kError, "ESD branch not available");
227 AliDebug(AliLog::kError, "fESDTrackCuts not available");
231 AliHeader* header = GetHeader();
234 AliDebug(AliLog::kError, "Header not available");
238 AliStack* stack = GetStack();
241 AliDebug(AliLog::kError, "Stack not available");
245 if (fSelectProcessType > 0)
247 // getting process information; NB: this only works for Pythia
248 Int_t processtype = AliPWG0Helper::GetPythiaEventProcessType(header);
250 Bool_t processEvent = kFALSE;
253 if (fSelectProcessType == 1 && processtype !=92 && processtype !=93 && processtype != 94)
254 processEvent = kTRUE;
256 // single diffractive
257 if (fSelectProcessType == 2 && (processtype == 92 || processtype == 93))
258 processEvent = kTRUE;
260 // double diffractive
261 if (fSelectProcessType == 3 && processtype == 94)
262 processEvent = kTRUE;
266 AliDebug(AliLog::kDebug, Form("Skipping this event, because it is not of the requested process type (%d)", processtype));
271 Bool_t eventTriggered = AliPWG0Helper::IsEventTriggered(fESD);
272 eventTriggered = kTRUE; // no generated trigger for the simulated events
273 Bool_t eventVertex = AliPWG0Helper::IsVertexReconstructed(fESD);
275 // get the ESD vertex
276 const AliESDVertex* vtxESD = fESD->GetVertex();
281 AliGenEventHeader* genHeader = header->GenEventHeader();
283 genHeader->PrimaryVertex(vtxMC);
286 const AliMultiplicity* mult = fESD->GetMultiplicity();
289 AliDebug(AliLog::kError, "AliMultiplicity not available");
293 //const Float_t kPtCut = 0.3;
295 // get number of tracks from MC
296 // loop over mc particles
297 Int_t nPrim = stack->GetNprimary();
298 Int_t nMCPart = stack->GetNtrack();
300 // tracks in different eta ranges
301 Int_t nMCTracks05 = 0;
302 Int_t nMCTracks09 = 0;
303 Int_t nMCTracks15 = 0;
304 Int_t nMCTracks20 = 0;
305 Int_t nMCTracksAll = 0;
307 // tracks per particle species, in |eta| < 2 (systematic study)
308 Int_t nMCTracksSpecies[4]; // (pi, K, p, other)
309 for (Int_t i = 0; i<4; ++i)
310 nMCTracksSpecies[i] = 0;
311 // eta range for nMCTracksSpecies, nESDTracksSpecies
312 #ifdef ITSMEASUREMENT
313 const Float_t etaRange = 2.0;
315 const Float_t etaRange = 0.9;
318 for (Int_t iMc = 0; iMc < nPrim; ++iMc)
320 AliDebug(AliLog::kDebug+1, Form("MC Loop: Processing particle %d.", iMc));
322 TParticle* particle = stack->Particle(iMc);
326 AliDebug(AliLog::kError, Form("UNEXPECTED: particle with label %d not found in stack (mc loop).", iMc));
330 Bool_t debug = kFALSE;
331 if (AliPWG0Helper::IsPrimaryCharged(particle, nPrim, debug) == kFALSE)
333 //printf("%d) DROPPED ", iMC);
338 //printf("%d) OK ", iMC);
341 //if (particle->Pt() < kPtCut)
344 Int_t particleWeight = 1;
346 Float_t pt = particle->Pt();
348 // in case of systematic study, weight according to the change of the pt spectrum
349 // it cannot be just multiplied because we cannot count "half of a particle"
350 // instead a random generator decides if the particle is counted twice (if value > 1)
351 // or not (if value < 0)
354 Int_t bin = fPtSpectrum->FindBin(pt);
355 if (bin > 0 && bin <= fPtSpectrum->GetNbinsX())
357 Float_t factor = fPtSpectrum->GetBinContent(bin);
360 Float_t random = gRandom->Uniform();
361 if (factor > 1 && random < factor - 1)
365 else if (factor < 1 && random < 1 - factor)
371 //Printf("MC weight is: %d", particleWeight);
373 if (TMath::Abs(particle->Eta()) < 0.5)
374 nMCTracks05 += particleWeight;
376 if (TMath::Abs(particle->Eta()) < 0.9)
377 nMCTracks09 += particleWeight;
379 if (TMath::Abs(particle->Eta()) < 1.5)
380 nMCTracks15 += particleWeight;
382 if (TMath::Abs(particle->Eta()) < 2.0)
383 nMCTracks20 += particleWeight;
385 nMCTracksAll += particleWeight;
387 if (fParticleCorrection[0] || fParticleSpecies)
390 switch (TMath::Abs(particle->GetPdgCode()))
392 case 211: id = 0; break;
393 case 321: id = 1; break;
394 case 2212: id = 2; break;
395 default: id = 3; break;
397 if (TMath::Abs(particle->Eta()) < etaRange)
398 nMCTracksSpecies[id]++;
399 if (fParticleCorrection[id])
400 fParticleCorrection[id]->GetTrackCorrection()->FillGene(vtxMC[2], particle->Eta(), particle->Pt());
402 }// end of mc particle
404 fMultiplicity->FillGenerated(vtxMC[2], eventTriggered, eventVertex, (Int_t) nMCTracks05, (Int_t) nMCTracks09, (Int_t) nMCTracks15, (Int_t) nMCTracks20, (Int_t) nMCTracksAll);
406 if (!eventTriggered || !eventVertex)
409 Int_t nESDTracks05 = 0;
410 Int_t nESDTracks09 = 0;
411 Int_t nESDTracks15 = 0;
412 Int_t nESDTracks20 = 0;
414 // tracks per particle species, in |eta| < 2 (systematic study)
415 Int_t nESDTracksSpecies[7]; // (pi, K, p, other, nolabel, doublecount_prim, doublecount_all)
416 for (Int_t i = 0; i<7; ++i)
417 nESDTracksSpecies[i] = 0;
419 Bool_t* foundPrimaries = new Bool_t[nPrim]; // to prevent double counting
420 for (Int_t i=0; i<nPrim; i++)
421 foundPrimaries[i] = kFALSE;
423 Bool_t* foundTracks = new Bool_t[nMCPart]; // to prevent double counting
424 for (Int_t i=0; i<nMCPart; i++)
425 foundTracks[i] = kFALSE;
427 #ifdef ITSMEASUREMENT
428 // get multiplicity from ITS tracklets
429 for (Int_t i=0; i<mult->GetNumberOfTracklets(); ++i)
431 //printf("%d %f %f %f\n", i, mult->GetTheta(i), mult->GetPhi(i), mult->GetDeltaPhi(i));
433 // This removes non-tracklets in PDC06 data. Very bad solution. New solution is implemented for newer data. Keeping this for compatibility.
434 if (mult->GetDeltaPhi(i) < -1000)
437 // systematic study: 10% lower efficiency
438 if (fSystSkipParticles && (gRandom->Uniform() < 0.1))
441 Float_t theta = mult->GetTheta(i);
442 Float_t eta = -TMath::Log(TMath::Tan(theta/2.));
444 // TODO define needed, because this only works with new AliRoot
445 Int_t label = mult->GetLabel(i);
447 #ifdef TPCMEASUREMENT
448 // get multiplicity from ESD tracks
449 TObjArray* list = fEsdTrackCuts->GetAcceptedTracks(fESD);
450 Int_t nGoodTracks = list->GetEntries();
451 // loop over esd tracks
452 for (Int_t i=0; i<nGoodTracks; i++)
454 AliESDtrack* esdTrack = dynamic_cast<AliESDtrack*> (list->At(i));
457 AliDebug(AliLog::kError, Form("ERROR: Could not retrieve track %d.", i));
462 esdTrack->GetConstrainedPxPyPz(p);
465 Float_t theta = vector.Theta();
466 Float_t eta = -TMath::Log(TMath::Tan(theta/2.));
468 Int_t label = esdTrack->GetLabel();
470 //Float_t pt = vector.Pt();
475 Int_t particleWeight = 1;
477 // in case of systematic study, weight according to the change of the pt spectrum
480 TParticle* mother = 0;
482 // preserve label for later
483 Int_t labelCopy = label;
485 labelCopy = AliPWG0Helper::FindPrimaryMotherLabel(stack, labelCopy);
487 mother = stack->Particle(labelCopy);
489 // in case of pt study we do not count particles w/o label, because they cannot be scaled
493 // it cannot be just multiplied because we cannot count "half of a particle"
494 // instead a random generator decides if the particle is counted twice (if value > 1)
495 // or not (if value < 0)
496 Int_t bin = fPtSpectrum->FindBin(mother->Pt());
497 if (bin > 0 && bin <= fPtSpectrum->GetNbinsX())
499 Float_t factor = fPtSpectrum->GetBinContent(bin);
502 Float_t random = gRandom->Uniform();
503 if (factor > 1 && random < factor - 1)
507 else if (factor < 1 && random < 1 - factor)
513 //Printf("ESD weight is: %d", particleWeight);
515 if (TMath::Abs(eta) < 0.5)
516 nESDTracks05 += particleWeight;
518 if (TMath::Abs(eta) < 0.9)
519 nESDTracks09 += particleWeight;
521 if (TMath::Abs(eta) < 1.5)
522 nESDTracks15 += particleWeight;
524 if (TMath::Abs(eta) < 2.0)
525 nESDTracks20 += particleWeight;
528 if (fParticleCorrection[0] || fParticleSpecies)
530 Int_t motherLabel = -1;
531 TParticle* mother = 0;
535 motherLabel = AliPWG0Helper::FindPrimaryMotherLabel(stack, label);
536 if (motherLabel >= 0)
537 mother = stack->Particle(motherLabel);
541 // count tracks that did not have a label
542 if (TMath::Abs(eta) < etaRange)
543 nESDTracksSpecies[4]++;
547 // get particle type (pion, proton, kaon, other)
549 switch (TMath::Abs(mother->GetPdgCode()))
551 case 211: id = 0; break;
552 case 321: id = 1; break;
553 case 2212: id = 2; break;
554 default: id = 3; break;
557 // double counting is ok for particle ratio study
558 if (TMath::Abs(eta) < etaRange)
559 nESDTracksSpecies[id]++;
561 // double counting is not ok for efficiency study
563 // check if we already counted this particle, this way distinguishes double counted particles (bug/artefact in tracking) or double counted primaries due to secondaries (physics)
564 if (foundTracks[label])
566 if (TMath::Abs(eta) < etaRange)
567 nESDTracksSpecies[6]++;
570 foundTracks[label] = kTRUE;
572 // particle (primary) already counted?
573 if (foundPrimaries[motherLabel])
575 if (TMath::Abs(eta) < etaRange)
576 nESDTracksSpecies[5]++;
579 foundPrimaries[motherLabel] = kTRUE;
581 if (fParticleCorrection[id])
582 fParticleCorrection[id]->GetTrackCorrection()->FillMeas(vtxMC[2], mother->Eta(), mother->Pt());
586 delete[] foundTracks;
587 delete[] foundPrimaries;
589 if ((Int_t) nMCTracks20 == 0 && nESDTracks20 > 3)
590 printf("WARNING: Event %lld has %d generated and %d reconstructed...\n", entry, nMCTracks20, nESDTracks20);
592 //printf("%.2f generated and %.2f reconstructed\n", nMCTracks20, nESDTracks20);
594 //Printf("%f %f %f %f %f %f %f %f %f %f", vtxMC[2], nMCTracks05, nMCTracks09, nMCTracks15, nMCTracks20, nMCTracksAll, nESDTracks05, nESDTracks09, nESDTracks15, nESDTracks20);
596 //Printf("%f %f %f %f %f %f %f %f %f %f", vtxMC[2], nMCTracks05, nMCTracks09, nMCTracks15, nMCTracks20, nMCTracksAll, nESDTracks05, nESDTracks09, nESDTracks15, nESDTracks20);
598 fMultiplicity->FillMeasured(vtx[2], nESDTracks05, nESDTracks09, nESDTracks15, nESDTracks20);
600 // fill response matrix using vtxMC (best guess)
601 fMultiplicity->FillCorrection(vtxMC[2], nMCTracks05, nMCTracks09, nMCTracks15, nMCTracks20, nMCTracksAll, nESDTracks05, nESDTracks09, nESDTracks15, nESDTracks20);
603 if (fParticleSpecies)
604 fParticleSpecies->Fill(vtxMC[2], nMCTracksSpecies[0], nMCTracksSpecies[1], nMCTracksSpecies[2], nMCTracksSpecies[3], nESDTracksSpecies[0], nESDTracksSpecies[1], nESDTracksSpecies[2], nESDTracksSpecies[3], nESDTracksSpecies[4], nESDTracksSpecies[5], nESDTracksSpecies[6]);
606 //Printf("%d = %d %d %d %d %d %d %d", (Int_t) nESDTracks20, nESDTracksSpecies[0], nESDTracksSpecies[1], nESDTracksSpecies[2], nESDTracksSpecies[3], nESDTracksSpecies[4], nESDTracksSpecies[5], nESDTracksSpecies[6]);
611 void AliMultiplicityMCSelector::SlaveTerminate()
613 // The SlaveTerminate() function is called after all entries or objects
614 // have been processed. When running with PROOF SlaveTerminate() is called
615 // on each slave server.
617 AliSelector::SlaveTerminate();
619 // Add the histograms to the output on each slave server
622 AliDebug(AliLog::kError, Form("ERROR: Output list not initialized."));
626 fOutput->Add(fMultiplicity);
627 for (Int_t i = 0; i < 4; ++i)
628 fOutput->Add(fParticleCorrection[i]);
630 fOutput->Add(fParticleSpecies);
633 void AliMultiplicityMCSelector::Terminate()
635 // The Terminate() function is the last function to be called during
636 // a query. It always runs on the client, it can be used to present
637 // the results graphically or save the results to file.
639 AliSelector::Terminate();
641 fMultiplicity = dynamic_cast<AliMultiplicityCorrection*> (fOutput->FindObject("Multiplicity"));
642 for (Int_t i = 0; i < 4; ++i)
643 fParticleCorrection[i] = dynamic_cast<AliCorrection*> (fOutput->FindObject(Form("correction_%d", i)));
644 fParticleSpecies = dynamic_cast<TNtuple*> (fOutput->FindObject("fParticleSpecies"));
648 AliDebug(AliLog::kError, Form("ERROR: Histograms not available %p", (void*) fMultiplicity));
652 TFile* file = TFile::Open("multiplicityMC.root", "RECREATE");
654 fMultiplicity->SaveHistograms();
655 for (Int_t i = 0; i < 4; ++i)
656 if (fParticleCorrection[i])
657 fParticleCorrection[i]->SaveHistograms();
658 if (fParticleSpecies)
659 fParticleSpecies->Write();
661 TObjString option(GetOption());
666 //fMultiplicity->DrawHistograms();