]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - PWG3/dielectron/AliDielectronCFdraw.cxx
Fix Coverity reports
[u/mrichter/AliRoot.git] / PWG3 / dielectron / AliDielectronCFdraw.cxx
index 5e73f10b827f2037a4f20d541983c8df61348e6b..28f458148551659fc331e7206bdf6489d2fc5234 100644 (file)
@@ -13,6 +13,8 @@
 * provided "as is" without express or implied warranty.                  *
 **************************************************************************/
 
+/* $Id$ */
+
 ///////////////////////////////////////////////////////////////////////////
 //       Dielectron Correction framework draw helper                     //
 //                                                                       //
@@ -180,14 +182,14 @@ void AliDielectronCFdraw::SetRangeUser(Int_t ivar, Double_t min, Double_t max, c
   if (arr->GetEntriesFast()==0){
     // all slices in case of 0 entries
     for (Int_t istep=0; istep<fCfContainer->GetNStep(); ++istep){
-      fCfContainer->SetRangeUser(ivar,min,max,istep);
+      fCfContainer->GetGrid(istep)->SetRangeUser(ivar,min,max);
     }
   } else {
     TIter next(arr);
     TObjString *ostr=0x0;
     while ( (ostr=static_cast<TObjString*>(next())) ) {
       Int_t istep=ostr->GetString().Atoi();
-      fCfContainer->SetRangeUser(ivar,min,max,istep);
+      fCfContainer->GetGrid(istep)->SetRangeUser(ivar,min,max);
     }
   }
   delete arr;
@@ -240,7 +242,7 @@ void AliDielectronCFdraw::Draw(const Option_t* varnames, const char* opt, const
   TIter next(arrVars);
   TObjString *ostr=0x0;
   Int_t ivar[3]={-1,-1,-1};
-  for (Int_t i=0; i<entries; ++i){
+  for (Int_t i=entries-1; i>=0; --i){
     ostr=static_cast<TObjString*>(next());
     if (ostr->GetString().IsDigit()){
       ivar[i]=ostr->GetString().Atoi();
@@ -249,17 +251,7 @@ void AliDielectronCFdraw::Draw(const Option_t* varnames, const char* opt, const
     }
   }
 
-  switch (entries){
-  case 1:
-    Draw(ivar[0],opt,slices);
-    break;
-  case 2:
-    Draw(ivar[1],ivar[0],opt,slices);
-    break;
-  case 3:
-    Draw(ivar[2],ivar[1],ivar[0],opt,slices);
-    break;
-  }
+  Draw(ivar[0],ivar[1],ivar[2],opt,slices);
   delete arrVars;
 }
 
@@ -275,9 +267,8 @@ void AliDielectronCFdraw::Draw(Int_t var, const char* opt, const char* slices)
   // are drawn in each sub pad
   //
 
-  const Int_t ndim=1;
-  Int_t vars[ndim]={var};
-  TObjArray *arr=CollectHistosProj(ndim,vars,slices);
+  Int_t vars[3]={var,-1,-1};
+  TObjArray *arr=CollectHistosProj(vars,slices);
   Draw(arr,opt);
   delete arr; 
 }
@@ -288,9 +279,8 @@ void AliDielectronCFdraw::Draw(Int_t var0, Int_t var1, const char* opt, const ch
   //
   // Draw 2D case
   //
-  const Int_t ndim=2;
-  Int_t vars[ndim]={var0,var1};
-  TObjArray *arr=CollectHistosProj(ndim,vars,slices);
+  Int_t vars[3]={var0,var1,-1};
+  TObjArray *arr=CollectHistosProj(vars,slices);
   Draw(arr,opt);
   delete arr;
 }
@@ -301,19 +291,19 @@ void AliDielectronCFdraw::Draw(Int_t var0, Int_t var1, Int_t var2, const char* o
   //
   // Draw 3D case
   //
-  const Int_t ndim=3;
-  Int_t vars[ndim]={var0,var1,var2};
-  TObjArray *arr=CollectHistosProj(ndim,vars,slices);
+  Int_t vars[3]={var0,var1,var2};
+  TObjArray *arr=CollectHistosProj(vars,slices);
   Draw(arr,opt);
   delete arr;
 }
 
 //________________________________________________________________
-TObjArray* AliDielectronCFdraw::CollectHistosProj(Int_t dim, Int_t *vars, const char* slices)
+TObjArray* AliDielectronCFdraw::CollectHistosProj(const Int_t vars[3], const char* slices)
 {
   //
-  // Collect histos with 'dim'ension of the 'slices' separated by one of "':;'"
+  // Collect histos with up to 3 dimension of the 'slices' separated by one of "':;'"
   // in a TObjArray and return it
+  // if a dimension is not used it must be set to -1
   //
   TObjArray *arr=TString(slices).Tokenize(",:;");
   TObjArray *arrHists=0x0;
@@ -321,7 +311,7 @@ TObjArray* AliDielectronCFdraw::CollectHistosProj(Int_t dim, Int_t *vars, const
     // all slices in case of 0 entries
     arrHists=new TObjArray(fCfContainer->GetNStep());
     for (Int_t istep=0; istep<fCfContainer->GetNStep(); ++istep){
-      TH1 *hproj=Project(dim,vars,istep);
+      TH1 *hproj=Project(vars,istep);
       if (!hproj) continue;
       hproj->SetName(Form("proj_%02d",istep));
       hproj->SetTitle(fCfContainer->GetStepTitle(istep));
@@ -333,7 +323,7 @@ TObjArray* AliDielectronCFdraw::CollectHistosProj(Int_t dim, Int_t *vars, const
     TObjString *ostr=0x0;
     while ( (ostr=static_cast<TObjString*>(next())) ) {
       Int_t istep=ostr->GetString().Atoi();
-      TH1 *hproj=Project(dim,vars,istep);
+      TH1 *hproj=Project(vars,istep);
       if (!hproj) continue;
       hproj->SetName(Form("proj_%02d",istep));
       hproj->SetTitle(fCfContainer->GetStepTitle(istep));
@@ -346,31 +336,49 @@ TObjArray* AliDielectronCFdraw::CollectHistosProj(Int_t dim, Int_t *vars, const
 }
 
 //________________________________________________________________
-TH1* AliDielectronCFdraw::Project(Int_t ndim, Int_t *vars, Int_t slice)
+TH1* AliDielectronCFdraw::Project(const Int_t *vars, Int_t slice)
 {
   //
-  // Do an nim projection
+  // Do an ndim projection
+  //
+  return fCfContainer->Project(slice,vars[0],vars[1],vars[2]);
+}
+
+//________________________________________________________________
+TH1* AliDielectronCFdraw::Project(const Option_t* var, Int_t slice)
+{
+  //
+  // translate variable names and do projection
   //
-  switch (ndim){
-  case 1:
-    return fCfContainer->Project(vars[0],slice);
-    break;
-  case 2:
-    return fCfContainer->Project(vars[0],vars[1],slice);
-    break;
-  case 3:
-    return fCfContainer->Project(vars[0],vars[1],vars[2],slice);
-    break;
+  TObjArray *arrVars=TString(var).Tokenize(":");
+  Int_t entries=arrVars->GetEntriesFast();
+  if (entries<1||entries>3){
+    AliError("Wrong number of variables, supported are 1 - 3 dimensions");
+    delete arrVars;
+    return 0x0;
   }
-  return 0x0;
+  
+  TObjString *ostr=0x0;
+  Int_t ivar[3]={-1,-1,-1};
+  for (Int_t i=entries-1; i>=0; --i){
+    ostr=static_cast<TObjString*>(arrVars->At(i));
+    if (ostr->GetString().IsDigit()){
+      ivar[i]=ostr->GetString().Atoi();
+    } else {
+      ivar[i]=fCfContainer->GetVar(ostr->GetName());
+    }
+  }
+  if (ivar[0]==-1) return 0x0;
+  delete arrVars;
+  return fCfContainer->Project(slice,ivar[0],ivar[1],ivar[2]);
 }
 
 //________________________________________________________________
-void AliDielectronCFdraw::DrawEfficiency(const char* varnames, const char* nominators, Int_t denominator, const char* opt)
+void AliDielectronCFdraw::DrawEfficiency(const char* varnames, const char* numerators, Int_t denominator, const char* opt)
 {
   //
   // plot efficiencies for variables. Variable may be up to 3 dim, separated by a ':'
-  // you may have several nominators, sparated by one of ',:;'
+  // you may have several numerators, sparated by one of ',:;'
   //
   
   TObjArray *arrVars=TString(varnames).Tokenize(":");
@@ -397,35 +405,24 @@ void AliDielectronCFdraw::DrawEfficiency(const char* varnames, const char* nomin
   TString optStr(opt);
   if (optStr.Contains("2")) type=1;
   
-  switch (entries){
-  case 1:
-    DrawEfficiency(ivar[0],nominators, denominator,opt,type);
-    break;
-  case 2:
-    DrawEfficiency(ivar[1],ivar[0], nominators, denominator,opt,type);
-    break;
-  case 3:
-    DrawEfficiency(ivar[2],ivar[1],ivar[0],nominators, denominator,opt,type);
-    break;
-  }
+  DrawEfficiency(ivar[2],ivar[1],ivar[0],numerators, denominator,opt,type);
   delete arrVars;
 }
 
 //________________________________________________________________
-void AliDielectronCFdraw::DrawEfficiency(Int_t var, const char* nominators, Int_t denominator, const char* opt, Int_t type)
+void AliDielectronCFdraw::DrawEfficiency(Int_t var, const char* numerators, Int_t denominator, const char* opt, Int_t type)
 {
   //
-  // Draw Efficiencies for all nominators
-  // nominators may be divided by and of ,;:
+  // Draw Efficiencies for all numerators
+  // numerators may be divided by and of ,;:
   //
   // if opt contains 'same' all histograms are drawn in the same pad
   // otherwise the pad will be divided in sub pads and the histograms
   // are drawn in each sub pad
   //
   
-  const Int_t ndim=1;
-  Int_t vars[ndim]={var};
-  TObjArray *arr=CollectHistosEff(ndim,vars,nominators,denominator,type);
+  Int_t vars[3]={var,-1,-1};
+  TObjArray *arr=CollectHistosEff(vars,numerators,denominator,type);
   TString drawOpt=opt;
   drawOpt+="eff";
   Draw(arr,drawOpt);
@@ -433,14 +430,13 @@ void AliDielectronCFdraw::DrawEfficiency(Int_t var, const char* nominators, Int_
 }
 
 //________________________________________________________________
-void AliDielectronCFdraw::DrawEfficiency(Int_t var0, Int_t var1, const char* nominators, Int_t denominator, const char* opt, Int_t type)
+void AliDielectronCFdraw::DrawEfficiency(Int_t var0, Int_t var1, const char* numerators, Int_t denominator, const char* opt, Int_t type)
 {
   //
   // Draw 2D case
   //
-  const Int_t ndim=2;
-  Int_t vars[ndim]={var0,var1};
-  TObjArray *arr=CollectHistosEff(ndim,vars,nominators,denominator,type);
+  Int_t vars[3]={var0,var1,-1};
+  TObjArray *arr=CollectHistosEff(vars,numerators,denominator,type);
   TString drawOpt=opt;
   drawOpt+="eff";
   Draw(arr,drawOpt);
@@ -448,14 +444,13 @@ void AliDielectronCFdraw::DrawEfficiency(Int_t var0, Int_t var1, const char* nom
 }
 
 //________________________________________________________________
-void AliDielectronCFdraw::DrawEfficiency(Int_t var0, Int_t var1, Int_t var2, const char* nominators, Int_t denominator, const char* opt, Int_t type)
+void AliDielectronCFdraw::DrawEfficiency(Int_t var0, Int_t var1, Int_t var2, const char* numerators, Int_t denominator, const char* opt, Int_t type)
 {
   //
   // Draw 3D case
   //
-  const Int_t ndim=3;
-  Int_t vars[ndim]={var0,var1,var2};
-  TObjArray *arr=CollectHistosEff(ndim,vars,nominators,denominator,type);
+  Int_t vars[3]={var0,var1,var2};
+  TObjArray *arr=CollectHistosEff(vars,numerators,denominator,type);
   TString drawOpt=opt;
   drawOpt+="eff";
   Draw(arr,drawOpt);
@@ -463,13 +458,13 @@ void AliDielectronCFdraw::DrawEfficiency(Int_t var0, Int_t var1, Int_t var2, con
 }
 
 //________________________________________________________________
-TObjArray* AliDielectronCFdraw::CollectHistosEff(Int_t dim, Int_t *vars, const char* nominators, Int_t denominator, Int_t type)
+TObjArray* AliDielectronCFdraw::CollectHistosEff(const  Int_t vars[3], const char* numerators, Int_t denominator, Int_t type)
 {
   //
   // Collect histos with 'dim'ension of the 'slices' separated by one of "':;'"
   // in a TObjArray and return it
   //
-  TObjArray *arr=TString(nominators).Tokenize(",:;");
+  TObjArray *arr=TString(numerators).Tokenize(",:;");
   TObjArray *arrHists=0x0;
 
   if (type==0){
@@ -479,7 +474,7 @@ TObjArray* AliDielectronCFdraw::CollectHistosEff(Int_t dim, Int_t *vars, const c
       fVdata.ResizeTo(arrHists->GetSize());
       for (Int_t istep=0; istep<fCfContainer->GetNStep(); ++istep){
         fEffGrid->CalculateEfficiency(istep,denominator);
-        TH1 *hproj=ProjectEff(dim,vars);
+        TH1 *hproj=ProjectEff(vars);
         if (!hproj) continue;
         Float_t eff=fEffGrid->GetAverage();
         fVdata(istep)=eff;
@@ -496,7 +491,7 @@ TObjArray* AliDielectronCFdraw::CollectHistosEff(Int_t dim, Int_t *vars, const c
       while ( (ostr=static_cast<TObjString*>(next())) ) {
         Int_t istep=ostr->GetString().Atoi();
         fEffGrid->CalculateEfficiency(istep,denominator);
-        TH1 *hproj=ProjectEff(dim,vars);
+        TH1 *hproj=ProjectEff(vars);
         if (!hproj) continue;
         Float_t eff=fEffGrid->GetAverage();
         fVdata(count++)=eff;
@@ -509,14 +504,14 @@ TObjArray* AliDielectronCFdraw::CollectHistosEff(Int_t dim, Int_t *vars, const c
 
   //second approach
   if (type==1){
-    TH1 *hDen=Project(dim,vars,denominator);
+    TH1 *hDen=Project(vars,denominator);
     Double_t entriesDen=hDen->GetEffectiveEntries();
     if (arr->GetEntriesFast()==0){
     // all slices in case of 0 entries
       arrHists=new TObjArray(fCfContainer->GetNStep());
       fVdata.ResizeTo(arrHists->GetSize());
       for (Int_t istep=0; istep<fCfContainer->GetNStep(); ++istep){
-        TH1 *hproj=Project(dim,vars,istep);
+        TH1 *hproj=Project(vars,istep);
         if (!hproj) continue;
         Float_t eff=0;
         if (entriesDen>0) eff=hproj->GetEffectiveEntries()/entriesDen;
@@ -534,7 +529,7 @@ TObjArray* AliDielectronCFdraw::CollectHistosEff(Int_t dim, Int_t *vars, const c
       Int_t count=0;
       while ( (ostr=static_cast<TObjString*>(next())) ) {
         Int_t istep=ostr->GetString().Atoi();
-        TH1 *hproj=Project(dim,vars,istep);
+        TH1 *hproj=Project(vars,istep);
         if (!hproj) continue;
         Float_t eff=0;
         if (entriesDen>0) eff=hproj->GetEffectiveEntries()/entriesDen;
@@ -554,23 +549,12 @@ TObjArray* AliDielectronCFdraw::CollectHistosEff(Int_t dim, Int_t *vars, const c
 }
 
 //________________________________________________________________
-TH1* AliDielectronCFdraw::ProjectEff(Int_t ndim, Int_t *vars)
+TH1* AliDielectronCFdraw::ProjectEff(const Int_t vars[3])
 {
   //
   // Do an nim projection
   //
-  switch (ndim){
-  case 1:
-    return fEffGrid->Project(vars[0]);
-    break;
-  case 2:
-    return fEffGrid->Project(vars[0],vars[1]);
-    break;
-  case 3:
-    return fEffGrid->Project(vars[0],vars[1],vars[2]);
-    break;
-  }
-  return 0x0;
+  return fEffGrid->Project(vars[0],vars[1],vars[2]);
 }
 
 //________________________________________________________________
@@ -669,3 +653,26 @@ void AliDielectronCFdraw::Draw(const TObjArray *arr, const char* opt)
   }
   
 }
+
+//________________________________________________________________
+Double_t AliDielectronCFdraw::GetAverageEfficiency(Int_t numerator, Int_t denominator, Double_t &effErr)
+{
+  //
+  // Extract the mean efficiency of the numerator and denominator
+  //
+
+  //use variable 0 as default, since for the average it doesn't matter
+  TH1 *hDen=fCfContainer->Project(denominator,0);
+  Double_t entriesDen=hDen->GetEffectiveEntries();
+  TH1 *hproj=fCfContainer->Project(numerator,0);
+  if (!hproj) return -1.;
+  Double_t entriesNum=hproj->GetEffectiveEntries();
+  if (entriesDen<1||entriesNum<1) return -1;
+  
+  Double_t eff=-1.;
+  if (entriesDen>0) eff=entriesNum/entriesDen;
+  effErr=TMath::Sqrt(1./entriesNum+1./entriesDen)*eff;
+  delete hDen;
+  delete hproj;
+  return eff;
+}