]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - PWG0/AliPWG0Helper.cxx
Fix for CMake
[u/mrichter/AliRoot.git] / PWG0 / AliPWG0Helper.cxx
index 5466eec538dace8ceeb9d4bd0dc6e1a6a90bcab3..bd5a9ad6a7d8b0e5d8b9c2341de0d00bed3a22e6 100644 (file)
 #include <TParticle.h>
 #include <TParticlePDG.h>
 #include <TH1.h>
+#include <TH2.h>
 #include <TH3.h>
+#include <TList.h>
+#include <TTree.h>
+#include <TBranch.h>
+#include <TLeaf.h>
+
+#include <AliHeader.h>
+#include <AliStack.h>
 
 #include <AliLog.h>
 #include <AliESD.h>
+#include <AliESDEvent.h>
 #include <AliESDVertex.h>
+#include <AliVertexerTracks.h>
+#include <AliMultiplicity.h>
+
+#include <AliGenEventHeader.h>
+#include <AliGenPythiaEventHeader.h>
+#include <AliGenCocktailEventHeader.h>
+#include <AliGenDPMjetEventHeader.h>
+#include <AliESDVZERO.h>
+
+#include <AliOfflineTrigger.h>
 
 //____________________________________________________________________
 ClassImp(AliPWG0Helper)
 
+Int_t AliPWG0Helper::fgLastProcessType = -1;
+AliOfflineTrigger* AliPWG0Helper::fgOfflineTrigger = 0;
+
+//____________________________________________________________________
+AliOfflineTrigger* AliPWG0Helper::GetOfflineTrigger()
+{
+  // returns the current AliOfflineTrigger object
+  // creates one if needed
+  
+  if (!fgOfflineTrigger)
+    fgOfflineTrigger = new AliOfflineTrigger;
+    
+  return fgOfflineTrigger;
+}
+
 //____________________________________________________________________
-Bool_t AliPWG0Helper::IsEventTriggered(AliESD* aEsd, Trigger trigger)
+Bool_t AliPWG0Helper::IsEventTriggered(const AliESDEvent* aEsd, Trigger trigger)
+{
+  // checks if an event has been triggered
+
+  if (trigger & kOfflineFlag)
+    return GetOfflineTrigger()->IsEventTriggered(aEsd, trigger);
+    
+  return IsEventTriggered(aEsd->GetTriggerMask(), trigger);
+}
+
+//____________________________________________________________________
+Bool_t AliPWG0Helper::IsEventTriggered(ULong64_t triggerMask, Trigger trigger)
 {
   // check if the event was triggered
   //
   // this function needs the branch fTriggerMask
-  //
-  // MB should be
-  // ITS_SPD_GFO_L0  : 32
-  // VZERO_OR_LEFT   : 1
-  // VZERO_OR_RIGHT  : 2
-
-  ULong64_t triggerMask = aEsd->GetTriggerMask();
+  
+  // definitions from p-p.cfg
+  ULong64_t spdFO = (1 << 14);
+  ULong64_t v0left = (1 << 10);
+  ULong64_t v0right = (1 << 11);
 
   switch (trigger)
   {
+    case kAcceptAll:
+    {
+      return kTRUE;
+      break;
+    }
     case kMB1:
     {
-      if (triggerMask&32 || ((triggerMask&1) || (triggerMask&2)))
+      if (triggerMask & spdFO || ((triggerMask & v0left) || (triggerMask & v0right)))
         return kTRUE;
       break;
     }
     case kMB2:
     {
-      if (triggerMask&32 && ((triggerMask&1) || (triggerMask&2)))
+      if (triggerMask & spdFO && ((triggerMask & v0left) || (triggerMask & v0right)))
         return kTRUE;
       break;
     }
+    case kMB3:
+    {
+      if (triggerMask & spdFO && (triggerMask & v0left) && (triggerMask & v0right))
+        return kTRUE;
+      break;
+    }
+    case kSPDGFO:
+    {
+      if (triggerMask & spdFO)
+        return kTRUE;
+      break;
+    }
+    default:
+      Printf("IsEventTriggered: ERROR: Trigger type %d not implemented in this method", (Int_t) trigger);
+      break;
   }
 
   return kFALSE;
 }
 
 //____________________________________________________________________
-Bool_t AliPWG0Helper::IsVertexReconstructed(AliESD* aEsd)
+Bool_t AliPWG0Helper::TestVertex(const AliESDVertex* vertex, AnalysisMode analysisMode, Bool_t debug)
 {
-  // checks if the vertex is reasonable
-  //
-  // this function needs the branches fSPDVertex*
+    // Checks if a vertex meets the needed quality criteria
 
+  Float_t requiredZResolution = -1;
+  if (analysisMode & kSPD || analysisMode & kTPCITS)
+  {
+    requiredZResolution = 0.1;
+  }
+  else if (analysisMode & kTPC)
+    requiredZResolution = 10.;
 
-  const AliESDVertex* vtxESD = aEsd->GetVertex();
+  // check resolution
+  Double_t zRes = vertex->GetZRes();
 
-  // the vertex should be reconstructed
-  if (strcmp(vtxESD->GetName(), "default")==0)
+  if (zRes > requiredZResolution) {
+    if (debug)
+      Printf("AliPWG0Helper::TestVertex: Resolution too poor %f (required: %f", zRes, requiredZResolution);
     return kFALSE;
+  }
 
-  Double_t vtx_res[3];
-  vtx_res[0] = vtxESD->GetXRes();
-  vtx_res[1] = vtxESD->GetYRes();
-  vtx_res[2] = vtxESD->GetZRes();
+  return kTRUE;
+}
 
-  if (vtx_res[2]==0 || vtx_res[2]>0.1)
-    return kFALSE;
+//____________________________________________________________________
+const AliESDVertex* AliPWG0Helper::GetVertex(AliESDEvent* aEsd, AnalysisMode analysisMode, Bool_t debug, Bool_t bRedoTPC)
+{
+  // Get the vertex from the ESD and returns it if the vertex is valid
+  //
+  // Second argument decides which vertex is used (this selects
+  // also the quality criteria that are applied)
 
-  // check Ncontributors, if <0 it means error *gna*
+  const AliESDVertex* vertex = 0;
+  if (analysisMode & kSPD || analysisMode & kTPCITS)
+  {
+    vertex = aEsd->GetPrimaryVertexSPD();
+    if (debug)
+      Printf("AliPWG0Helper::GetVertex: Returning SPD vertex");
+  }
+  else if (analysisMode & kTPC)
+  {
+    if(bRedoTPC){
+      if (debug)
+        Printf("AliPWG0Helper::GetVertex: Redoing vertex");
+      Double_t kBz = aEsd->GetMagneticField();
+      AliVertexerTracks vertexer(kBz);
+      vertexer.SetTPCMode();
+      AliESDVertex *vTPC = vertexer.FindPrimaryVertex(aEsd);
+      aEsd->SetPrimaryVertexTPC(vTPC);
+      for (Int_t i=0; i<aEsd->GetNumberOfTracks(); i++) {
+       AliESDtrack *t = aEsd->GetTrack(i);
+       t->RelateToVertexTPC(vTPC, kBz, kVeryBig);
+      }
+      delete vTPC;
+    }
 
-  return kTRUE;
+    vertex = aEsd->GetPrimaryVertexTPC();
+    if (debug)
+      Printf("AliPWG0Helper::GetVertex: Returning vertex from tracks");
+  }
+  else
+    Printf("AliPWG0Helper::GetVertex: ERROR: Invalid second argument %d", analysisMode);
+
+  if (!vertex) {
+    if (debug)
+      Printf("AliPWG0Helper::GetVertex: No vertex found in ESD");
+    return 0;
+  }
+
+  // check Ncontributors
+  if (vertex->GetNContributors() <= 0) {
+    if (debug){
+      Printf("AliPWG0Helper::GetVertex: NContributors() <= 0: %d",vertex->GetNContributors());
+      Printf("AliPWG0Helper::GetVertex: NIndices(): %d",vertex->GetNIndices());
+      vertex->Print();
+    }
+    return 0;
+  }
+
+  // check resolution
+  Double_t zRes = vertex->GetZRes();
+  if (zRes == 0) {
+    Printf("AliPWG0Helper::GetVertex: UNEXPECTED: resolution is 0.");
+    return 0;
+  }
+
+  if (debug)
+  {
+    Printf("AliPWG0Helper::GetVertex: Returning valid vertex: %s", vertex->GetTitle());
+    vertex->Print();
+  }
+
+  return vertex;
 }
 
 //____________________________________________________________________
@@ -83,6 +215,8 @@ Bool_t AliPWG0Helper::IsPrimaryCharged(TParticle* aParticle, Int_t aTotalPrimari
   //
   // This function or a equivalent should be available in some common place of AliRoot
   //
+  // WARNING: Call this function only for particles that are among the particles from the event generator!
+  // --> stack->Particle(id) with id < stack->GetNprimary()
 
   // if the particle has a daughter primary, we do not want to count it
   if (aParticle->GetFirstDaughter() != -1 && aParticle->GetFirstDaughter() < aTotalPrimaries)
@@ -93,6 +227,7 @@ Bool_t AliPWG0Helper::IsPrimaryCharged(TParticle* aParticle, Int_t aTotalPrimari
   }
 
   Int_t pdgCode = TMath::Abs(aParticle->GetPdgCode());
+  
 
   // skip quarks and gluon
   if (pdgCode <= 10 || pdgCode == 21)
@@ -102,10 +237,17 @@ Bool_t AliPWG0Helper::IsPrimaryCharged(TParticle* aParticle, Int_t aTotalPrimari
     return kFALSE;
   }
 
+  Int_t status = aParticle->GetStatusCode();
+  // skip non final state particles..
+  if(status!=1){
+    if (adebug)
+      printf("Dropping particle because it is not a final state particle.\n");
+    return kFALSE;
+  }
+
   if (strcmp(aParticle->GetName(),"XXX") == 0)
   {
-    if (adebug)
-      printf("WARNING: There is a particle named XXX.\n");
+    Printf("WARNING: There is a particle named XXX (pdg code %d).", pdgCode);
     return kFALSE;
   }
 
@@ -113,8 +255,7 @@ Bool_t AliPWG0Helper::IsPrimaryCharged(TParticle* aParticle, Int_t aTotalPrimari
 
   if (strcmp(pdgPart->ParticleClass(),"Unknown") == 0)
   {
-    if (adebug)
-      printf("WARNING: There is a particle with an unknown particle class (pdg code %d).\n", pdgCode);
+    Printf("WARNING: There is a particle with an unknown particle class (pdg code %d).", pdgCode);
     return kFALSE;
   }
 
@@ -237,3 +378,271 @@ const char* AliPWG0Helper::GetAxisTitle(TH3* hist, const char axis)
 
   return 0;
 }
+
+
+AliPWG0Helper::MCProcessType AliPWG0Helper::GetPythiaEventProcessType(AliGenEventHeader* aHeader, Bool_t adebug) {
+
+  AliGenPythiaEventHeader* pythiaGenHeader = dynamic_cast<AliGenPythiaEventHeader*>(aHeader);
+
+  if (!pythiaGenHeader) {
+    printf("AliPWG0Helper::GetProcessType : Unknown gen Header type). \n");
+    return kInvalidProcess;
+  }
+
+
+  Int_t pythiaType = pythiaGenHeader->ProcessType();
+  fgLastProcessType = pythiaType;
+  MCProcessType globalType = kInvalidProcess;  
+
+
+  if (adebug) {
+    printf("AliPWG0Helper::GetProcessType : Pythia process type found: %d \n",pythiaType);
+  }
+
+
+  if(pythiaType==92||pythiaType==93){
+    globalType = kSD;
+  }
+  else if(pythiaType==94){
+    globalType = kDD;
+  }
+  //else if(pythiaType != 91){ // also exclude elastic to be sure... CKB??
+  else {
+    globalType = kND;
+  }
+  return globalType;
+}
+
+
+AliPWG0Helper::MCProcessType AliPWG0Helper::GetDPMjetEventProcessType(AliGenEventHeader* aHeader, Bool_t adebug) {
+  //
+  // get the process type of the event.
+  //
+
+  // can only read pythia headers, either directly or from cocktalil header
+  AliGenDPMjetEventHeader* dpmJetGenHeader = dynamic_cast<AliGenDPMjetEventHeader*>(aHeader);
+
+  if (!dpmJetGenHeader) {
+    printf("AliPWG0Helper::GetDPMjetProcessType : Unknown header type (not DPMjet or). \n");
+    return kInvalidProcess;
+  }
+
+  Int_t dpmJetType = dpmJetGenHeader->ProcessType();
+  fgLastProcessType = dpmJetType;
+  MCProcessType globalType = kInvalidProcess;  
+
+
+  if (adebug) {
+    printf("AliPWG0Helper::GetDPMJetProcessType : DPMJet process type found: %d \n",dpmJetType);
+  }
+
+
+  if(dpmJetType == 1){ // this is explicitly inelastic
+    globalType = kND;
+  }  
+  else if(dpmJetType==5||dpmJetType==6){
+    globalType = kSD;
+  }
+  else if(dpmJetType==7||dpmJetType==4){// DD and double pomeron
+    globalType = kDD;
+  }
+  return globalType;
+}
+
+
+AliPWG0Helper::MCProcessType AliPWG0Helper::GetEventProcessType(AliHeader* aHeader, Bool_t adebug) {
+  //
+  // get the process type of the event.
+  //
+
+
+  // Check for simple headers first
+
+  AliGenPythiaEventHeader* pythiaGenHeader = dynamic_cast<AliGenPythiaEventHeader*>(aHeader->GenEventHeader());
+  if (pythiaGenHeader) {
+    return GetPythiaEventProcessType(pythiaGenHeader,adebug);
+  }
+
+  AliGenDPMjetEventHeader* dpmJetGenHeader = dynamic_cast<AliGenDPMjetEventHeader*>(aHeader->GenEventHeader());
+  if (dpmJetGenHeader) {
+    return GetDPMjetEventProcessType(dpmJetGenHeader,adebug);
+  }
+  
+
+  // check for cocktail
+
+  AliGenCocktailEventHeader* genCocktailHeader = dynamic_cast<AliGenCocktailEventHeader*>(aHeader->GenEventHeader());
+  if (!genCocktailHeader) {
+    printf("AliPWG0Helper::GetProcessType : Unknown header type (not Pythia or Cocktail). \n");
+    return kInvalidProcess;
+  }
+
+  TList* headerList = genCocktailHeader->GetHeaders();
+  if (!headerList) {
+    return kInvalidProcess;
+  }
+
+  for (Int_t i=0; i<headerList->GetEntries(); i++) {
+
+    pythiaGenHeader = dynamic_cast<AliGenPythiaEventHeader*>(headerList->At(i));
+    if (pythiaGenHeader) {
+      return GetPythiaEventProcessType(pythiaGenHeader,adebug);
+    }
+
+    dpmJetGenHeader = dynamic_cast<AliGenDPMjetEventHeader*>(headerList->At(i));
+    if (dpmJetGenHeader) {
+      return GetDPMjetEventProcessType(dpmJetGenHeader,adebug);
+    }
+  }
+  return kInvalidProcess;
+}
+
+
+
+//____________________________________________________________________
+TParticle* AliPWG0Helper::FindPrimaryMother(AliStack* stack, Int_t label)
+{
+  //
+  // Finds the first mother among the primary particles of the particle identified by <label>,
+  // i.e. the primary that "caused" this particle
+  //
+
+  Int_t motherLabel = FindPrimaryMotherLabel(stack, label);
+  if (motherLabel < 0)
+    return 0;
+
+  return stack->Particle(motherLabel);
+}
+
+//____________________________________________________________________
+Int_t AliPWG0Helper::FindPrimaryMotherLabel(AliStack* stack, Int_t label)
+{
+  //
+  // Finds the first mother among the primary particles of the particle identified by <label>,
+  // i.e. the primary that "caused" this particle
+  //
+  // returns its label
+  //
+
+  Int_t nPrim  = stack->GetNprimary();
+
+  while (label >= nPrim)
+  {
+    //printf("Particle %d (pdg %d) is not a primary. Let's check its mother %d\n", label, mother->GetPdgCode(), mother->GetMother(0));
+
+    TParticle* particle = stack->Particle(label);
+    if (!particle)
+    {
+      AliDebugGeneral("FindPrimaryMother", AliLog::kError, Form("UNEXPECTED: particle with label %d not found in stack.", label));
+      return -1;
+    }
+    // find mother
+    if (particle->GetMother(0) < 0)
+    {
+      AliDebugGeneral("FindPrimaryMother", AliLog::kError, Form("UNEXPECTED: Could not find mother of secondary particle %d.", label));
+      return -1;
+    }
+
+    label = particle->GetMother(0);
+  }
+
+  return label;
+}
+
+//____________________________________________________________________
+void AliPWG0Helper::NormalizeToBinWidth(TH1* hist)
+{
+  //
+  // normalizes a 1-d histogram to its bin width
+  //
+
+  for (Int_t i=1; i<=hist->GetNbinsX(); ++i)
+  {
+    hist->SetBinContent(i, hist->GetBinContent(i) / hist->GetBinWidth(i));
+    hist->SetBinError(i, hist->GetBinError(i) / hist->GetBinWidth(i));
+  }
+}
+
+//____________________________________________________________________
+void AliPWG0Helper::NormalizeToBinWidth(TH2* hist)
+{
+  //
+  // normalizes a 2-d histogram to its bin width (x width * y width)
+  //
+
+  for (Int_t i=1; i<=hist->GetNbinsX(); ++i)
+    for (Int_t j=1; j<=hist->GetNbinsY(); ++j)
+    {
+      Double_t factor = hist->GetXaxis()->GetBinWidth(i) * hist->GetYaxis()->GetBinWidth(j);
+      hist->SetBinContent(i, j, hist->GetBinContent(i, j) / factor);
+      hist->SetBinError(i, j, hist->GetBinError(i, j) / factor);
+    }
+}
+
+//____________________________________________________________________
+const char* AliPWG0Helper::GetTriggerName(Trigger trigger) 
+{
+  // returns the name of the requested trigger
+  // the returned string will only be valid until the next call to this function [not thread-safe]
+  
+  static TString str;
+  
+  UInt_t triggerNoFlags = (UInt_t) trigger % (UInt_t) kStartOfFlags;
+  
+  switch (triggerNoFlags)
+  {
+    case kAcceptAll : str = "ACCEPT ALL (bypass!)"; break;
+    case kMB1 : str = "MB1"; break;
+    case kMB2 : str = "MB2"; break;
+    case kMB3 : str = "MB3"; break;
+    case kSPDGFO : str = "SPD GFO"; break;
+    case kV0A : str = "V0 A"; break;
+    case kV0C : str = "V0 C"; break;
+    case kZDC : str = "ZDC"; break;
+    case kZDCA : str = "ZDC A"; break;
+    case kZDCC : str = "ZDC C"; break;
+    case kFMDA : str = "FMD A"; break;
+    case kFMDC : str = "FMD C"; break;
+    case kFPANY : str = "SPD GFO | V0 | ZDC | FMD"; break;
+    default: str = ""; break;
+  }
+   
+  if (trigger & kOfflineFlag)
+    str += " OFFLINE";  
+  
+  return str;
+}
+
+//____________________________________________________________________
+void AliPWG0Helper::PrintConf(AnalysisMode analysisMode, Trigger trigger)
+{
+  //
+  // Prints the given configuration
+  //
+
+  TString str(">>>> Running with >");
+
+  if (analysisMode & kSPD)
+    str += "SPD-only";
+    
+  if (analysisMode & kTPC)
+     str += "TPC-only";
+    
+  if (analysisMode & kTPCITS)
+     str += "Global tracking";
+
+  if (analysisMode & kFieldOn)
+  {
+     str += " (with field)";
+  }
+  else
+     str += " (WITHOUT field)";
+  
+  str += "< and trigger >";
+  str += GetTriggerName(trigger);
+  str += "< <<<<";
+
+  Printf("%s", str.Data());
+}
+