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