]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWG0/AliPWG0Helper.cxx
new histograms added
[u/mrichter/AliRoot.git] / PWG0 / AliPWG0Helper.cxx
1 /* $Id$ */
2
3 #include <AliPWG0Helper.h>
4
5 #include <TParticle.h>
6 #include <TParticlePDG.h>
7 #include <TH1.h>
8 #include <TH2.h>
9 #include <TH3.h>
10 #include <TList.h>
11 #include <TTree.h>
12 #include <TBranch.h>
13 #include <TLeaf.h>
14
15 #include <AliHeader.h>
16 #include <AliStack.h>
17
18 #include <AliLog.h>
19 #include <AliESD.h>
20 #include <AliESDEvent.h>
21 #include <AliESDVertex.h>
22 #include <AliVertexerTracks.h>
23 #include <AliMultiplicity.h>
24
25 #include <AliGenEventHeader.h>
26 #include <AliGenPythiaEventHeader.h>
27 #include <AliGenCocktailEventHeader.h>
28 #include <AliGenDPMjetEventHeader.h>
29 #include <AliESDVZERO.h>
30
31 //____________________________________________________________________
32 ClassImp(AliPWG0Helper)
33
34 Int_t AliPWG0Helper::fgLastProcessType = -1;
35
36 //____________________________________________________________________
37 Bool_t AliPWG0Helper::TestVertex(const AliESDVertex* vertex, AnalysisMode analysisMode, Bool_t debug)
38 {
39     // Checks if a vertex meets the needed quality criteria
40
41   Float_t requiredZResolution = -1;
42   if (analysisMode & kSPD || analysisMode & kTPCITS)
43   {
44     // disable cut on resolution
45     requiredZResolution = 1000;
46   }
47   else if (analysisMode & kTPC)
48     requiredZResolution = 10.;
49
50   // check resolution
51   Double_t zRes = vertex->GetZRes();
52
53   if (zRes > requiredZResolution) {
54     if (debug)
55       Printf("AliPWG0Helper::TestVertex: Resolution too poor %f (required: %f", zRes, requiredZResolution);
56     return kFALSE;
57   }
58   
59   if (vertex->IsFromVertexerZ())
60   {
61     if (vertex->GetDispersion() > 0.02) 
62     {
63       if (debug)
64         Printf("AliPWG0Helper::TestVertex: Delta Phi too large in Vertexer Z: %f (required: %f", vertex->GetDispersion(), 0.02);
65       return kFALSE;
66     }
67   }
68
69   return kTRUE;
70 }
71
72 //____________________________________________________________________
73 const AliESDVertex* AliPWG0Helper::GetVertex(AliESDEvent* aEsd, AnalysisMode analysisMode, Bool_t debug)
74 {
75   // Get the vertex from the ESD and returns it if the vertex is valid
76   //
77   // Second argument decides which vertex is used (this selects
78   // also the quality criteria that are applied)
79
80   const AliESDVertex* vertex = 0;
81   if (analysisMode & kSPD || analysisMode & kTPCITS)
82   {
83     vertex = aEsd->GetPrimaryVertexSPD();
84     if (debug)
85       Printf("AliPWG0Helper::GetVertex: Returning SPD vertex");
86   }
87   else if (analysisMode & kTPC)
88   {
89     vertex = aEsd->GetPrimaryVertexTPC();
90     if (debug)
91       Printf("AliPWG0Helper::GetVertex: Returning vertex from tracks");
92   }
93   else
94     Printf("AliPWG0Helper::GetVertex: ERROR: Invalid second argument %d", analysisMode);
95
96   if (!vertex) {
97     if (debug)
98       Printf("AliPWG0Helper::GetVertex: No vertex found in ESD");
99     return 0;
100   }
101
102   // check Ncontributors
103   if (vertex->GetNContributors() <= 0) {
104     if (debug){
105       Printf("AliPWG0Helper::GetVertex: NContributors() <= 0: %d",vertex->GetNContributors());
106       Printf("AliPWG0Helper::GetVertex: NIndices(): %d",vertex->GetNIndices());
107       vertex->Print();
108     }
109     return 0;
110   }
111
112   // check resolution
113   Double_t zRes = vertex->GetZRes();
114   if (zRes == 0) {
115     Printf("AliPWG0Helper::GetVertex: UNEXPECTED: resolution is 0.");
116     return 0;
117   }
118
119   if (debug)
120   {
121     Printf("AliPWG0Helper::GetVertex: Returning valid vertex: %s", vertex->GetTitle());
122     vertex->Print();
123   }
124
125   return vertex;
126 }
127
128 //____________________________________________________________________
129 Bool_t AliPWG0Helper::IsPrimaryCharged(TParticle* aParticle, Int_t aTotalPrimaries, Bool_t adebug)
130 {
131   //
132   // this function checks if a particle from the event generator (i.e. among the nPrim particles in the stack)
133   // shall be counted as a primary particle
134   //
135   // This function or a equivalent should be available in some common place of AliRoot
136   //
137   // WARNING: Call this function only for particles that are among the particles from the event generator!
138   // --> stack->Particle(id) with id < stack->GetNprimary()
139
140   // if the particle has a daughter primary, we do not want to count it
141   if (aParticle->GetFirstDaughter() != -1 && aParticle->GetFirstDaughter() < aTotalPrimaries)
142   {
143     if (adebug)
144       printf("Dropping particle because it has a daughter among the primaries.\n");
145     return kFALSE;
146   }
147
148   Int_t pdgCode = TMath::Abs(aParticle->GetPdgCode());
149   
150
151   // skip quarks and gluon
152   if (pdgCode <= 10 || pdgCode == 21)
153   {
154     if (adebug)
155       printf("Dropping particle because it is a quark or gluon.\n");
156     return kFALSE;
157   }
158
159   Int_t status = aParticle->GetStatusCode();
160   // skip non final state particles..
161   if(status!=1){
162     if (adebug)
163       printf("Dropping particle because it is not a final state particle.\n");
164     return kFALSE;
165   }
166
167   if (strcmp(aParticle->GetName(),"XXX") == 0)
168   {
169     Printf("WARNING: There is a particle named XXX (pdg code %d).", pdgCode);
170     return kFALSE;
171   }
172
173   TParticlePDG* pdgPart = aParticle->GetPDG();
174
175   if (strcmp(pdgPart->ParticleClass(),"Unknown") == 0)
176   {
177     Printf("WARNING: There is a particle with an unknown particle class (pdg code %d).", pdgCode);
178     return kFALSE;
179   }
180
181   if (pdgPart->Charge() == 0)
182   {
183     if (adebug)
184       printf("Dropping particle because it is not charged.\n");
185     return kFALSE;
186   }
187
188   return kTRUE;
189 }
190
191 //____________________________________________________________________
192 void AliPWG0Helper::CreateProjections(TH3* hist, Bool_t save)
193 {
194   // create projections of 3d hists to all 2d combinations
195   // the histograms are not returned, just use them from memory or use this to create them in a file
196
197   TH1* proj = hist->Project3D("yx");
198   proj->SetXTitle(hist->GetXaxis()->GetTitle());
199   proj->SetYTitle(hist->GetYaxis()->GetTitle());
200   if (save)
201     proj->Write();
202
203   proj = hist->Project3D("zx");
204   proj->SetXTitle(hist->GetXaxis()->GetTitle());
205   proj->SetYTitle(hist->GetZaxis()->GetTitle());
206   if (save)
207     proj->Write();
208
209   proj = hist->Project3D("zy");
210   proj->SetXTitle(hist->GetYaxis()->GetTitle());
211   proj->SetYTitle(hist->GetZaxis()->GetTitle());
212   if (save)
213     proj->Write();
214 }
215
216 //____________________________________________________________________
217 void AliPWG0Helper::CreateDividedProjections(TH3* hist, TH3* hist2, const char* axis, Bool_t putErrors, Bool_t save)
218 {
219   // create projections of the 3d hists divides them
220   // axis decides to which plane, if axis is 0 to all planes
221   // the histograms are not returned, just use them from memory or use this to create them in a file
222
223   if (axis == 0)
224   {
225     CreateDividedProjections(hist, hist2, "yx", putErrors, save);
226     CreateDividedProjections(hist, hist2, "zx", putErrors, save);
227     CreateDividedProjections(hist, hist2, "zy", putErrors, save);
228
229     return;
230   }
231
232   TH1* proj = hist->Project3D(axis);
233
234   if (strlen(axis) == 2)
235   {
236     proj->SetYTitle(GetAxisTitle(hist, axis[0]));
237     proj->SetXTitle(GetAxisTitle(hist, axis[1]));
238   }
239   else if (strlen(axis) == 1)
240     proj->SetXTitle(GetAxisTitle(hist, axis[0]));
241
242   TH1* proj2 = hist2->Project3D(axis);
243   if (strlen(axis) == 2)
244   {
245     proj2->SetYTitle(GetAxisTitle(hist2, axis[0]));
246     proj2->SetXTitle(GetAxisTitle(hist2, axis[1]));
247   }
248   else if (strlen(axis) == 1)
249     proj2->SetXTitle(GetAxisTitle(hist2, axis[0]));
250
251   TH1* division = dynamic_cast<TH1*> (proj->Clone(Form("%s_div_%s", proj->GetName(), proj2->GetName())));
252   //printf("doing axis: %s, x axis has %d %d bins, min %f %f max %f %f\n", axis, division->GetNbinsX(), proj2->GetNbinsX(), division->GetXaxis()->GetBinLowEdge(1), proj2->GetXaxis()->GetBinLowEdge(1), division->GetXaxis()->GetBinUpEdge(division->GetNbinsX()), proj2->GetXaxis()->GetBinUpEdge(proj2->GetNbinsX()));
253   //printf("doing axis: %s, y axis has %d %d bins, min %f %f max %f %f\n", axis, division->GetNbinsY(), proj2->GetNbinsY(), division->GetYaxis()->GetBinLowEdge(1), proj2->GetYaxis()->GetBinLowEdge(1), division->GetYaxis()->GetBinUpEdge(division->GetNbinsY()), proj2->GetYaxis()->GetBinUpEdge(proj2->GetNbinsY()));
254   division->Divide(proj, proj2, 1, 1, "B");
255   division->SetTitle(Form("%s divided %s", proj->GetTitle(), proj2->GetTitle()));
256
257   if (putErrors)
258   {
259     division->Sumw2();
260     if (division->GetDimension() == 1)
261     {
262       Int_t nBins = division->GetNbinsX();
263       for (Int_t i = 1; i <= nBins; ++i)
264         if (proj2->GetBinContent(i) != 0)
265           division->SetBinError(i, TMath::Sqrt(proj->GetBinContent(i)) / proj2->GetBinContent(i));
266     }
267     else if (division->GetDimension() == 2)
268     {
269       Int_t nBinsX = division->GetNbinsX();
270       Int_t nBinsY = division->GetNbinsY();
271       for (Int_t i = 1; i <= nBinsX; ++i)
272         for (Int_t j = 1; j <= nBinsY; ++j)
273           if (proj2->GetBinContent(i, j) != 0)
274             division->SetBinError(i, j, TMath::Sqrt(proj->GetBinContent(i, j)) / proj2->GetBinContent(i, j));
275     }
276   }
277
278   if (save)
279   {
280     proj->Write();
281     proj2->Write();
282     division->Write();
283   }
284 }
285
286 //____________________________________________________________________
287 const char* AliPWG0Helper::GetAxisTitle(TH3* hist, const char axis)
288 {
289   // returns the title of the axis given in axis (x, y, z)
290
291   if (axis == 'x')
292     return hist->GetXaxis()->GetTitle();
293   else if (axis == 'y')
294     return hist->GetYaxis()->GetTitle();
295   else if (axis == 'z')
296     return hist->GetZaxis()->GetTitle();
297
298   return 0;
299 }
300
301
302 AliPWG0Helper::MCProcessType AliPWG0Helper::GetPythiaEventProcessType(AliGenEventHeader* aHeader, Bool_t adebug) {
303
304   AliGenPythiaEventHeader* pythiaGenHeader = dynamic_cast<AliGenPythiaEventHeader*>(aHeader);
305
306   if (!pythiaGenHeader) {
307     printf("AliPWG0Helper::GetProcessType : Unknown gen Header type). \n");
308     return kInvalidProcess;
309   }
310
311
312   Int_t pythiaType = pythiaGenHeader->ProcessType();
313   fgLastProcessType = pythiaType;
314   MCProcessType globalType = kInvalidProcess;  
315
316
317   if (adebug) {
318     printf("AliPWG0Helper::GetProcessType : Pythia process type found: %d \n",pythiaType);
319   }
320
321
322   if(pythiaType==92||pythiaType==93){
323     globalType = kSD;
324   }
325   else if(pythiaType==94){
326     globalType = kDD;
327   }
328   //else if(pythiaType != 91){ // also exclude elastic to be sure... CKB??
329   else {
330     globalType = kND;
331   }
332   return globalType;
333 }
334
335
336 AliPWG0Helper::MCProcessType AliPWG0Helper::GetDPMjetEventProcessType(AliGenEventHeader* aHeader, Bool_t adebug) {
337   //
338   // get the process type of the event.
339   //
340
341   // can only read pythia headers, either directly or from cocktalil header
342   AliGenDPMjetEventHeader* dpmJetGenHeader = dynamic_cast<AliGenDPMjetEventHeader*>(aHeader);
343
344   if (!dpmJetGenHeader) {
345     printf("AliPWG0Helper::GetDPMjetProcessType : Unknown header type (not DPMjet or). \n");
346     return kInvalidProcess;
347   }
348
349   Int_t dpmJetType = dpmJetGenHeader->ProcessType();
350   fgLastProcessType = dpmJetType;
351   MCProcessType globalType = kInvalidProcess;  
352
353
354   if (adebug) {
355     printf("AliPWG0Helper::GetDPMJetProcessType : DPMJet process type found: %d \n",dpmJetType);
356   }
357
358
359   if(dpmJetType == 1){ // this is explicitly inelastic
360     globalType = kND;
361   }  
362   else if(dpmJetType==5||dpmJetType==6){
363     globalType = kSD;
364   }
365   else if(dpmJetType==7||dpmJetType==4){// DD and double pomeron
366     globalType = kDD;
367   }
368   return globalType;
369 }
370
371
372 AliPWG0Helper::MCProcessType AliPWG0Helper::GetEventProcessType(AliHeader* aHeader, Bool_t adebug) {
373   //
374   // get the process type of the event.
375   //
376
377
378   // Check for simple headers first
379
380   AliGenPythiaEventHeader* pythiaGenHeader = dynamic_cast<AliGenPythiaEventHeader*>(aHeader->GenEventHeader());
381   if (pythiaGenHeader) {
382     return GetPythiaEventProcessType(pythiaGenHeader,adebug);
383   }
384
385   AliGenDPMjetEventHeader* dpmJetGenHeader = dynamic_cast<AliGenDPMjetEventHeader*>(aHeader->GenEventHeader());
386   if (dpmJetGenHeader) {
387     return GetDPMjetEventProcessType(dpmJetGenHeader,adebug);
388   }
389   
390
391   // check for cocktail
392
393   AliGenCocktailEventHeader* genCocktailHeader = dynamic_cast<AliGenCocktailEventHeader*>(aHeader->GenEventHeader());
394   if (!genCocktailHeader) {
395     printf("AliPWG0Helper::GetProcessType : Unknown header type (not Pythia or Cocktail). \n");
396     return kInvalidProcess;
397   }
398
399   TList* headerList = genCocktailHeader->GetHeaders();
400   if (!headerList) {
401     return kInvalidProcess;
402   }
403
404   for (Int_t i=0; i<headerList->GetEntries(); i++) {
405
406     pythiaGenHeader = dynamic_cast<AliGenPythiaEventHeader*>(headerList->At(i));
407     if (pythiaGenHeader) {
408       return GetPythiaEventProcessType(pythiaGenHeader,adebug);
409     }
410
411     dpmJetGenHeader = dynamic_cast<AliGenDPMjetEventHeader*>(headerList->At(i));
412     if (dpmJetGenHeader) {
413       return GetDPMjetEventProcessType(dpmJetGenHeader,adebug);
414     }
415   }
416   return kInvalidProcess;
417 }
418
419
420
421 //____________________________________________________________________
422 TParticle* AliPWG0Helper::FindPrimaryMother(AliStack* stack, Int_t label)
423 {
424   //
425   // Finds the first mother among the primary particles of the particle identified by <label>,
426   // i.e. the primary that "caused" this particle
427   //
428
429   Int_t motherLabel = FindPrimaryMotherLabel(stack, label);
430   if (motherLabel < 0)
431     return 0;
432
433   return stack->Particle(motherLabel);
434 }
435
436 //____________________________________________________________________
437 Int_t AliPWG0Helper::FindPrimaryMotherLabel(AliStack* stack, Int_t label)
438 {
439   //
440   // Finds the first mother among the primary particles of the particle identified by <label>,
441   // i.e. the primary that "caused" this particle
442   //
443   // returns its label
444   //
445
446   Int_t nPrim  = stack->GetNprimary();
447
448   while (label >= nPrim)
449   {
450     //printf("Particle %d (pdg %d) is not a primary. Let's check its mother %d\n", label, mother->GetPdgCode(), mother->GetMother(0));
451
452     TParticle* particle = stack->Particle(label);
453     if (!particle)
454     {
455       AliDebugGeneral("FindPrimaryMother", AliLog::kError, Form("UNEXPECTED: particle with label %d not found in stack.", label));
456       return -1;
457     }
458  
459     // find mother
460     if (particle->GetMother(0) < 0)
461     {
462       AliDebugGeneral("FindPrimaryMother", AliLog::kError, Form("UNEXPECTED: Could not find mother of secondary particle %d.", label));
463       return -1;
464     }
465
466     label = particle->GetMother(0);
467   }
468
469   return label;
470 }
471
472 //____________________________________________________________________
473 void AliPWG0Helper::NormalizeToBinWidth(TH1* hist)
474 {
475   //
476   // normalizes a 1-d histogram to its bin width
477   //
478
479   for (Int_t i=1; i<=hist->GetNbinsX(); ++i)
480   {
481     hist->SetBinContent(i, hist->GetBinContent(i) / hist->GetBinWidth(i));
482     hist->SetBinError(i, hist->GetBinError(i) / hist->GetBinWidth(i));
483   }
484 }
485
486 //____________________________________________________________________
487 void AliPWG0Helper::NormalizeToBinWidth(TH2* hist)
488 {
489   //
490   // normalizes a 2-d histogram to its bin width (x width * y width)
491   //
492
493   for (Int_t i=1; i<=hist->GetNbinsX(); ++i)
494     for (Int_t j=1; j<=hist->GetNbinsY(); ++j)
495     {
496       Double_t factor = hist->GetXaxis()->GetBinWidth(i) * hist->GetYaxis()->GetBinWidth(j);
497       hist->SetBinContent(i, j, hist->GetBinContent(i, j) / factor);
498       hist->SetBinError(i, j, hist->GetBinError(i, j) / factor);
499     }
500 }
501
502 //____________________________________________________________________
503 void AliPWG0Helper::PrintConf(AnalysisMode analysisMode, AliTriggerAnalysis::Trigger trigger)
504 {
505   //
506   // Prints the given configuration
507   //
508
509   TString str(">>>> Running with >");
510
511   if (analysisMode & kSPD)
512     str += "SPD-only";
513     
514   if (analysisMode & kSPDOnlyL0)
515     str += " (only L0 clusters)";
516   
517   if (analysisMode & kTPC)
518      str += "TPC-only";
519     
520   if (analysisMode & kTPCITS)
521      str += "Global tracking";
522
523   if (analysisMode & kFieldOn)
524   {
525      str += " (with field)";
526   }
527   else
528      str += " (WITHOUT field)";
529   
530   str += "< and trigger >";
531   str += AliTriggerAnalysis::GetTriggerName(trigger);
532   str += "< <<<<";
533
534   Printf("%s", str.Data());
535 }
536