]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - PWG0/dNdEta/dNdEtaAnalysis.cxx
o) proper splitting of STEER dependent classes (PWG0dep) and ESD dependent classes
[u/mrichter/AliRoot.git] / PWG0 / dNdEta / dNdEtaAnalysis.cxx
index 06501f1e34abe180e6215f62045790d50028f41d..4e21461b867b53204f493f368d660c79ab4af755 100644 (file)
+/* $Id$ */
+
 #include "dNdEtaAnalysis.h"
 
+#include <TFile.h>
+#include <TH2F.h>
+#include <TH1D.h>
+#include <TMath.h>
+#include <TCanvas.h>
+#include <TCollection.h>
+#include <TIterator.h>
+#include <TList.h>
+#include <TLegend.h>
+
+#include "dNdEtaCorrection.h"
+
 //____________________________________________________________________
 ClassImp(dNdEtaAnalysis)
 
 //____________________________________________________________________
-dNdEtaAnalysis::dNdEtaAnalysis(Char_t* name) {
+dNdEtaAnalysis::dNdEtaAnalysis(Char_t* name, Char_t* title) :
+  TNamed(name, title),
+  fEtaVsVtx(0),
+  fEtaVsVtxUncorrected(0),
+  fVtx(0)
+{
+  // constructor
+
+  fEtaVsVtx  = new TH2F(Form("%s_eta_vs_vtx", name),"",80,-20,20,120,-6,6);
+  fEtaVsVtx->SetXTitle("vtx z [cm]");
+  fEtaVsVtx->SetYTitle("#eta");
+
+  fEtaVsVtxUncorrected = dynamic_cast<TH2F*> (fEtaVsVtx->Clone(Form("%s_eta_vs_vtx_uncorrected", name)));
+  fVtx       = fEtaVsVtx->ProjectionX(Form("%s_vtx", name));
+  for (Int_t i=0; i<kVertexBinning; ++i)
+  {
+    fdNdEta[i]    = fEtaVsVtx->ProjectionY(Form("%s_dNdEta_%d", name, i));
+    fdNdEta[i]->SetYTitle("dN/d#eta");
+  }
+
+  fEtaVsVtx->Sumw2();
+  fVtx->Sumw2();
+}
+
+//____________________________________________________________________
+dNdEtaAnalysis::~dNdEtaAnalysis()
+{
+  // destructor
+
+  delete fEtaVsVtx;
+  fEtaVsVtx = 0;
+
+  delete fEtaVsVtxUncorrected;
+  fEtaVsVtxUncorrected = 0;
+
+  delete fVtx;
+  fVtx = 0;
+
+  for (Int_t i=0; i<kVertexBinning; ++i)
+  {
+    delete fdNdEta[i];
+    fdNdEta[i] = 0;
+  }
+}
+
+//_____________________________________________________________________________
+dNdEtaAnalysis::dNdEtaAnalysis(const dNdEtaAnalysis &c) :
+  TNamed(c),
+  fEtaVsVtx(0),
+  fEtaVsVtxUncorrected(0),
+  fVtx(0)
+{
+  //
+  // dNdEtaAnalysis copy constructor
+  //
+
+  ((dNdEtaAnalysis &) c).Copy(*this);
+}
+
+//_____________________________________________________________________________
+dNdEtaAnalysis &dNdEtaAnalysis::operator=(const dNdEtaAnalysis &c)
+{
+  //
+  // Assignment operator
+  //
+
+  if (this != &c) ((dNdEtaAnalysis &) c).Copy(*this);
+  return *this;
+}
+
+//_____________________________________________________________________________
+void dNdEtaAnalysis::Copy(TObject &c) const
+{
+  //
+  // Copy function
+  //
 
-  fName = TString(name);
+  dNdEtaAnalysis& target = (dNdEtaAnalysis &) c;
 
-  hEtaVsVtx  = new TH2F("eta_vs_vtx","",80,-20,20,120,-6,6);   
-  hVtx       = (TH1F*)hEtaVsVtx->ProjectionX("vtx");
-  hdNdEta    = (TH1F*)hEtaVsVtx->ProjectionY("dNdEta");
-  hEtaVsVtx->SetXTitle("vtx z [cm]");
-  hEtaVsVtx->SetYTitle("#eta");
-  hVtx->SetXTitle("vtx z [cm]");
-  hdNdEta->SetXTitle("#eta");
-  hdNdEta->SetYTitle("dN/d#eta");
+  target.fEtaVsVtx = dynamic_cast<TH2F*> (fEtaVsVtx->Clone());
+  target.fEtaVsVtxUncorrected = dynamic_cast<TH2F*> (fEtaVsVtxUncorrected->Clone());
+  target.fVtx = dynamic_cast<TH1D*> (fVtx->Clone());
 
-  hEtaVsVtx->Sumw2();
-  hVtx->Sumw2();
+  for (Int_t i=0; i<kVertexBinning; ++i)
+    target.fdNdEta[i] = dynamic_cast<TH1D*> (fdNdEta[i]->Clone());
 
+  TNamed::Copy((TNamed &) c);
 }
 
 //____________________________________________________________________
-void
-dNdEtaAnalysis::FillTrack(Float_t vtx, Float_t eta, Float_t weight) {
-  hEtaVsVtx->Fill(vtx, eta, weight);
+void dNdEtaAnalysis::FillTrack(Float_t vtx, Float_t eta)
+{
+  // fills a track into the histograms
+
+  fEtaVsVtxUncorrected->Fill(vtx,eta);
 }
 
 //____________________________________________________________________
-void
-dNdEtaAnalysis::FillEvent(Float_t vtx) {
-  hVtx->Fill(vtx);
+void dNdEtaAnalysis::FillEvent(Float_t vtx)
+{
+  // fills an event into the histograms
+
+  fVtx->Fill(vtx);
 }
 
 //____________________________________________________________________
-void
-dNdEtaAnalysis::Finish() {  
-
-  // first normalize with n events (per vtx)
-  for (Int_t i_vtx=0; i_vtx<=hVtx->GetNbinsX(); i_vtx++) {
-    Float_t nEvents      = hVtx->GetBinContent(i_vtx);
-    Float_t nEventsError = hVtx->GetBinError(i_vtx);
-    
-    if (nEvents==0) continue;
-    
-    for (Int_t i_eta=0; i_eta<=hEtaVsVtx->GetNbinsY(); i_eta++) {
-      Float_t value = hEtaVsVtx->GetBinContent(i_vtx, i_eta)/nEvents;
-      if (value==0) continue;
-      Float_t error = hEtaVsVtx->GetBinError(i_vtx, i_eta)/nEvents;
-      error = TMath::Sqrt(TMath::Power(hEtaVsVtx->GetBinError(i_vtx, i_eta)/
-                                      hEtaVsVtx->GetBinContent(i_vtx, i_eta),2) +
-                         TMath::Power(nEventsError/nEvents,2));
-      hEtaVsVtx->SetBinContent(i_vtx, i_eta, value);
-      hEtaVsVtx->SetBinError(i_vtx,   i_eta, error);
+void dNdEtaAnalysis::Finish(dNdEtaCorrection* correction)
+{
+  // correct with correction values if available
+
+  // TODO what do we do with the error?
+  if (!correction)
+    printf("INFO: No correction applied\n");
+
+  // this can be replaced by TH2F::Divide if we agree that the binning will be always the same
+  for (Int_t iVtx=0; iVtx<=fEtaVsVtxUncorrected->GetNbinsX(); iVtx++)
+  {
+    for (Int_t iEta=0; iEta<=fEtaVsVtxUncorrected->GetNbinsY(); iEta++)
+    {
+      Float_t correctionValue = 1;
+      if (correction)
+        correctionValue = correction->GetCorrection(fEtaVsVtxUncorrected->GetXaxis()->GetBinCenter(iVtx), fEtaVsVtxUncorrected->GetYaxis()->GetBinCenter(iEta));
+
+      Float_t value = fEtaVsVtxUncorrected->GetBinContent(iVtx, iEta);
+      Float_t error = fEtaVsVtxUncorrected->GetBinError(iVtx, iEta);
+
+      Float_t correctedValue = value * correctionValue;
+      Float_t correctedError = error * correctionValue;
+
+      if (correctedValue != 0)
+      {
+        fEtaVsVtx->SetBinContent(iVtx, iEta, correctedValue);
+        fEtaVsVtx->SetBinError(iVtx, iEta, correctedError);
+      }
     }
   }
 
-  // then take the wieghted average for each eta
-  // is this the right way to do it???
-  for (Int_t i_eta=0; i_eta<=hEtaVsVtx->GetNbinsY(); i_eta++) {
-    Float_t sum           = 0;
-    Float_t sumError2     = 0;
-    Int_t   nMeasurements = 0;    
-
-    Float_t sum_xw = 0;
-    Float_t sum_w  = 0;
-    
-    for (Int_t i_vtx=0; i_vtx<=hVtx->GetNbinsX(); i_vtx++) {
-      if (hVtx->GetBinContent(i_vtx)==0)             continue;
-      if (hEtaVsVtx->GetBinContent(i_vtx, i_eta)==0) continue;
-
-      Float_t w = 1/TMath::Power(hEtaVsVtx->GetBinError(i_vtx, i_eta),2);
-      sum_xw = sum_xw + hEtaVsVtx->GetBinContent(i_vtx, i_eta)*w;
-      sum_w  = sum_w + w;
-
-      sum = sum + hEtaVsVtx->GetBinContent(i_vtx, i_eta);
-      sumError2 = sumError2 + TMath::Power(hEtaVsVtx->GetBinError(i_vtx, i_eta),2);      
-      nMeasurements++;
-    }
-    Float_t dndeta = 0;
-    Float_t error  = 0;
+  for (Int_t iEta=0; iEta<=fEtaVsVtx->GetNbinsY(); iEta++)
+  {
+    // do we have several histograms for different vertex positions?
+    Int_t vertexBinWidth = fVtx->GetNbinsX() / (kVertexBinning-1);
+    for (Int_t vertexPos=0; vertexPos<kVertexBinning; ++vertexPos)
+    {
+      Int_t vertexBinBegin = 1;
+      Int_t vertexBinEnd = fVtx->GetNbinsX() + 1;
 
-    if (nMeasurements!=0) {
-      dndeta = sum/Float_t(nMeasurements);
-      error  = TMath::Sqrt(sumError2)/Float_t(nMeasurements);
-      
-      dndeta = sum_xw/sum_w;
-      error  = 1/TMath::Sqrt(sum_w);
+      // the first histogram is always for the whole vertex range
+      if (vertexPos > 0)
+      {
+        vertexBinBegin = 1 + vertexBinWidth * (vertexPos-1);
+        vertexBinEnd = vertexBinBegin + vertexBinWidth;
+      }
 
-      dndeta = dndeta/hdNdEta->GetBinWidth(i_eta);
-      error  = error/hdNdEta->GetBinWidth(i_eta);
+      Float_t totalEvents = fVtx->Integral(vertexBinBegin, vertexBinEnd - 1);
+      if (totalEvents == 0)
+      {
+        printf("WARNING: No events for hist %d %d %d\n", vertexPos, vertexBinBegin, vertexBinEnd);
+        continue;
+      }
 
-      hdNdEta->SetBinContent(i_eta, dndeta);    
-      hdNdEta->SetBinError(i_eta, error);    
-    }
+      Float_t sum = 0;
+      Float_t sumError2 = 0;
+      for (Int_t iVtx = vertexBinBegin; iVtx < vertexBinEnd; iVtx++)
+      {
+        if (fEtaVsVtx->GetBinContent(iVtx, iEta) != 0)
+        {
+          sum = sum + fEtaVsVtx->GetBinContent(iVtx, iEta);
+          sumError2 = sumError2 + TMath::Power(fEtaVsVtx->GetBinError(iVtx, iEta),2);
+        }
+      }
 
+      Float_t dndeta = sum / totalEvents;
+      Float_t error  = TMath::Sqrt(sumError2) / totalEvents;
+
+      dndeta = dndeta/fdNdEta[vertexPos]->GetBinWidth(iEta);
+      error  = error/fdNdEta[vertexPos]->GetBinWidth(iEta);
+
+      fdNdEta[vertexPos]->SetBinContent(iEta, dndeta);
+      fdNdEta[vertexPos]->SetBinError(iEta, error);
+    }
   }
 }
 
+//____________________________________________________________________
+void dNdEtaAnalysis::SaveHistograms()
+{
+  // save the histograms to a directory with the name of this class (retrieved from TNamed)
 
+  gDirectory->mkdir(GetName());
+  gDirectory->cd(GetName());
 
-//____________________________________________________________________
-void
-dNdEtaAnalysis::SaveHistograms() {
+  fEtaVsVtx  ->Write();
+  fEtaVsVtxUncorrected->Write();
+  fVtx       ->Write();
+  for (Int_t i=0; i<kVertexBinning; ++i)
+    fdNdEta[i]    ->Write();
+
+  gDirectory->cd("../");
+}
+
+void dNdEtaAnalysis::LoadHistograms()
+{
+  // loads the histograms from a directory with the name of this class (retrieved from TNamed)
 
-  gDirectory->mkdir(fName.Data());
-  gDirectory->cd(fName.Data());
-  
-  hEtaVsVtx  ->Write();
-  hVtx       ->Write();
-  hdNdEta    ->Write();
+  gDirectory->cd(GetName());
+
+  fEtaVsVtx = dynamic_cast<TH2F*> (gDirectory->Get(fEtaVsVtx->GetName()));
+  fEtaVsVtxUncorrected = dynamic_cast<TH2F*> (gDirectory->Get(fEtaVsVtxUncorrected->GetName()));
+
+  fVtx = dynamic_cast<TH1D*> (gDirectory->Get(fVtx->GetName()));
+
+  for (Int_t i=0; i<kVertexBinning; ++i)
+    fdNdEta[i] = dynamic_cast<TH1D*> (gDirectory->Get(fdNdEta[i]->GetName()));
 
   gDirectory->cd("../");
 }
 
+//____________________________________________________________________
+void dNdEtaAnalysis::DrawHistograms()
+{
+  // draws the histograms
+
+  TCanvas* canvas = new TCanvas("dNdEtaAnalysis", "dNdEtaAnalysis", 800, 800);
+  canvas->Divide(2, 2);
+
+  canvas->cd(1);
+  if (fEtaVsVtx)
+    fEtaVsVtx->Draw("COLZ");
+
+  canvas->cd(2);
+  if (fEtaVsVtxUncorrected)
+    fEtaVsVtxUncorrected->Draw("COLZ");
+
+  canvas->cd(3);
+  if (fVtx)
+    fVtx->Draw();
+
+  canvas->cd(4);
+  if (fdNdEta[0])
+    fdNdEta[0]->Draw();
+
+    // histograms for different vertices?
+  if (kVertexBinning > 0)
+  {
+      // doesnt work, but i dont get it, giving up...
+    /*TCanvas* canvas2 =*/ new TCanvas("dNdEtaAnalysisVtx", "dNdEtaAnalysisVtx", 450, 450);
+    //Int_t yPads = (Int_t) TMath::Ceil(((Double_t) kVertexBinning - 1) / 2);
+    //printf("%d\n", yPads);
+    //canvas2->Divide(2, yPads);
+
+    TLegend* legend = new TLegend(0.7, 0.7, 0.9, 0.9);
+
+    for (Int_t i=0; i<kVertexBinning; ++i)
+    {
+      //canvas2->cd(i-1);
+      //printf("%d\n", i);
+      if (fdNdEta[i])
+      {
+        fdNdEta[i]->SetLineColor(i+1);
+        fdNdEta[i]->Draw((i == 0) ? "" : "SAME");
+        legend->AddEntry(fdNdEta[i], (i == 0) ? "Vtx All" : Form("Vtx Bin %d", i-1));
+      }
+    }
+
+    legend->Draw();
+  }
+}
+
+Long64_t dNdEtaAnalysis::Merge(TCollection* list)
+{
+  // Merges a list of dNdEtaAnalysis objects with this one.
+  // This is needed for PROOF.
+  // Returns the number of merged objects (including this)
+
+  if (!list)
+    return 0;
+
+  if (list->IsEmpty())
+    return 1;
+
+  TIterator* iter = list->MakeIterator();
+  TObject* obj;
+
+  // sub collections
+  const Int_t nCollections = kVertexBinning + 3;
+  TList* collections[nCollections];
+  for (Int_t i=0; i<nCollections; ++i)
+    collections[i] = new TList;
+
+  Int_t count = 0;
+  while ((obj = iter->Next()))
+  {
+    dNdEtaAnalysis* entry = dynamic_cast<dNdEtaAnalysis*> (obj);
+    if (entry == 0)
+      continue;
+
+    collections[0]->Add(entry->fEtaVsVtx);
+    collections[1]->Add(entry->fEtaVsVtxUncorrected);
+    collections[2]->Add(entry->fVtx);
+
+    for (Int_t i=0; i<kVertexBinning; ++i)
+      collections[3+i]->Add(entry->fdNdEta[i]);
+
+    ++count;
+  }
+
+  fEtaVsVtx->Merge(collections[0]);
+  fEtaVsVtxUncorrected->Merge(collections[1]);
+  fVtx->Merge(collections[2]);
+  for (Int_t i=0; i<kVertexBinning; ++i)
+    fdNdEta[i]->Merge(collections[3+i]);
+
+  for (Int_t i=0; i<nCollections; ++i)
+    delete collections[i];
+
+  return count+1;
+}