]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - STEER/AliTRDPIDResponse.cxx
Fixes for Coverity warnings
[u/mrichter/AliRoot.git] / STEER / AliTRDPIDResponse.cxx
index 0bd6aac6e1a4e3200525bf54768e1fd5bcd94a97..0616b683ba71fdb96ba967eb8b268a3d28b977e2 100644 (file)
 #include <TAxis.h>
 #include <TFile.h>
 #include <TH1.h>
+#include <TKey.h>
 #include <TObjArray.h>
 #include <TString.h>
+#include <TROOT.h> 
 #include <TSystem.h>
+#include <TDirectory.h>
 
 #include "AliLog.h"
 
@@ -42,19 +45,23 @@ const Double_t AliTRDPIDResponse::fgkPBins[kNPBins] = {1, 2, 3, 4, 5, 6};
 AliTRDPIDResponse::AliTRDPIDResponse(const Char_t * filename):
  TObject()
  ,fReferences(NULL)
+ ,fGainNormalisationFactor(1.)
  ,fPIDmethod(2)
 {
  //
  // Default constructor
  //
- memset(fMapRefHists, 0, sizeof(Int_t) * 10);
- Load(filename);
+  for(Int_t ispec = 0; ispec < AliPID::kSPECIES; ispec++)
+    for(Int_t ipbin = 0; ipbin < kNPBins; ipbin++)
+      fMapRefHists[ispec][ipbin] = -1;
+  Load(filename);
 }
 
 //____________________________________________________________
 AliTRDPIDResponse::AliTRDPIDResponse(const AliTRDPIDResponse &ref):
  TObject(ref)
  ,fReferences(ref.fReferences)
+ ,fGainNormalisationFactor(ref.fGainNormalisationFactor)
  ,fPIDmethod(ref.fPIDmethod)
 {
  //
@@ -62,7 +69,8 @@ AliTRDPIDResponse::AliTRDPIDResponse(const AliTRDPIDResponse &ref):
  // Flat copy of the reference histos
  // For creating a deep copy call SetOwner
  //
- memcpy(fMapRefHists, ref.fMapRefHists, sizeof(Double_t) * 10);
+  Int_t size  = (AliPID::kSPECIES)*(kNPBins);
+ memcpy(fMapRefHists, ref.fMapRefHists, sizeof(Double_t) * size);
 }
 
 //____________________________________________________________
@@ -81,9 +89,10 @@ AliTRDPIDResponse &AliTRDPIDResponse::operator=(const AliTRDPIDResponse &ref){
      delete fReferences;
    }
    SetBit(kIsOwner, kFALSE);
- }
+ } else if(fReferences) delete fReferences;
  fReferences = ref.fReferences;
- memcpy(fMapRefHists, ref.fMapRefHists, sizeof(Double_t) * 10);
+ Int_t size  = (AliPID::kSPECIES)*(kNPBins);
+ memcpy(fMapRefHists, ref.fMapRefHists, sizeof(Double_t) * size);
  fPIDmethod = ref.fPIDmethod;
 
  return *this;
@@ -94,10 +103,11 @@ AliTRDPIDResponse::~AliTRDPIDResponse(){
  //
  // Destructor
  //
- if(fReferences && IsOwner()){
-   fReferences->Delete();
-   delete fReferences;
+ if(IsOwner()){
+   // Destroy histos
+   if(fReferences) fReferences->Delete();
  }
+ if(fReferences) delete fReferences;
 }
 
 //____________________________________________________________
@@ -105,6 +115,9 @@ Bool_t AliTRDPIDResponse::Load(const Char_t * filename){
  //
  // Load References into the toolkit
  //
+
+  TDirectory *owd = gDirectory;// store old working directory
+
  if(!filename)
    filename = Form("%s/STEER/LQ1dRef_v1.root",gSystem->ExpandPathName("$ALICE_ROOT"));
  TFile *in = TFile::Open(filename);
@@ -113,14 +126,17 @@ Bool_t AliTRDPIDResponse::Load(const Char_t * filename){
    return kFALSE;
  }
 
+ gROOT->cd();
  fReferences = new TObjArray();
  fReferences->SetOwner();
  TIter iter(in->GetListOfKeys());
+ TKey *histkey = NULL;
  TObject *tmp = NULL;
  Int_t arrayPos = 0, pbin = 0; 
  AliPID::EParticleType species;
  TString histname;
- while((tmp = iter())){
+ while((histkey = dynamic_cast<TKey *>(iter()))){
+   tmp = histkey->ReadObj();
    histname = tmp->GetName();
    if(histname.BeginsWith("fHQel")){ // Electron histogram
      histname.ReplaceAll("fHQel_p","");
@@ -129,14 +145,16 @@ Bool_t AliTRDPIDResponse::Load(const Char_t * filename){
      histname.ReplaceAll("fHQpi_p","");
      species = AliPID::kPion;
    }
-   pbin = histname.Atoi();
+   pbin = histname.Atoi() - 1;
+   AliDebug(1, Form("Species %d, Histname %s, Pbin %d, Position in container %d", species, histname.Data(), pbin, arrayPos));
    fMapRefHists[species][pbin] = arrayPos;
-   fReferences->AddAt(tmp->Clone(), arrayPos);
+   fReferences->AddAt(new TH1F(*dynamic_cast<TH1F *>(tmp)), arrayPos);
    arrayPos++;
  } 
  in->Close();
-
+ owd->cd();
  AliDebug(2, Form("Successfully loaded %d Reference Histograms", arrayPos));
+ SetBit(kIsOwner, kTRUE);
  delete in;
  return kTRUE;
 }
@@ -169,14 +187,15 @@ Bool_t AliTRDPIDResponse::GetResponse(Int_t n, Double_t *dedx, Float_t *p, Doubl
 
  for(Int_t is(AliPID::kSPECIES); is--;) prob[is]=.2;
  Double_t prLayer[AliPID::kSPECIES];
- memset(prLayer, 0, AliPID::kSPECIES*sizeof(Double_t));
  Double_t DE[10], s(0.);
  for(Int_t il(kNlayer); il--;){
+   memset(prLayer, 0, AliPID::kSPECIES*sizeof(Double_t));
    if(!CookdEdx(&dedx[il*n], &DE[0])) continue;
 
    s=0.;
    for(Int_t is(AliPID::kSPECIES); is--;){
-     prLayer[is] = GetProbabilitySingleLayer(is, p[il], DE[0]);
+     if((DE[0] > 0.) && (p[il] > 0.)) prLayer[is] = GetProbabilitySingleLayer(is, p[il], DE[0]);
+     AliDebug(3, Form("Probability for Species %d in Layer %d: %f", is, il, prLayer[is]));
      s+=prLayer[is];
    }
    if(s<1.e-30){
@@ -208,30 +227,42 @@ Double_t AliTRDPIDResponse::GetProbabilitySingleLayer(Int_t species, Double_t pl
  // from the reference histogram
  // Interpolation between momentum bins
  //
- Int_t pbin = GetLowerMomentumBin(plocal);
+ AliDebug(1, Form("Make Probability for Species %d with Momentum %f", species, plocal));
+ Int_t pbin = GetLowerMomentumBin(plocal);  
+ AliDebug(1, Form("Bin %d", pbin));
  Double_t pLayer = 0.;
  // Do Interpolation exept for underflow and overflow
  if(pbin >= 0 && pbin < kNPBins-1){
    TH1 *refHistUpper = NULL, *refHistLower = NULL;
-   refHistLower = dynamic_cast<TH1 *>(fReferences->UncheckedAt(fMapRefHists[species][pbin]));
-   refHistUpper = dynamic_cast<TH1 *>(fReferences->UncheckedAt(fMapRefHists[species][pbin+1]));
+   if(fMapRefHists[species][pbin] >= 0)
+    refHistLower = dynamic_cast<TH1 *>(fReferences->UncheckedAt(fMapRefHists[species][pbin]));
+   if(fMapRefHists[species][pbin+1] >= 0)
+    refHistUpper = dynamic_cast<TH1 *>(fReferences->UncheckedAt(fMapRefHists[species][pbin+1]));
+   AliDebug(1, Form("Reference Histos (Upper/Lower): [%p|%p]", refHistUpper, refHistLower));
 
-   Double_t pLower = refHistLower->GetBinContent(refHistLower->GetXaxis()->FindBin(dEdx));
-   Double_t pUpper = refHistUpper->GetBinContent(refHistUpper->GetXaxis()->FindBin(dEdx));
+   if (refHistLower && refHistUpper ) {
+     Double_t pLower = refHistLower->GetBinContent(refHistLower->GetXaxis()->FindBin(dEdx));
+     Double_t pUpper = refHistUpper->GetBinContent(refHistUpper->GetXaxis()->FindBin(dEdx));
 
-   pLayer = pLower + (pUpper - pLower)/(fgkPBins[pbin+1]-fgkPBins[pbin]) * plocal; 
+     pLayer = pLower + (pUpper - pLower)/(fgkPBins[pbin+1]-fgkPBins[pbin]) * (plocal - fgkPBins[pbin]); 
+   }
  }
  else{
    TH1 *refHist = NULL;
    if(pbin < 0){
      // underflow
-     refHist = dynamic_cast<TH1 *>(fReferences->UncheckedAt(fMapRefHists[0][0])); 
+     if(fMapRefHists[species][0] >= 0){
+       refHist = dynamic_cast<TH1 *>(fReferences->UncheckedAt(fMapRefHists[species][0])); 
+     }
    } else {
      // overflow
-     refHist = dynamic_cast<TH1 *>(fReferences->UncheckedAt(fMapRefHists[species][kNPBins])); 
+     if(fMapRefHists[species][kNPBins-1] > 0)
+       refHist = dynamic_cast<TH1 *>(fReferences->UncheckedAt(fMapRefHists[species][kNPBins-1])); 
    }
-   pLayer = refHist->GetBinContent(refHist->GetXaxis()->FindBin(dEdx));
+   if (refHist)
+     pLayer = refHist->GetBinContent(refHist->GetXaxis()->FindBin(dEdx));
  }
+ AliDebug(1, Form("Probability %f", pLayer));
  return pLayer;
 }
 
@@ -260,6 +291,7 @@ void AliTRDPIDResponse::SetOwner(){
  TObjArray *ctmp = new TObjArray();
  for(Int_t ien = 0; ien < fReferences->GetEntriesFast(); ien++)
    ctmp->AddAt(fReferences->UncheckedAt(ien)->Clone(), ien);
+ delete fReferences;
  fReferences = ctmp;
  SetBit(kIsOwner, kTRUE);
 }
@@ -274,7 +306,7 @@ Bool_t AliTRDPIDResponse::CookdEdx(Double_t *in, Double_t *out) const
    break;
  case 2: // 1D LQ 
    out[0]= 0.;
-   for(Int_t islice = 0; islice < 8; islice++) out[0] += in[islice];
+   for(Int_t islice = 0; islice < 8; islice++) out[0] += in[islice] * fGainNormalisationFactor;
    if(out[0] < 1e-6) return kFALSE;
    break;
  default: