AliPhysics  efbe636 (efbe636)
AliEmcalList.cxx
Go to the documentation of this file.
1 /**************************************************************************
2  * Copyright(c) 1998-2016, ALICE Experiment at CERN, All rights reserved. *
3  * *
4  * Author: R. Haake *
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 #include "TList.h"
17 #include "TH1.h"
18 #include "TH2.h"
19 #include "THnBase.h"
20 #include "AliLog.h"
21 #include "AliEmcalList.h"
22 
23 #ifdef WITH_ROOUNFOLD
24 #include "RooUnfoldResponse.h"
25 #endif
26 
28 ClassImp(AliEmcalList)
30 
31 //________________________________________________________________________
32 AliEmcalList::AliEmcalList() : TList(), fUseScaling(kFALSE)
33 {
34  // constructor
35 }
36 
38 //________________________________________________________________________
40 {
41  if(!hlist)
42  return 0;
43 
44  AliInfo(Form("Scaled merging for list %s is %sactivated.", hlist->GetName(),(fUseScaling) ? "" : "not "));
45  if(!fUseScaling)
46  return TList::Merge(hlist);
47 
48  // #### Retrieve xsection and ntrials from histograms in this list
49  // NOTE: they must be directly added to the AliEmcalList, not nested in sublists!
50  TH1* xsection = static_cast<TH1*>(FindObject("fHistXsection"));
51  TH1* ntrials = static_cast<TH1*>(FindObject("fHistTrials"));
52 
53  // #### If xsection or ntrials are not given, go to fatal
54  if(!(xsection && ntrials))
55  AliFatal("Scaled merging is active but AliEmcalList does not contain fHistXsection or fHistTrials. Do not activate scaling for those lists or include these histograms.");
56 
57  // #### Do the scaling only on the last level when we mix several pt hard bins
58  // This is easy to find out checking the std histos in hlist
59  Bool_t isLastLevel = IsLastMergeLevel(hlist);
60 
61  // #### On last level, do the scaling
62  if(isLastLevel)
63  {
64  AliInfo(Form("===== LAST LEVEL OF MERGING ====="));
65 
66  // Scale all histograms in this list
67  Double_t scalingFactor = GetScalingFactor(xsection, ntrials);
68  ScaleAllHistograms(this, scalingFactor);
69 
70  // Scale all histograms in the lists to be merged
71  TIter listIterator(hlist);
72  while (AliEmcalList* tmpList = static_cast<AliEmcalList*>(listIterator()))
73  {
74  xsection = static_cast<TH1*>(tmpList->FindObject("fHistXsection"));
75  ntrials = static_cast<TH1*>(tmpList->FindObject("fHistTrials"));
76  scalingFactor = GetScalingFactor(xsection, ntrials);
77  ScaleAllHistograms(tmpList, scalingFactor);
78  }
79  }
80 
81  AliInfo("Merge() done.");
82 
83  TList::Merge(hlist);
84  return hlist->GetEntries() + 1;
85 }
86 
88 //________________________________________________________________________
90 {
91  TIter listIterator(hlist);
92  while (TObject* listObject = listIterator())
93  {
94  // Recurse into nested all lists
95  TCollection* sublist = dynamic_cast<TCollection*>(listObject);
96  if(sublist)
97  {
98  ScaleAllHistograms(sublist, scalingFactor);
99  continue;
100  }
101 
102  // Otherwise, scale TH1-derived / THnBase-derived histograms
103  if (!(listObject->InheritsFrom(TH1::Class()) || listObject->InheritsFrom(THnBase::Class())))
104  continue;
105 
106  // Don't scale profiles and histograms used for scaling
107  TString histogram_class (listObject->ClassName());
108  TString histogram_name (listObject->GetName());
109  if (histogram_name.Contains("fHistXsection") || histogram_name.Contains("fHistTrials") || histogram_name.Contains("fHistEvents"))
110  {
111  AliInfo(Form("Histogram %s will not be scaled, because a scaling histogram", listObject->GetName()));
112  continue;
113  }
114  if (histogram_class.Contains("TProfile"))
115  {
116  AliInfo(Form("Histogram %s will not be scaled, because it is a TProfile", listObject->GetName()));
117  continue;
118  }
119 
120  TH1 *histogram = dynamic_cast<TH1 *>(listObject);
121  if(histogram) {
122  // Handle TH1/TH2/TH3
123  histogram->Sumw2();
124  histogram->Scale(scalingFactor);
125  } else if(listObject->InheritsFrom(THnBase::Class())){
126  // Handle THn
127  THnBase *histogramND = dynamic_cast<THnBase *>(listObject);
128  histogramND->Sumw2();
129  histogramND->Scale(scalingFactor);
130  }
131 #ifdef WITH_ROOUNFOLD
132  else if(listObject->InheritsFrom(RooUnfoldResponse::Class())){
133  RooUnfoldResponse *response = dynamic_cast<RooUnfoldResponse *>(listObject);
134  if(auto measured = response->Hmeasured()) measured->Scale(scalingFactor);
135  if(auto fakes = response->Hfakes()) fakes->Scale(scalingFactor);
136  if(auto truth = response->Htruth()) truth->Scale(scalingFactor);
137  if(auto responseND = response->Hresponse()) responseND->Scale(scalingFactor);
138  }
139 #endif
140  AliInfo(Form("Histogram %s (%s) was scaled...", listObject->GetName(), histogram_class.Data()));
141 
142  }
143 }
144 
146 //________________________________________________________________________
148 {
149  Int_t binNumber = GetFilledBinNumber(xsection);
150  if(!binNumber)
151  {
152  AliInfo("List already scaled or scaling invalid. Scaling factor = 1.");
153  return 1.0;
154  }
155 
156  Double_t valNTRIALS = ntrials->GetBinContent(binNumber);
157  Double_t valXSEC = xsection->GetBinContent(binNumber);
158  Double_t scalingFactor = 0;
159  if(valNTRIALS)
160  scalingFactor = valXSEC/valNTRIALS;
161 
162  AliInfo(Form("## Bin %i: trials=%f, xsec=%f -> scaling=%f", binNumber, valNTRIALS, valXSEC, scalingFactor));
163  return scalingFactor;
164 }
165 
168 //________________________________________________________________________
170 {
171  // Get the pt hard bin number that is filled in this object
172  TH1* xsection = static_cast<TH1*>(FindObject("fHistXsection"));
173  Int_t binNumberThis = GetFilledBinNumber(xsection);
174 
175  TIter listIterator(collection);
176  while (AliEmcalList* tmpList = static_cast<AliEmcalList*>(listIterator()))
177  {
178  // For every list in the collection, test if they are from different pt hard bins
179  xsection = static_cast<TH1*>(tmpList->FindObject("fHistXsection"));
180  if(binNumberThis != GetFilledBinNumber(xsection))
181  return kTRUE;
182  }
183  return kFALSE;
184 }
185 
189 //________________________________________________________________________
191 {
192  AliInfo(Form("%s: nbinsX=%i", hist->GetName(), hist->GetNbinsX()));
193 
194  Int_t binFound = 0;
195  for(Int_t i=1; i<=hist->GetNbinsX(); i++)
196  {
197  AliInfo(Form("%s: bin=%i, val=%f", hist->GetName(), i, hist->GetBinContent(i)));
198  if(hist->GetBinContent(i))
199  {
200  if(!binFound)
201  binFound = i;
202  else
203  {
204  return 0; // more than one bin filled (e.g. when merging is partly done)
205  }
206  }
207  }
208 
209  if(!binFound)
210  AliError("No bin filled in scaling histogram.");
211 
212  // 0 if no bin found or more than one filled, otherwise returns bin number
213  return binFound;
214 }
double Double_t
Definition: External.C:58
Bool_t fUseScaling
if true, scaling will be done. if false AliEmcalList simplifies to TList
Definition: AliEmcalList.h:41
long long Long64_t
Definition: External.C:43
Int_t GetFilledBinNumber(TH1 *hist)
void ScaleAllHistograms(TCollection *hlist, Double_t scalingFactor)
Function that does the scaling of all histograms in hlist recursively.
int Int_t
Definition: External.C:63
Bool_t IsLastMergeLevel(TCollection *collection)
Double_t GetScalingFactor(TH1 *xsection, TH1 *ntrials)
Helper function scaling factor.
Long64_t Merge(TCollection *hlist)
Overridden Merge function.
TH1 * Merge(const TH1 *cen, const TH1 *fwd, Double_t &xlow, Double_t &xhigh)
TObject * FindObject(int bin, const char *nameH, const TList *lst, Bool_t normPerEvent=kTRUE)
Enhanced TList-derived class that implements correct merging for pt_hard binned production.
Definition: AliEmcalList.h:25
bool Bool_t
Definition: External.C:53
Definition: External.C:196