forked from xination/ROOTSCOPE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyUtility.h
706 lines (462 loc) · 17.9 KB
/
myUtility.h
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
/*
* Author : Pei-Luan Tai
* Contact: pt10f@my.fsu.edu
* Last update: Aug 24, 2019
***************************************/
#include <vector>
#include <TH1.h>
#include <TH2.h>
#include <TFile.h>
#include <TList.h>
#include <TKey.h>
#include <TString.h>
class ReadIn_and_parse {
// to do: need to clean up the comment.
private:
vector<TH1*>* fHistos;
vector<TH2*>* fHisto2ds;
Bool_t HasTH2;
TFile* fInFile;
Int_t fCntHistos;
Int_t fCntHisto2ds;
TH1C* h1c;
TH1S* h1s;
TH1I* h1i;
TH1F* h1f;
TH1D* h1d;
TH2C* h2c;
TH2S* h2s;
TH2I* h2i;
TH2F* h2f;
TH2D* h2d;
public:
// constructor
ReadIn_and_parse( vector<TH1*>* histos, vector<TH2*>* histo2ds, TString filename) {
fCntHistos = 0;
fCntHisto2ds = 0;
fHistos = histos;
fHisto2ds = histo2ds;
fInFile = new TFile( filename.Data() );
if( fInFile->IsOpen() )
{
// preparation
TList* keyList = fInFile->GetListOfKeys();
Int_t objN = keyList->GetSize();
TString histoName;
TString classType;
short cycle;
TString s1;
HasTH2 = false;
for(int i=0; i< objN; i++)
{
TKey* objKey = (TKey*) keyList-> At(i);
histoName = objKey->GetName();
classType = objKey->GetClassName();
cycle = objKey->GetCycle();
// search for TH2C, TH2S, TH2F ...
s1 = classType.SubString( "TH2" );
if ( s1 != "" ) {
HasTH2 = true;
Search_for_TH2( classType, histoName, cycle );
fCntHisto2ds++; }
// search for TH1C, TH1S, TH1F ..
s1 = classType.SubString( "TH1" );
if ( s1 != "" ) {
Search_for_TH1( classType, histoName, cycle );
fCntHistos ++; }
}
}
}
Int_t Get_loadedTH1number () { return fCntHistos; }
Int_t Get_loadedTH2number () { return fCntHisto2ds; }
Bool_t IS_hasNewTH1() { return (fCntHistos>0)? 1:0; }
Bool_t IS_hasNewTH2() { return (fCntHisto2ds>0)? 1:0; }
Bool_t Get_hasTH2() { return (fHisto2ds->size()>0)? 1:0; }
// According to the type of the 2d histogram,
// we retrieve it and assign to fHisto2d.
void Search_for_TH2( TString type, TString name, short cycle ) {
TString fullName = Form( "%s;%d", name.Data(), cycle );
if( type == "TH2C" ) {
h2c = (TH2C*) fInFile->Get( fullName.Data() ) ;
h2c->SetName( Form( "%s%f", name.Data(),gRandom->Uniform() ) );
fHisto2ds->push_back( (TH2*) h2c );
}
else if( type == "TH2S" ) {
h2s = (TH2S*) fInFile->Get( fullName.Data() ) ;
h2s->SetName( Form( "%s%f", name.Data(),gRandom->Uniform() ) );
fHisto2ds->push_back( (TH2*) h2s );
}
else if( type == "TH2I" ) {
h2i = (TH2I*) fInFile->Get( fullName.Data() ) ;
h2i->SetName( Form( "%s%f", name.Data(),gRandom->Uniform() ) );
fHisto2ds->push_back( (TH2*) h2i );
}
else if( type == "TH2F" ) {
h2f = (TH2F*) fInFile->Get( fullName.Data() ) ;
h2f->SetName( Form( "%s%f", name.Data(),gRandom->Uniform() ) );
fHisto2ds->push_back( (TH2*) h2f );
}
else if( type == "TH2D" ) {
h2d = (TH2D*) fInFile->Get( fullName.Data() ) ;
h2d->SetName( Form( "%s%f", name.Data(),gRandom->Uniform() ) );
fHisto2ds->push_back( (TH2*) h2d );
}
}
// According to the type of the 1d histogram,
// we retrieve it and append it to fHistos
void Search_for_TH1( TString type, TString name, short cycle ) {
TString fullName = Form( "%s;%d", name.Data(), cycle );
if( type == "TH1C" ) {
h1c = (TH1C*) fInFile->Get( fullName.Data() ) ;
h1c->SetName( Form( "%s%f", name.Data(),gRandom->Uniform() ) );
fHistos->push_back( (TH1*) h1c );
}
if( type == "TH1S" ) {
h1s = (TH1S*) fInFile->Get( fullName.Data() ) ;
h1s->SetName( Form( "%s%f", name.Data(),gRandom->Uniform() ) );
fHistos->push_back( (TH1*) h1s );
}
if( type == "TH1I" ) {
h1i = (TH1I*) fInFile->Get( fullName.Data() ) ;
h1i->SetName( Form( "%s%f", name.Data(),gRandom->Uniform() ) );
fHistos->push_back( (TH1*) h1i );
}
if( type == "TH1F" ) {
h1f = (TH1F*) fInFile->Get( fullName.Data() ) ;
h1f->SetName( Form( "%s%f", name.Data(),gRandom->Uniform() ) );
fHistos->push_back( (TH1*) h1f );
}
if( type == "TH1D" ) {
// or fInFile->GetObject( fullName.Data() , h1d );
h1d = (TH1D*) fInFile->Get( fullName.Data() ) ;
h1d->SetName( Form( "%s%f", name.Data(),gRandom->Uniform() ) );
fHistos->push_back( (TH1*) h1d );
}
}
~ReadIn_and_parse() { fInFile->Close(); }
};
//_____________________________________________________________________________
class Calculate_xlow2_xup2 {
private:
Float_t diff;
Float_t interval;
Int_t ratio;
Int_t binN2;
Float_t xup2;
Float_t xlow2;
public:
// constructor
Calculate_xlow2_xup2( Float_t xlow, Float_t xup,
Float_t xlow_select, Float_t xup_select,
Float_t binWidth ) {
//========================================== xup2 calculation
interval = xup_select-xlow;
ratio = interval/binWidth + 0.000005;
// we have truncation (int) = (float)
// about the precision testing
// suppose xup = 40.0 --> exact 200 bins.
// xup = 40.00001 -> 201 bins
// xup = 40.000001 -> 200 bins
diff = interval - ratio * binWidth ;
// note: due to the truncation, interval alway > (ratio*binWidth)
if ( abs(diff) < 0.00001 ) {
xup2 = xup_select;
binN2 = ratio;
}
else {
// we need one more bin.
binN2 = ratio + 1;
xup2 = binN2 * binWidth + xlow;
}
//========================================== xlow2 calculation
interval = xup2 - xlow_select;
ratio = interval/binWidth + 0.000005;
diff = interval - ratio*binWidth;
// note: due to the truncation, interval alway > (ratio*binWidth)
if ( diff < 0.00001 ) {
xlow2 = xlow_select;
binN2 = ratio;
}
else {
// we need one more bin.
binN2 = ratio + 1;
xlow2 = xup2 - binN2 * binWidth;
}
} //-------------- end of constructor
Float_t Get_xup2 () { return xup2; }
Float_t Get_xlow2 () { return xlow2; }
Int_t Get_binN2 () { return binN2; }
};
//_____________________________________________________________________________
class Calculate_common_x_range{
Bool_t fIsSameDim;
Float_t fUpperBound;
Float_t fLowerBound;
Float_t fXpick1;
Float_t fXpick2;
public:
Calculate_common_x_range( TH1* h1, TH1* h2, float xpick1, float xpick2 ) {
fXpick1 = xpick1;
fXpick2 = xpick2;
//check whether we have the same dimensions
Float_t h1xmin = h1->GetXaxis()->GetXmin();
Float_t h1xmax = h1->GetXaxis()->GetXmax();
Float_t h2xmin = h2->GetXaxis()->GetXmin();
Float_t h2xmax = h2->GetXaxis()->GetXmax();
Bool_t check1 = ( h1xmin == h2xmin );
Bool_t check2 = ( h1xmax == h2xmax );
fIsSameDim = ( check1&& check2 );
if( fIsSameDim == false ){
// find out the smallest one amont h1xmax, h2xmax, and xpick2
vector<Float_t> upper;
upper.push_back( h1xmax );
upper.push_back( h2xmax );
upper.push_back( xpick2 );
fUpperBound = 1E+38;
for( Int_t i =0; i< upper.size(); i++ ) {
if( upper[i] < fUpperBound ) { fUpperBound = upper[i]; }
}
// find out the largest one amont h1xmin, h2xmin, and xpick1
vector<Float_t> lower;
lower.push_back( h1xmin );
lower.push_back( h2xmin );
lower.push_back( xpick1 );
fLowerBound = -1E+38;
for( Int_t i =0; i< lower.size(); i++ ) {
if( lower[i] > fLowerBound ) { fLowerBound = lower[i]; }
}
}
}
Float_t Get_upper_bound ( ) {
if( fIsSameDim ){ return fXpick2; }
else { return fUpperBound;}
}
Float_t Get_lower_bound ( ) {
if( fIsSameDim ){ return fXpick1; }
else { return fLowerBound;}
}
Bool_t Is_same_dim () { return fIsSameDim; }
};
//_____________________________________________________________________________
class Calculate_fraction_bg{
private:
TH2* fHisto2d;
TH1D* fFullYProj;
TH1D* fGatedX_ProjY;
TH1D* fGatedX_ProjY_bg;
TH1D* fFullXProj;
TH1D* fGatedY_ProjX;
TH1D* fGatedY_ProjX_bg;
Bool_t toProjonY;
public:
TH1* get_histogram( ) {
/* according to gating on which axis, we retrun the corresponding histogram */
if( toProjonY ) { return (TH1*) fGatedX_ProjY_bg->Clone(); }
else { return (TH1*) fGatedY_ProjX_bg->Clone(); }
}
// constructor:
Calculate_fraction_bg ( TH2* histo2d,
Float_t g_range1,
Float_t g_range2,
Float_t bg_value,
Bool_t gating_on_x_proj_to_y ) {
/* NOTE:
we will create hitsogram here,
to retreive, use get_hitogram()
*/
fHisto2d = histo2d;
toProjonY = gating_on_x_proj_to_y;
if ( gating_on_x_proj_to_y == true ) {
/* this part corresponds to key 'p' :
gating on x then project to y
*/
fFullYProj = fHisto2d->ProjectionY() ;
float xMax = TMath::Max( g_range1, g_range2 );
float xMin = TMath::Min( g_range1, g_range2 );
Int_t binx1 = fHisto2d->GetXaxis()-> FindBin(xMin);
Int_t binx2 = fHisto2d->GetXaxis()-> FindBin(xMax);
fGatedX_ProjY = fHisto2d->ProjectionY( "gating_on_x_proj_to_y", binx1, binx2 );
fGatedX_ProjY_bg = (TH1D*) fGatedX_ProjY->Clone();
fGatedX_ProjY_bg -> Reset(); // to make counts all zero.
// doing the calculations
Float_t ratio;
Float_t C_x;
Float_t C_total ;
Int_t gate_range;
Int_t binN;
C_total = fFullYProj->GetEntries();
gate_range = binx2- binx1 + 1;
binN = fGatedX_ProjY->GetNbinsX();
for( Int_t i=0; i<= binN; i++ ) {
C_x = fFullYProj-> GetBinContent(i);
ratio = C_x / C_total;
Float_t yyy = fGatedX_ProjY-> GetBinContent(i); // gated, but no bg substraction.
yyy = yyy - ratio * bg_value * gate_range ; // doing bg substraction.
fGatedX_ProjY_bg->SetBinContent(i, yyy );
//printf( "y before = %5f , y after = %5f, bg = %5f\n", fGatedX_ProjY-> GetBinContent(i), yyy, ratio * bg_value * gate_range );
}
}
else {
/* this part corresponds to key 'P'
gating on y then project to x
*/
fFullXProj = fHisto2d->ProjectionX() ;
float yMax = TMath::Max( g_range1, g_range2 );
float yMin = TMath::Min( g_range1, g_range2 );
Int_t biny1 = fHisto2d->GetYaxis()-> FindBin(yMin);
Int_t biny2 = fHisto2d->GetYaxis()-> FindBin(yMax);
fGatedY_ProjX = fHisto2d->ProjectionX( "gating_on_y_proj_to_x", biny1, biny2 );
fGatedY_ProjX_bg = (TH1D*) fGatedY_ProjX->Clone();
fGatedY_ProjX_bg -> Reset(); // to make counts all zero.
Float_t ratio;
Float_t C_x;
Float_t C_total ;
Int_t gate_range;
Int_t binN;
C_total = fFullXProj->GetEntries();
gate_range = biny2- biny1 + 1;
binN = fGatedY_ProjX->GetNbinsX();
for( Int_t i=0; i<= binN; i++ ) {
C_x = fFullXProj-> GetBinContent(i);
ratio = C_x / C_total;
Float_t yyy = fGatedY_ProjX-> GetBinContent(i); // gated, but no bg substraction.
yyy = yyy - ratio * bg_value * gate_range ; // doing bg substraction.
fGatedY_ProjX_bg->SetBinContent(i, yyy );
}
}
} //====================================== end of constructor
};
//_____________________________________________________________________________
class Calculate_addSub_bg{
private:
TH2* fHisto2d;
TH1D* fAdd;
TH1D* fSub;
TH1D* fGatedX_ProjY_bg;
TH1D* fGatedY_ProjX_bg;
Float_t* fAddGate_1;
Float_t* fAddGate_2;
Float_t* fSubGate_1;
Float_t* fSubGate_2;
Bool_t toProjonY;
public:
TH1* get_histogram( ) {
/* according to gating on which axis, we retrun the corresponding histogram */
if( toProjonY ) { return (TH1*) fGatedX_ProjY_bg->Clone(); }
else { return (TH1*) fGatedY_ProjX_bg->Clone(); }
}
// constructor:
Calculate_addSub_bg ( TH2* histo2d,
Float_t add_range1,
Float_t add_range2,
Float_t sub_range1,
Float_t sub_range2,
Bool_t gating_on_x_proj_to_y ) {
/* NOTE:
we will create hitsogram here,
to retreive, use get_hitogram()
*/
fHisto2d = histo2d;
toProjonY = gating_on_x_proj_to_y;
if ( gating_on_x_proj_to_y == true ) {
/* this part corresponds to key 'p' :
gating on x then project to y
*/
Float_t xMax;
Float_t xMin;
Int_t binx1;
Int_t binx2;
Int_t rAdd, rSub;
Float_t wSub;
xMax = TMath::Max( add_range1, add_range2 );
xMin = TMath::Min( add_range1, add_range2 );
binx1 = fHisto2d->GetXaxis()-> FindBin(xMin);
binx2 = fHisto2d->GetXaxis()-> FindBin(xMax);
rAdd = binx2 - binx1 + 1;
fAdd = fHisto2d->ProjectionY( "AddgX_projtoY", binx1, binx2 );
xMax = TMath::Max( sub_range1, sub_range2 );
xMin = TMath::Min( sub_range1, sub_range2 );
binx1 = fHisto2d->GetXaxis()-> FindBin(xMin);
binx2 = fHisto2d->GetXaxis()-> FindBin(xMax);
rSub = binx2 - binx1 + 1;
fSub = fHisto2d->ProjectionY( "SubgX_projtoY", binx1, binx2 );
wSub = static_cast<float>( rAdd )/ ( rSub );
fGatedX_ProjY_bg = (TH1D*) fAdd->Clone();
fGatedX_ProjY_bg->Reset();
fGatedX_ProjY_bg->Add( fAdd, fSub, 1, -wSub );
}
else {
/* this part corresponds to key 'P'
gating on y then project to x
*/
Float_t yMax;
Float_t yMin;
Int_t biny1;
Int_t biny2;
Int_t rAdd, rSub;
Float_t wSub;
yMax = TMath::Max( add_range1, add_range2 );
yMin = TMath::Min( add_range1, add_range2 );
biny1 = fHisto2d->GetYaxis()-> FindBin(yMin);
biny2 = fHisto2d->GetYaxis()-> FindBin(yMax);
rAdd = biny2 - biny1 + 1;
fAdd = fHisto2d->ProjectionX( "AddgY_projtoX", biny1, biny2 );
yMax = TMath::Max( sub_range1, sub_range2 );
yMin = TMath::Min( sub_range1, sub_range2 );
biny1 = fHisto2d->GetYaxis()-> FindBin(yMin);
biny2 = fHisto2d->GetYaxis()-> FindBin(yMax);
rSub = biny2 - biny1 + 1;
fSub = fHisto2d->ProjectionX( "SubgY_projtoX", biny1, biny2 );
wSub = static_cast<float>( rAdd )/ ( rSub );
fGatedY_ProjX_bg = (TH1D*) fAdd->Clone();
fGatedY_ProjX_bg->Reset();
fGatedY_ProjX_bg->Add( fAdd, fSub, 1, -wSub );
}
} //====================================== end of constructor
};
//_____________________________________________________________________________
// function for fitting.
double gauss_bg( Double_t *t, Double_t *par)
{
Double_t x = t[0];
Double_t const_1 = par[0];
Double_t linear_1 = par[1];
Double_t height = par[2];
Double_t center = par[3];
Double_t sigma = par[4];
Double_t y;
y = const_1 + linear_1 * x +
height * exp ( -0.5 * pow( (x-center)/sigma, 2 ) );
return y;
}
double double_gauss_bg( Double_t *t, Double_t *par)
{
Double_t x = t[0];
Double_t const_1 = par[0];
Double_t linear_1 = par[1];
Double_t height1 = par[2];
Double_t center1 = par[3];
Double_t sigma1 = par[4];
Double_t height2 = par[5];
Double_t center2 = par[6];
Double_t sigma2 = par[7];
// 8 parameters
Double_t y;
y = const_1 + linear_1 * x +
height1 * exp ( -0.5 * pow( (x-center1)/sigma1, 2 ) ) +
height2 * exp ( -0.5 * pow( (x-center2)/sigma2, 2 ) )
;
return y;
}
const int NPEAKS = 10; // hard limit used in N-Gaussian fitting.
int GL_peakn = 3 ; // tai: this can be modified by user
double n_gauss_bg(Double_t *x, Double_t *par) {
Double_t result = par[0] + par[1]*x[0];
for (Int_t p=0;p< GL_peakn; p++) {
Double_t norm = par[3*p+2];
Double_t mean = par[3*p+3];
Double_t sigma = par[3*p+4];
result += norm*TMath::Gaus(x[0],mean,sigma);
}
return result;
}