2 HistoUtils_weightedmean(TH1 *h1, TH1 *h2)
5 TH1 *ho = (TH1 *)h1->Clone("ho");
7 Double_t val1, val2, w1, w2, mean, meane;
8 for (Int_t i = 0; i < ho->GetNbinsX(); i++) {
9 val1 = h1->GetBinContent(i + 1);
10 w1 = 1. / (h1->GetBinError(i + 1) * h1->GetBinError(i + 1));
11 val2 = h2->GetBinContent(i + 1);
12 w2 = 1. / (h2->GetBinError(i + 1) * h2->GetBinError(i + 1));
14 if (val1 == 0 && val2 == 0) continue;
16 mean = (w1 * val1 + w2 * val2) / (w1 + w2);
17 meane = TMath::Sqrt(1. / (w1 + w2));
19 ho->SetBinContent(i + 1, mean);
20 ho->SetBinError(i + 1, meane);
26 //__________________________________________________________________
29 HistoUtils_smartdifference(TH1 *hnum, TH1 *hden)
32 TH1 *hr = (TH1 *)hnum->Clone("hr");
35 for (Int_t i = 0; i < hr->GetNbinsX(); i++) {
36 if (hnum->GetBinError(i + 1) <= 0.) continue;
37 ref = hden->Interpolate(hr->GetBinCenter(i + 1));
38 if (ref <= 0.) continue;
39 hr->SetBinContent(i + 1, (hnum->GetBinContent(i + 1) - ref) / ref);
40 hr->SetBinError(i + 1, hnum->GetBinError(i + 1) / ref);
45 //__________________________________________________________________
48 HistoUtils_smartratio(TH1 *hnum, TH1 *hden)
51 TH1 *hr = (TH1 *)hnum->Clone("hr");
54 for (Int_t i = 0; i < hr->GetNbinsX(); i++) {
55 if (hnum->GetBinError(i + 1) <= 0.) continue;
56 ref = hden->Interpolate(hr->GetBinCenter(i + 1));
57 if (ref <= 0.) continue;
58 hr->SetBinContent(i + 1, hnum->GetBinContent(i + 1) / ref);
59 hr->SetBinError(i + 1, hnum->GetBinError(i + 1) / ref);
64 //__________________________________________________________________
67 HistoUtils_smartratio(TH1 *hnum, TGraph *hden)
70 TH1 *hr = (TH1 *)hnum->Clone("hr");
73 for (Int_t i = 0; i < hr->GetNbinsX(); i++) {
74 if (hnum->GetBinError(i + 1) <= 0.) continue;
75 ref = hden->Eval(hr->GetBinCenter(i + 1));
76 if (ref <= 0.) continue;
77 hr->SetBinContent(i + 1, hnum->GetBinContent(i + 1) / ref);
78 hr->SetBinError(i + 1, hnum->GetBinError(i + 1) / ref);
83 //__________________________________________________________________
85 HistoUtils_drawthemall(const Char_t *filename, Int_t sleepms = 100)
87 TFile *f1 = TFile::Open(filename);
88 TList *l1 = f1->GetListOfKeys();
91 for (Int_t i = 0; i < l1->GetEntries(); i++) {
92 name = l1->At(i)->GetName();
96 gSystem->Sleep(sleepms);
101 //__________________________________________________________________
103 HistoUtils_autoratio(const Char_t *f1name, const Char_t *f2name, const Char_t *outname, const Char_t *title = NULL)
106 TFile *f1 = TFile::Open(f1name);
107 TFile *f2 = TFile::Open(f2name);
108 TFile *fo = TFile::Open(outname, "RECREATE");
109 TList *l1 = f1->GetListOfKeys();
114 for (Int_t i = 0; i < l1->GetEntries(); i++) {
115 name = l1->At(i)->GetName();
116 h1 = (TH1D *)f1->Get(name);
117 h2 = (TH1D *)f2->Get(name);
118 if (!h1 || !h2) continue;
134 //__________________________________________________________________
136 HistoUtils_autosystematics(const Char_t *f1name, const Char_t *f2name, const Char_t *outname, const Char_t *title = NULL)
139 TFile *f1 = TFile::Open(f1name);
140 TFile *f2 = TFile::Open(f2name);
141 if (!f1 || !f1->IsOpen() || !f2 || !f2->IsOpen()) return;
142 TFile *fo = TFile::Open(outname, "RECREATE");
143 TList *l1 = f1->GetListOfKeys();
148 for (Int_t i = 0; i < l1->GetEntries(); i++) {
149 name = l1->At(i)->GetName();
150 h1 = (TH1D *)f1->Get(name);
151 h2 = (TH1D *)f2->Get(name);
152 if (!h1 || !h2) continue;
155 Double_t val1, val2, vald, vale1, vale2, valde;
156 for (Int_t ii = 0; ii < hd->GetNbinsX(); ii++) {
157 val1 = h1->GetBinContent(ii + 1);
158 vale1 = h1->GetBinError(ii + 1);
159 val2 = h2->GetBinContent(ii + 1);
160 vale2 = h2->GetBinError(ii + 1);
161 if (val2 == 0.) continue;
162 vald = (val1 - val2) / val2;
163 valde = TMath::Sqrt(TMath::Abs((vale1 * vale1 - vale2 * vale2))) / val2;
164 hd->SetBinContent(ii + 1, vald);
165 hd->SetBinError(ii + 1, valde);
181 //__________________________________________________________________
184 HistoUtils_ratio(const Char_t *f1name, const Char_t *f2name, const Char_t *h1name, const Char_t *h2name)
187 if (!f2name) f2name = f1name;
188 TFile *f1 = TFile::Open(f1name);
189 TFile *f2 = TFile::Open(f2name);
191 if (!h2name) h2name = h1name;
192 TH1 *h1 = (TH1 *)f1->Get(h1name);
193 TH1 *h2 = (TH1 *)f2->Get(h2name);
195 TH1 *hr = h1->Clone("hr");
203 //__________________________________________________________________
206 HistoUtils_systematics(const Char_t *f1name, const Char_t *f2name, const Char_t *h1name, const Char_t *h2name)
209 if (!f2name) f2name = f1name;
210 TFile *f1 = TFile::Open(f1name);
211 TFile *f2 = TFile::Open(f2name);
213 if (!h2name) h2name = h1name;
214 TH1 *h1 = (TH1 *)f1->Get(h1name);
215 TH1 *h2 = (TH1 *)f2->Get(h2name);
217 TH1 *hd = h1->Clone("hd");
218 Double_t val1, val2, vald, vale1, vale2, valde;
219 for (Int_t i = 0; i < h1->GetNbinsX(); i++) {
220 val1 = h1->GetBinContent(i + 1);
221 vale1 = h1->GetBinError(i + 1);
222 val2 = h2->GetBinContent(i + 1);
223 vale2 = h2->GetBinError(i + 1);
224 if (val2 == 0.) continue;
225 vald = (val1 - val2) / val2;
226 valde = TMath::Sqrt(TMath::Abs((vale1 * vale1 - vale2 * vale2))) / val2;
227 hd->SetBinContent(i + 1, vald);
228 hd->SetBinError(i + 1, valde);
235 //__________________________________________________________________
237 HistoUtils_BinLogX(TH1 *h)
239 TAxis *axis = h->GetXaxis();
240 Int_t bins = axis->GetNbins();
241 Axis_t from = axis->GetXmin();
242 Axis_t to = axis->GetXmax();
243 Axis_t width = (to - from) / bins;
244 Axis_t *new_bins = new Axis_t[bins + 1];
245 for (int i = 0; i <= bins; i++) new_bins[i] = TMath::Power(10, from + i * width);
246 axis->Set(bins, new_bins);
250 //__________________________________________________________________
252 HistoUtils_BinNormX(TH1 *h)
254 TAxis *axis = h->GetXaxis();
255 Int_t bins = axis->GetNbins();
258 for (Int_t i = 0; i < bins; i++) {
259 c = h->GetBinContent(i + 1);
260 ec = h->GetBinError(i + 1);
261 w = axis->GetBinWidth(i + 1);
264 h->SetBinContent(i + 1, c);
265 h->SetBinError(i + 1, ec);
269 //__________________________________________________________________
272 HistoUtils_FitPeak(TF1 *fitFunc, TH2 *h, Float_t startSigma, Float_t nSigmaMin, Float_t nSigmaMax, Int_t minIntegral = 100., const Char_t *basename = "hParam", Bool_t monitor = kFALSE)
275 /* gaus function if not specified */
277 fitFunc = (TF1*)gROOT->GetFunction("gaus");
279 /* prepare output histos */
280 TObjArray *outArray = new TObjArray();
281 Int_t npars = fitFunc->GetNpar();
282 TH1D *hpx = h->ProjectionX("hpx");
284 for (Int_t ipar = 0; ipar < npars; ipar++) {
285 hParam = new TH1D(*hpx);
286 hParam->SetName(Form("%s_%d", basename, ipar));
288 outArray->Add(hParam);
291 /* loop over x-bins */
292 for (Int_t ibin = 0; ibin < hpx->GetNbinsX(); ibin++) {
295 if (hpx->GetBinContent(ibin + 1) < minIntegral) continue;
297 TH1D *hpy = h->ProjectionY("hpy", ibin + 1, ibin + 1);
299 if (HistoUtils_FitPeak(fitFunc, hpy, startSigma, nSigmaMin, nSigmaMax) != 0) {
303 /* setup output histos */
304 for (Int_t ipar = 0; ipar < npars; ipar++) {
305 hParam = (TH1D *)outArray->At(ipar);
306 hParam->SetBinContent(ibin + 1, fitFunc->GetParameter(ipar));
307 hParam->SetBinError(ibin + 1, fitFunc->GetParError(ipar));
311 hpy->SetMarkerStyle(20);
312 hpy->SetMarkerColor(4);
314 fitFunc->Draw("same");
324 /* return output array */
328 //__________________________________________________________________
331 HistoUtils_FitPeak(TF1 *fitFunc, TH1 *h, Float_t startSigma, Float_t nSigmaMin, Float_t nSigmaMax)
334 Double_t fitCent = h->GetBinCenter(h->GetMaximumBin());
335 Double_t fitMin = fitCent - nSigmaMin * startSigma;
336 Double_t fitMax = fitCent + nSigmaMax * startSigma;
337 if (fitMin < h->GetXaxis()->GetXmin()) fitMin = h->GetXaxis()->GetXmin();
338 if (fitMax > h->GetXaxis()->GetXmax()) fitMax = h->GetXaxis()->GetXmax();
339 fitFunc->SetParameter(1, fitCent);
340 fitFunc->SetParameter(2, startSigma);
341 Int_t fitres = h->Fit(fitFunc, "WWq0", "", fitMin, fitMax);
342 if (fitres != 0) return fitres;
343 /* refit with better range */
344 for (Int_t i = 0; i < 3; i++) {
345 fitCent = fitFunc->GetParameter(1);
346 fitMin = fitCent - nSigmaMin * fitFunc->GetParameter(2);
347 fitMax = fitCent + nSigmaMax * fitFunc->GetParameter(2);
348 if (fitMin < h->GetXaxis()->GetXmin()) fitMin = h->GetXaxis()->GetXmin();
349 if (fitMax > h->GetXaxis()->GetXmax()) fitMax = h->GetXaxis()->GetXmax();
350 fitres = h->Fit(fitFunc, "q0", "", fitMin, fitMax);
351 if (fitres != 0) return fitres;
357 //__________________________________________________________________
360 HistoUtils_Function2Profile(TF1 *fin, TProfile *p, Int_t ntry = 10000)
363 TF1 *f = new TF1(*fin);
364 Int_t npars = f->GetNpar();
367 for (Int_t ipar = 0; ipar < npars; ipar++) {
368 par[ipar] = f->GetParameter(ipar);
369 pare[ipar] = f->GetParError(ipar);
373 for (Int_t ibin = 0; ibin < p->GetNbinsX(); ibin++) {
374 for (Int_t itry = 0; itry < ntry; itry++) {
375 for (Int_t ipar = 0; ipar < npars; ipar++)
376 f->SetParameter(ipar, gRandom->Gaus(par[ipar], pare[ipar]));
377 x = gRandom->Uniform(p->GetXaxis()->GetBinLowEdge(ibin + 1), p->GetXaxis()->GetBinUpEdge(ibin + 1));
378 p->Fill(x, f->Eval(x));