3 * @author Valentina Zaccolo
4 * @date Tue Mar 05 12:56:56 2013
6 * @brief Bayesian Method Unfolding Script
13 #include <TDirectory.h>
18 #include "RooUnfoldBayes.h"
19 #include "RooUnfoldResponse.h"
23 * Get an object from a directory
26 * @param name Name of object
28 * @return Pointer to object or null if not found
30 TObject* GetObject(TDirectory* d, const char* name)
32 TObject* o = d->Get(name);
34 Error("GetCollection", "%s not found in %s", name, d->GetName());
40 * Get an object from a collection
43 * @param name Name of object
45 * @return Pointer to object or null if not found
47 TObject* GetObject(TCollection* d, const char* name)
49 TObject* o = d->FindObject(name);
51 Error("GetCollection", "%s not found in %s", name, d->GetName());
58 * Get a collection from a directory
60 * @param d Parent directory
61 * @param name Name of collection
63 * @return Pointer to collection or null
65 TCollection* GetCollection(TDirectory* d, const char* name)
67 return static_cast<TCollection*>(GetObject(d, name));
70 * Get a collection from a collection
72 * @param d Parent collection
73 * @param name Name of collection
75 * @return Pointer to collection or null
77 TCollection* GetCollection(TCollection* d, const char* name)
79 return static_cast<TCollection*>(GetObject(d, name));
82 * Get a 1D-histogram from a directory
84 * @param d Parent directory
85 * @param name Name of histogram
87 * @return Pointer to histogram or null
89 TH1* GetH1(TDirectory* d, const char* name)
91 return static_cast<TH1*>(GetObject(d, name));
94 * Get a 1D-histogram from a collection
96 * @param d Parent collectoin
97 * @param name Name of histogram
99 * @return Pointer to histogram or null
101 TH1* GetH1(TCollection* d, const char* name)
103 return static_cast<TH1*>(GetObject(d, name));
106 * Get a 2D-histogram from a directory
108 * @param d Parent directory
109 * @param name Name of histogram
111 * @return Pointer to histogram or null
113 TH2* GetH2(TDirectory* d, const char* name)
115 return static_cast<TH2*>(GetObject(d, name));
118 * Get a 2D-histogram from a collection
120 * @param d Parent collection
121 * @param name Name of histogram
123 * @return Pointer to histogram or null
125 TH2* GetH2(TCollection* d, const char* name)
127 return static_cast<TH2*>(GetObject(d, name));
132 * @param filename File name
133 * @param readNotWrite If true, open read-only
135 * @return Pointer to file, or null
137 TFile* OpenFile(const char* filename, Bool_t readNotWrite)
139 TFile* ret = TFile::Open(filename, readNotWrite ? "READ" : "RECREATE");
141 Error("OpenFile", "Failed to open the file \"%s\"", filename);
146 /** A type definition to make it easier to switch to TDirectory structure */
147 typedef TCollection Dir;
150 * Get histograms for @f$\eta@f$-bin from @a lowLim to @a highLim and
153 * @param lowLim Lower @f$\eta@f$ limit
154 * @param highLim Upper @f$\eta@f$ limit
155 * @param resp Directory containing response matrices
156 * @param data Directory containing data histogram
157 * @param out Output directory
159 void GetHistos(Double_t lowLim,
165 * Do the actual unfolding and corrections
167 * @param response Response matrix
168 * @param measured Measured distribution
169 * @param mcHist MC truth distribution
170 * @param esdHist MC distribution after ESD event selection
172 void ProcessUnfold(TH2* response, TH1* measured, TH1* MCHist, TH1* ESDHist);
174 * Caclulate the trigger bias correction
176 * @param mcHist MC truth distribution
177 * @param esdHist MC distribution after ESD event selection
179 * @return Histogram of trigger bias correction
181 TH1* GetTriggerBias(TH1* mcHist, TH1* esdHist);
183 * Apply the trigger bias correction to the unfolded result
185 * @param unfoldBefore Unfolded measured distribution
186 * @param triggerBias Trigger bias correction
187 * @param mcHist MC truth distribution
189 void DoCorrection(TH1* unfoldBefore, TH1* triggerBias, TH1* mcHist);
193 * Main Loop to call eta range
195 * @param respFile File containing response matrices
196 * @param dataFile File containing measured distributions
197 * @param outFile Output file
199 void UnfoldMult(const char* respFile="forward_response.root",
200 const char* dataFile="forward_multiplicy.root",
201 const char* outFile="unfolded.root")
203 TFile* output = OpenFile(outFile,false);
204 TFile* resp = OpenFile(respFile, true);
205 TFile* data = OpenFile(dataFile, true);
206 if (!output || !resp || !data) return;
208 Dir* topResp = GetCollection(resp, "ResponseMatricesSums");
209 Dir* topData = GetCollection(data, "MultSums");
210 if (!topData || !topResp) return;
212 Double_t limits[] = {5.1, 3.4, 3.0, 2.4, 2.0, 1.5, 1.0, 0.5, 0. };
213 Double_t* limit = limits;
215 while ((*limit) > 0.1) {
216 if ((*limit) >5.) GetHistos(-3.4, (*limit), topResp, topData, output);
217 else GetHistos(-(*limit), (*limit), topResp, topData, output);
226 //____________________________________________________________________
227 void GetHistos(Double_t lowLim,
233 // Format the name of the bin
234 TString sLow( TString::Format("%+03d",int(10*lowLim)));
235 TString sHigh(TString::Format("%+03d",int(10*highLim)));
236 sLow .ReplaceAll("+", "plus");
237 sLow .ReplaceAll("-", "minus");
238 sHigh.ReplaceAll("+", "plus");
239 sHigh.ReplaceAll("-", "minus");
240 TString name = TString::Format("%s_%s", sLow.Data(), sHigh.Data());
241 TDirectory* dir = out->mkdir(name);
244 // Get our collections
245 Dir* listResp = GetCollection(topResp, name);
246 Dir* listData = GetCollection(topData,name);
247 if(!listResp || !listData) return;
249 // Get the MC histograms
250 TH2* response = GetH2(listResp, "responseMatrix");
251 TH1* mcHist = GetH1(listResp, "fMCNSD");
252 TH1* esdHist = GetH1(listResp, "fESDNSD");
253 if (!response || !mcHist || !esdHist) return;
255 // Get the data histogram
256 TH1* measured = GetH1(listData, "mult");
257 if(!measured) return;
259 // Now do the unfolding
260 ProcessUnfold(response, measured, mcHist, esdHist);
265 //____________________________________________________________________
266 void ProcessUnfold(TH2* response, TH1* measured, TH1* mcHist, TH1* esdHist)
268 Int_t nX = response->GetNbinsX();
269 Int_t nY = response->GetNbinsY();
270 TH1* projY = static_cast<TH1*>(response->ProjectionY("projY",1,nX,""));
271 TH1* projX = static_cast<TH1*>(response->ProjectionX("projX",1,nY,""));
272 projX->SetDirectory(0);
273 projY->SetDirectory(0);
275 TH2* responseTranspose = static_cast<TH2*>(response->Clone("response"));
276 for(Int_t i = 1; i <= nX; i++) {
277 for(Int_t j = 1; j <= nY; j++) {
278 responseTranspose->SetBinContent(j,i, response->GetBinContent(i,j));
279 responseTranspose->SetBinError(j,i, response->GetBinError(i,j));
283 RooUnfoldResponse* responseObject = new RooUnfoldResponse(projY, projX,
286 RooUnfold* unfold = new RooUnfoldBayes(responseObject,
288 TH1* unfolded = static_cast<TH1*>(unfold->Hreco());
289 TH1* triggerBias = GetTriggerBias(mcHist, esdHist);
290 DoCorrection(unfolded, TriggerBias, mcHist);
292 Double_t scale_unf = 1/unfolded->Integral();
293 unfolded->Scale(scale_unf);
294 unfolded->Write("Unfolded");
296 Double_t scale_raw = 1/measured->Integral();
297 measured->Scale(scale_raw);
298 measured->Write("Raw");
299 triggerBias->Write("TriggerBiasCorr");
304 //____________________________________________________________________
305 TH1* GetTriggerBias(TH1* mcHist, TH1* esdHist)
310 TH1* corr = new TH1D("corr", "corr", 40, -0.5, 39.5);
311 for (Int_t j=1; j<corr->GetNbinsX(); j++) {
312 Double_t errSqr = 0.;
313 Double_t cESD = esdHist->GetBinContent(j);
314 Double_t cMS = mcHist->GetBinContent(j);
315 Double_t c = (cMC == 0 ? 1 :
316 cESD == 0 ? 1/cMC : cESD/cMC);
317 corr->SetBinContent(j, c);
318 corr->SetBinError(j, c*TMath::Sqrt(errSqr));
323 //____________________________________________________________________
324 void DoCorrection(TH1* unfoldBefore, TH1* triggerBias, TH1*)
326 for (Int_t k=1; k<=35; k++) {
327 Double_t tb = triggerBias->GetBinContent(k);
328 Double_t ub = unfoldBefore->GetBinContent(k);
329 if (tb > 1e-5 && ub > 0) {
330 unfoldBefore->SetBinContent(k, ub / tb);
331 Double_t eub = UnfoldBefore->GetBinError(k);
332 Double_t etb = TriggerBias->GetBinError(k);
333 unfoldBefore->SetBinError(k, TMath::Sqrt(TMath::Power(eub/ub)+
334 TMath::Power(etb/tb))*ub/tb);