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