AliPhysics  vAN-20150723 (baea2bf)
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Macros
AliAnaWeights.cxx
Go to the documentation of this file.
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  * *
4  * Author: The ALICE Off-line Project. *
5  * Contributors are mentioned in the code where appropriate. *
6  * *
7  * Permission to use, copy, modify and distribute this software and its *
8  * documentation strictly for non-commercial purposes is hereby granted *
9  * without fee, provided that the above copyright notice appears in all *
10  * copies and that both the copyright notice and this permission notice *
11  * appear in the supporting documentation. The authors make no claims *
12  * about the suitability of this software for any purpose. It is *
13  * provided "as is" without express or implied warranty. *
14  **************************************************************************/
15 
16 // Root
17 #include <TSystem.h>
18 #include <TFile.h>
19 #include <TTree.h>
20 #include <TKey.h>
21 #include <TH1F.h>
22 #include <TList.h>
23 #include <TProfile.h>
24 
25 // AliRoot
26 #include "AliAnalysisManager.h"
27 #include "AliLog.h"
28 
29 #include "AliAnaWeights.h"
30 
34 
35 //______________________________________________________________
37 //______________________________________________________________
39 : TObject(),
40  fhCentralityWeight(0),
41  fCentrality(0),
42  fUseCentralityWeight(0),
43  fCurrFileName(0),
44  fCheckMCCrossSection(kFALSE),
45  fhXsec(0),
46  fhTrials(0),
47  fDebug(0)
48 {
49 }
50 
51 //____________________________________________________
54 //____________________________________________________
56 {
57  TList * outputContainer = new TList() ;
58  outputContainer->SetName("MCWeightHistogram") ;
59 
60  if(!fCheckMCCrossSection) return outputContainer;
61 
62  outputContainer->SetOwner(kFALSE);
63 
64  fhXsec = new TH1F("hXsec","xsec from pyxsec.root",1,0,1);
65  fhXsec->GetXaxis()->SetBinLabel(1,"<#sigma>");
66  outputContainer->Add(fhXsec);
67 
68  fhTrials = new TH1F("hTrials","trials root file",1,0,1);
69  fhTrials->GetXaxis()->SetBinLabel(1,"#sum{ntrials}");
70  outputContainer->Add(fhTrials);
71 
72  return outputContainer ;
73 }
74 
75 //__________________________________
80 //_________________________________
82 {
83  Double_t weight = 1.;
84 
86  {
87  Double_t temp = GetPythiaCrossSection() ;
88 
89  AliDebug(1,Form("MC pT-hard weight: %e",temp));
90 
91  weight*=temp;
92  }
93 
95  {
96  Double_t temp = fhCentralityWeight->GetBinContent(fhCentralityWeight->FindBin(fCentrality));
97 
98  AliDebug(1,Form("Centrality %2.1f, weight: %2.2f",fCentrality,temp));
99 
100  weight*=temp;
101  }
102 
103  AliDebug(1,Form("Event weight %e",weight));
104 
105  return weight ;
106 }
107 
108 //_____________________________________________
109 //
113 //_____________________________________________
115 {
116  // Fetch the aod also from the input in,
117  // have todo it in notify
118 
119  Float_t xsection = 0;
120  Float_t trials = 1;
121  Float_t avgTrials = 0;
122 
123  TTree *tree = AliAnalysisManager::GetAnalysisManager()->GetTree();
124  if ( !tree ) return 1;
125 
126  TFile *curfile = tree->GetCurrentFile();
127 
128  if ( !curfile ) return 1;
129 
130  // Check if file not accessed previously, if so
131  // return the previously calculated weight
132  if(fCurrFileName == curfile->GetName()) return fMCWeight;
133 
134  fCurrFileName = TString(curfile->GetName());
135 
136  if ( !fhXsec || !fhTrials )
137  {
138  AliInfo(Form("%s%d No Histogram fhXsec",(char*)__FILE__,__LINE__));
139  return 1;
140  }
141 
142  Bool_t ok = GetPythiaInfoFromFile(fCurrFileName,xsection,trials);
143 
144  if ( !ok )
145  {
146  AliWarning("Parameters from file not recovered properly");
147  return 1;
148  }
149 
150  fhXsec->Fill("<#sigma>",xsection);
151 
152  // average number of trials
153  Float_t nEntries = (Float_t)tree->GetTree()->GetEntries();
154 
155  if(trials >= nEntries && nEntries > 0.) avgTrials = trials/nEntries;
156 
157  fhTrials->Fill("#sum{ntrials}",avgTrials);
158 
159  AliInfo(Form("xs %e, trial %e, avg trials %2.2f, events per file %e",
160  xsection,trials,avgTrials,nEntries));
161 
162  AliDebug(1,Form("Reading File %s",curfile->GetName()));
163 
164  if(avgTrials > 0.)
165  {
166  fMCWeight = xsection / avgTrials ;
167 
168  AliInfo(Form("MC Weight: %e",fMCWeight));
169  }
170  else
171  {
172  AliWarning(Form("Average number of trials is NULL!! Set weight to 1: xs : %e, trials %e, entries %e",
173  xsection,trials,nEntries));
174 
175  fMCWeight = 1;
176  }
177 
178  return fMCWeight ;
179 }
180 
181 //_______________________________________________________________________________________
188 //_______________________________________________________________________________________
189 Bool_t AliAnaWeights::GetPythiaInfoFromFile(TString file,Float_t & xsec,Float_t & trials)
190 {
191  xsec = 0;
192  trials = 1;
193 
194  if(file.Contains("root_archive.zip#"))
195  {
196  Ssiz_t pos1 = file.Index("root_archive",12,0,TString::kExact);
197  Ssiz_t pos = file.Index("#",1,pos1,TString::kExact);
198  Ssiz_t pos2 = file.Index(".root",5,TString::kExact);
199  file.Replace(pos+1,pos2-pos1,"");
200  }
201  else
202  {
203  // not an archive take the basename....
204  file.ReplaceAll(gSystem->BaseName(file.Data()),"");
205  }
206 
207  //Printf("%s",file.Data());
208 
209  TFile *fxsec = TFile::Open(Form("%s%s",file.Data(),"pyxsec.root")); // problem that we cannot really test the existance of a file in a archive so we have to lvie with open error message from root
210  if(!fxsec)
211  {
212  // next trial fetch the histgram file
213  fxsec = TFile::Open(Form("%s%s",file.Data(),"pyxsec_hists.root"));
214  if(!fxsec)
215  {
216  // not a severe condition but inciate that we have no information
217  return kFALSE;
218  }
219  else
220  {
221  // find the tlist we want to be independtent of the name so use the Tkey
222  TKey* key = (TKey*)fxsec->GetListOfKeys()->At(0);
223  if(!key)
224  {
225  fxsec->Close();
226  return kFALSE;
227  }
228 
229  TList *list = dynamic_cast<TList*>(key->ReadObj());
230  if(!list)
231  {
232  fxsec->Close();
233  return kFALSE;
234  }
235 
236  xsec = ((TProfile*)list->FindObject("h1Xsec")) ->GetBinContent(1);
237  trials = ((TH1F*) list->FindObject("h1Trials"))->GetBinContent(1);
238  fxsec->Close();
239  }
240  } // no tree pyxsec.root
241  else
242  {
243  TTree *xtree = (TTree*)fxsec->Get("Xsection");
244  if(!xtree)
245  {
246  fxsec->Close();
247  return kFALSE;
248  }
249 
250  UInt_t ntrials = 0;
251  Double_t xsection = 0;
252  xtree->SetBranchAddress("xsection",&xsection);
253  xtree->SetBranchAddress("ntrials",&ntrials);
254  xtree->GetEntry(0);
255  trials = ntrials;
256  xsec = xsection;
257  fxsec->Close();
258  }
259 
260  return kTRUE;
261 }
262 
263 //_______________________________________________________________________________________
269 //_______________________________________________________________________________________
270 void AliAnaWeights::InitCentralityWeightsHistogram(Int_t nbins, Int_t minCen, Int_t maxCen)
271 {
272  if ( fhCentralityWeight ) delete fhCentralityWeight ;
273 
274  fhCentralityWeight = new TH1F("hCentralityWeights","Centrality weights",nbins,minCen,maxCen);
275 
276  for(Int_t ibin = 0; ibin < fhCentralityWeight->GetNbinsX(); ibin++)
277  fhCentralityWeight->SetBinContent(ibin,1.) ;
278 }
279 
280 //_________________________________________________
282 //_________________________________________________
284 {
286 
287  return fhCentralityWeight ;
288 }
289 
ClassImp(AliAnalysisTaskTriggerRates) AliAnalysisTaskTriggerRates
TList * GetCreateOutputHistograms()
Calculate the weight to the event to be applied when filling histograms.
Definition: AliAnaWeights.h:31
TList * list
TH1F * fhCentralityWeight
Container of centrality weights.
Definition: AliAnaWeights.h:93
Float_t fCentrality
Container of centrality percentile.
Definition: AliAnaWeights.h:95
void InitCentralityWeightsHistogram(Int_t nbins=100, Int_t minCen=0, Int_t maxCen=100)
TH1F * GetCentralityWeightsHistogram()
AliAnaWeights()
Constructor.
TH1F * fhXsec
! Cross section in PYTHIA.
Double_t fMCWeight
pT-hard bin MC weight. It is used only internally.
TFile * file
TString fCurrFileName
Current file path name.
virtual Double_t GetWeight()
Bool_t fUseCentralityWeight
Return the centratlity weight.
Definition: AliAnaWeights.h:97
Bool_t fCheckMCCrossSection
Retrieve from the pyxsec.root file the cross section, only if requested.
virtual Double_t GetPythiaCrossSection()
static Bool_t GetPythiaInfoFromFile(TString currFile, Float_t &xsec, Float_t &trials)
TH1F * fhTrials
! Number of event trials in PYTHIA.