-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathautohist.cc
382 lines (339 loc) · 11.9 KB
/
autohist.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
// .
// ..: P. Chang, [email protected]
#include "autohist.h"
///////////////////////////////////////////////////////////////////////////////////////////////////
//
//
// Auto histogram maker
//
//
///////////////////////////////////////////////////////////////////////////////////////////////////
//__________________________________________________________________________________________________
RooUtil::AutoHist::AutoHist()
{
resolution = 1000;
}
//__________________________________________________________________________________________________
RooUtil::AutoHist::~AutoHist()
{
if ( histdb.size() > 0 )
save( "autohist_output.root" );
}
//__________________________________________________________________________________________________
void RooUtil::AutoHist::save( TString ofilename, TString option )
{
TFile* ofile = new TFile( ofilename, option );
RooUtil::print( "AutoHist::save() saving histograms to " + ofilename );
ofile->cd();
for ( auto& pair_tstr_th1 : histdb )
{
TString name = pair_tstr_th1.first;
TH1* hist = pair_tstr_th1.second;
if ( hist )
{
hist->Write();
delete hist;
}
}
histdb.clear();
}
//__________________________________________________________________________________________________
void RooUtil::AutoHist::save( TFile* ofile )
{
RooUtil::print( Form( "AutoHist::save() saving histograms to %s", ofile->GetName() ) );
ofile->cd();
for ( auto& pair_tstr_th1 : histdb )
{
TString name = pair_tstr_th1.first;
TH1* hist = pair_tstr_th1.second;
if ( hist )
{
hist->Write();
delete hist;
}
}
histdb.clear();
}
//__________________________________________________________________________________________________
void RooUtil::AutoHist::fill( double xval, STRING name, double wgt )
{
TH1* hist = 0;
std::pair<MAP<STRING, TH1*>::iterator, bool> ret;
ret = histdb.insert( {name, hist} );
if ( ret.second == false )
fill( xval, ( *ret.first ).second, wgt );
else
{
hist = createHist( xval, name, wgt );
histdb[name] = hist;
}
}
//__________________________________________________________________________________________________
void RooUtil::AutoHist::fill(
double xval, STRING name, double wgt, int nbin, double min, double max,
std::vector<TString> binlabels )
{
TH1* hist = 0;
std::pair<MAP<STRING, TH1*>::iterator, bool> ret;
ret = histdb.insert( {name, hist} );
if ( ret.second == false )
fill( xval, ( *ret.first ).second, wgt, true );
else
{
hist = createFixedBinHist( xval, name, wgt, nbin, min, max );
for ( unsigned int ibin = 0; ibin < binlabels.size(); ++ibin )
hist->GetXaxis()->SetBinLabel( ibin + 1, binlabels[ibin] );
histdb[name] = hist;
}
}
//__________________________________________________________________________________________________
void RooUtil::AutoHist::fill( double xval, STRING name, double wgt, int nbin, double* bins )
{
TH1* hist = 0;
std::pair<MAP<STRING, TH1*>::iterator, bool> ret;
ret = histdb.insert( {name, hist} );
if ( ret.second == false )
fill( xval, ( *ret.first ).second, wgt, true );
else
{
hist = createFixedBinHist( xval, name, wgt, nbin, bins );
histdb[name] = hist;
}
}
//__________________________________________________________________________________________________
void RooUtil::AutoHist::fill(
double xval, double yval, STRING name, double wgt,
int nbx, double xm, double xM,
int nby, double ym, double yM,
std::vector<TString> binlabels
)
{
TH1* hist = 0;
std::pair<MAP<STRING, TH1*>::iterator, bool> ret;
ret = histdb.insert( {name, hist} );
if ( ret.second == false )
( ( TH2D* )( *ret.first ).second )->Fill( xval, yval, wgt );
else
{
hist = createFixedBinHist( xval, yval, name, wgt, nbx, xm, xM, nby, ym, yM );
for ( unsigned int ibin = 0; ibin < binlabels.size(); ++ibin )
hist->GetXaxis()->SetBinLabel( ibin + 1, binlabels[ibin] );
histdb[name] = hist;
}
}
//__________________________________________________________________________________________________
void RooUtil::AutoHist::fill(
double xval, double yval, STRING name, double wgt,
int nbx, double* bx,
int nby, double* by )
{
TH1* hist = 0;
std::pair<MAP<STRING, TH1*>::iterator, bool> ret;
ret = histdb.insert( {name, hist} );
if ( ret.second == false )
( ( TH2D* )( *ret.first ).second )->Fill( xval, yval, wgt );
else
{
hist = createFixedBinHist( xval, yval, name, wgt, nbx, bx, nby, by );
histdb[name] = hist;
}
}
//__________________________________________________________________________________________________
int RooUtil::AutoHist::getRes( double range )
{
if ( range > 1000 )
return 10;
else if ( range > 500 )
return 100;
else if ( range > 250 )
return 1000;
else if ( range > 1 )
return 1000;
else
return 10000;
}
//__________________________________________________________________________________________________
int RooUtil::AutoHist::getRes( TH1* h )
{
double max = h->GetXaxis()->GetXmax();
double min = h->GetXaxis()->GetXmin();
int nbin = h->GetNbinsX();
return nbin / ( max - min );
}
//__________________________________________________________________________________________________
TH1* RooUtil::AutoHist::createHist( double xval, TString name, double wgt, bool alreadyneg, int forceres )
{
// Create a histogram with the xval as the characteristic value.
// It takes xval, then multiply by 2 find the closest integer,
// and give histogram of nbin where each intger has 1000 bins.
// If it extends to negative value, blow it up
int bound = 2 * std::max( ( ( int ) fabs( xval ) ), 1 );
bool extendneg = ( alreadyneg || xval < 0 ) ? true : false;
double min = extendneg ? -bound : 0;
double max = bound;
int res = forceres > 0 ? forceres : getRes( max - min );
int nbin = ( max - min ) * res;
TH1D* h = new TH1D( name, name, nbin, min, max );
h->SetDirectory( 0 );
h->Sumw2();
h->Fill( xval, wgt );
return h;
}
//__________________________________________________________________________________________________
TH1* RooUtil::AutoHist::createFixedBinHist( double xval, TString name, double wgt, int n, double m, double M )
{
TH1D* h = new TH1D( name, name, n, m, M );
h->SetDirectory( 0 );
h->Sumw2();
h->Fill( xval, wgt );
return h;
}
//__________________________________________________________________________________________________
TH1* RooUtil::AutoHist::createFixedBinHist( double xval, TString name, double wgt, int n, double* bin )
{
TH1D* h = new TH1D( name, name, n, bin );
h->SetDirectory( 0 );
h->Sumw2();
h->Fill( xval, wgt );
return h;
}
//__________________________________________________________________________________________________
TH1* RooUtil::AutoHist::createFixedBinHist(
double xval, double yval, TString name, double wgt,
int xn, double xm, double xM,
int yn, double ym, double yM )
{
TH2D* h = new TH2D( name, name, xn, xm, xM, yn, ym, yM );
h->SetDirectory( 0 );
h->Sumw2();
h->Fill( xval, yval, wgt );
return h;
}
//__________________________________________________________________________________________________
TH1* RooUtil::AutoHist::createFixedBinHist(
double xval, double yval, TString name, double wgt,
int xn, double* xb,
int yn, double* yb )
{
TH2D* h = new TH2D( name, name, xn, xb, yn, yb );
h->SetDirectory( 0 );
h->Sumw2();
h->Fill( xval, yval, wgt );
return h;
}
//__________________________________________________________________________________________________
void RooUtil::AutoHist::fill( double xval, TH1*& h, double wgt, bool norebinning )
{
if ( ( xval >= h->GetXaxis()->GetXmax() || xval <= h->GetXaxis()->GetXmin() ) && !norebinning )
{
TH1D* hist = ( TH1D* ) createHist( xval, h->GetName(), wgt, h->GetXaxis()->GetXmin() < 0 );
transfer( hist, h );
delete h;
h = hist;
}
else
h->Fill( xval, wgt );
}
//__________________________________________________________________________________________________
void RooUtil::AutoHist::transfer( TH1* tohist, TH1* fromhist )
{
for ( int ibin = 0; ibin <= fromhist->GetNbinsX(); ++ibin )
{
double wgt = fromhist->GetBinContent( ibin );
double err = fromhist->GetBinError( ibin );
double val = fromhist->GetBinCenter( ibin );
if ( fabs( wgt ) )
{
int jbin = tohist->FindBin( val );
double curerror = tohist->GetBinError( jbin );
tohist->Fill( val, wgt );
tohist->SetBinError( jbin, sqrt( curerror * curerror + err * err ) );
}
}
}
//__________________________________________________________________________________________________
TH1* RooUtil::AutoHist::get( STRING name )
{
try
{
return histdb.at( name );
}
catch ( const std::out_of_range& oor )
{
RooUtil::print( "Warning! No histogram named " + name + " found!" );
return 0;
}
}
//__________________________________________________________________________________________________
void RooUtil::AutoHist::print()
{
RooUtil::print("Printing histdb ...");
for (auto& i : histdb)
{
TString msg = Form(" %s", i.second->GetName());
RooUtil::print(msg.Data());
}
}
//__________________________________________________________________________________________________
TH1* RooUtil::AutoHist::crop( TH1* orighist, int nbin, double min, double max )
{
// Checks whether this is uniform binning
// https://root-forum.cern.ch/t/taxis-getxbins/19598/2
if ( ( *( orighist->GetXaxis()->GetXbins() ) ).GetSize() == 0 )
{
int orignbin = orighist->GetNbinsX();
double origmin = orighist->GetXaxis()->GetXmin();
double origmax = orighist->GetXaxis()->GetXmax();
double origbinsize = ( origmax - origmin ) / orignbin;
if ( std::remainder( fabs( origmin - max ), origbinsize ) < 1e-9
&& std::remainder( fabs( origmin - min ), origbinsize ) < 1e-9 )
{
TH1D* rtnhist = new TH1D( orighist->GetName(), orighist->GetName(), nbin, min, max );
rtnhist->Sumw2();
rtnhist->SetDirectory( 0 );
transfer( rtnhist, orighist );
return rtnhist;
}
// Not good, I can't crop it.
else
RooUtil::error( "You are trying to crop a histogram to a binning where you can't!", __FUNCTION__ );
}
return 0;
}
//__________________________________________________________________________________________________
TH1* RooUtil::AutoHist::hadd( TH1* hist1, TH1* hist2 )
{
double min1 = hist1->GetXaxis()->GetXmin();
double max1 = hist1->GetXaxis()->GetXmax();
double min2 = hist2->GetXaxis()->GetXmin();
double max2 = hist2->GetXaxis()->GetXmax();
if ( min1 <= min2 && max1 >= max2 )
{
transfer( hist1, hist2 );
return hist1;
}
else if ( min2 <= min1 && max2 >= max1 )
{
transfer( hist2, hist1 );
return hist2;
}
else if ( max1 >= max2 && min1 >= min2 )
{
TH1* newhist = createHist( max1, hist1->GetName(), 0, true, getRes( hist1 ) );
transfer( newhist, hist1 );
transfer( newhist, hist2 );
return newhist;
}
else if ( max2 >= max1 && min2 >= min1 )
{
TH1* newhist = createHist( max2, hist2->GetName(), 0, true, getRes( hist2 ) );
transfer( newhist, hist1 );
transfer( newhist, hist2 );
return newhist;
}
else
{
RooUtil::error( "it should never reach here! send email to [email protected]", __FUNCTION__ );
return 0;
}
}