CMS 3D CMS Logo

FitWithRooFit.h
Go to the documentation of this file.
1 #ifndef Alignment_OfflineValidation_FitWithRooFit_h
2 #define Alignment_OfflineValidation_FitWithRooFit_h
3 
4 #include "RooGlobalFunc.h"
5 #include "TCanvas.h"
6 #include "TTree.h"
7 #include "TH1D.h"
8 #include "TRandom.h"
9 #include "RooRealVar.h"
10 #include "RooDataSet.h"
11 #include "RooGaussian.h"
12 #include "RooVoigtian.h"
13 #include "RooExponential.h"
14 #include "RooPlot.h"
15 #include "RooDataHist.h"
16 #include "RooAddPdf.h"
17 #include "RooChebychev.h"
18 #include "RooFitResult.h"
19 #include "RooGenericPdf.h"
20 #include "RooGaussModel.h"
21 #include "RooAddModel.h"
22 #include "RooPolynomial.h"
23 #include "RooCBShape.h"
24 #include "RooMinimizer.h"
25 #include "RooBreitWigner.h"
26 #include "RooFFTConvPdf.h"
27 
47 public:
48  std::unique_ptr<RooDataHist> importTH1(TH1* histo, double xMin, double xMax);
49 
50  void fit(TH1* histo,
51  const TString signalType,
52  const TString backgroundType,
53  double xMin = 0.,
54  double xMax = 0.,
55  bool sumW2Error = false);
56 
57  void initMean(double value, double min, double max, const TString& name = "mean", const TString& title = "mean");
58  void initMean2(double value, double min, double max, const TString& name = "mean2", const TString& title = "mean2");
59  void initMean3(double value, double min, double max, const TString& name = "mean3", const TString& title = "mean3");
60  void initSigma(double value, double min, double max, const TString& name = "sigma", const TString& title = "sigma");
61  void initSigma2(double value, double min, double max, const TString& name = "sigma2", const TString& title = "sigma2");
62  void initSigma3(double value, double min, double max, const TString& name = "sigma3", const TString& title = "sigma3");
63  void initGamma(double value, double min, double max, const TString& name = "gamma", const TString& title = "gamma");
64  void initGaussFrac(
65  double value, double min, double max, const TString& name = "GaussFrac", const TString& title = "GaussFrac");
66  void initGaussFrac2(
67  double value, double min, double max, const TString& name = "GaussFrac2", const TString& title = "GaussFrac2");
68  void initExpCoeffA0(
69  double value, double min, double max, const TString& name = "expCoeffa0", const TString& title = "expCoeffa0");
70  void initExpCoeffA1(
71  double value, double min, double max, const TString& name = "expCoeffa1", const TString& title = "expCoeffa1");
72  void initExpCoeffA2(
73  double value, double min, double max, const TString& name = "expCoeffa2", const TString& title = "expCoeffa2");
74  void initFsig(
75  double value, double min, double max, const TString& name = "fsig", const TString& title = "signal fraction");
76  void initA0(double value, double min, double max, const TString& name = "a0", const TString& title = "a0");
77  void initA1(double value, double min, double max, const TString& name = "a1", const TString& title = "a1");
78  void initA2(double value, double min, double max, const TString& name = "a2", const TString& title = "a2");
79  void initA3(double value, double min, double max, const TString& name = "a3", const TString& title = "a3");
80  void initA4(double value, double min, double max, const TString& name = "a4", const TString& title = "a4");
81  void initA5(double value, double min, double max, const TString& name = "a5", const TString& title = "a5");
82  void initA6(double value, double min, double max, const TString& name = "a6", const TString& title = "a6");
83  void initAlpha(double value, double min, double max, const TString& name = "alpha", const TString& title = "alpha");
84  void initN(double value, double min, double max, const TString& name = "n", const TString& title = "n");
85  void initFGCB(double value, double min, double max, const TString& name = "fGCB", const TString& title = "fGCB");
86 
87  inline RooRealVar* mean() { return mean_.get(); }
88  inline RooRealVar* mean2() { return mean2_.get(); }
89  inline RooRealVar* mean3() { return mean3_.get(); }
90  inline RooRealVar* sigma() { return sigma_.get(); }
91  inline RooRealVar* sigma2() { return sigma2_.get(); }
92  inline RooRealVar* sigma3() { return sigma3_.get(); }
93  inline RooRealVar* gamma() { return gamma_.get(); }
94  inline RooRealVar* gaussFrac() { return gaussFrac_.get(); }
95  inline RooRealVar* gaussFrac2() { return gaussFrac2_.get(); }
96  inline RooRealVar* expCoeffa0() { return expCoeffa0_.get(); }
97  inline RooRealVar* expCoeffa1() { return expCoeffa1_.get(); }
98  inline RooRealVar* expCoeffa2() { return expCoeffa2_.get(); }
99  inline RooRealVar* fsig() { return fsig_.get(); }
100  inline RooRealVar* a0() { return a0_.get(); }
101  inline RooRealVar* a1() { return a1_.get(); }
102  inline RooRealVar* a2() { return a2_.get(); }
103  inline RooRealVar* a3() { return a3_.get(); }
104  inline RooRealVar* a4() { return a4_.get(); }
105  inline RooRealVar* a5() { return a5_.get(); }
106  inline RooRealVar* a6() { return a6_.get(); }
107  inline RooRealVar* alpha() { return alpha_.get(); }
108  inline RooRealVar* n() { return n_.get(); }
109  inline RooRealVar* fGCB() { return fGCB_.get(); }
110 
111  void reinitializeParameters();
112 
113  std::unique_ptr<RooAbsPdf> buildSignalModel(RooRealVar* x, const TString& signalType);
114  std::unique_ptr<RooAbsPdf> buildBackgroundModel(RooRealVar* x, const TString& backgroundType);
115  std::unique_ptr<RooAbsPdf> buildModel(RooRealVar* x, const TString& signalType, const TString& backgroundType);
116 
117  bool useChi2_ = false;
118 
119 protected:
120  // Declare all variables
121  std::unique_ptr<RooRealVar> mean_;
122  std::unique_ptr<RooRealVar> mean2_;
123  std::unique_ptr<RooRealVar> mean3_;
124  std::unique_ptr<RooRealVar> sigma_;
125  std::unique_ptr<RooRealVar> sigma2_;
126  std::unique_ptr<RooRealVar> sigma3_;
127  std::unique_ptr<RooRealVar> gamma_;
128  std::unique_ptr<RooRealVar> gaussFrac_;
129  std::unique_ptr<RooRealVar> gaussFrac2_;
130  std::unique_ptr<RooRealVar> expCoeffa0_;
131  std::unique_ptr<RooRealVar> expCoeffa1_;
132  std::unique_ptr<RooRealVar> expCoeffa2_;
133  std::unique_ptr<RooRealVar> fsig_;
134  std::unique_ptr<RooRealVar> a0_;
135  std::unique_ptr<RooRealVar> a1_;
136  std::unique_ptr<RooRealVar> a2_;
137  std::unique_ptr<RooRealVar> a3_;
138  std::unique_ptr<RooRealVar> a4_;
139  std::unique_ptr<RooRealVar> a5_;
140  std::unique_ptr<RooRealVar> a6_;
141  std::unique_ptr<RooRealVar> alpha_;
142  std::unique_ptr<RooRealVar> n_;
143  std::unique_ptr<RooRealVar> fGCB_;
144 
145  // Initial values
146  double initVal_mean = 0.0;
147  double initVal_mean2 = 0.0;
148  double initVal_mean3 = 0.0;
149  double initVal_sigma = 0.0;
150  double initVal_sigma2 = 0.0;
151  double initVal_sigma3 = 0.0;
152  double initVal_gamma = 0.0;
153  double initVal_gaussFrac = 0.0;
154  double initVal_gaussFrac2 = 0.0;
155  double initVal_expCoeffa0 = 0.0;
156  double initVal_expCoeffa1 = 0.0;
157  double initVal_expCoeffa2 = 0.0;
158  double initVal_fsig = 0.0;
159  double initVal_a0 = 0.0;
160  double initVal_a1 = 0.0;
161  double initVal_a2 = 0.0;
162  double initVal_a3 = 0.0;
163  double initVal_a4 = 0.0;
164  double initVal_a5 = 0.0;
165  double initVal_a6 = 0.0;
166  double initVal_alpha = 0.0;
167  double initVal_n = 0.0;
168  double initVal_fGCB = 0.0;
169 };
170 
171 #endif
void initA0(double value, double min, double max, const TString &name="a0", const TString &title="a0")
RooRealVar * mean3()
Definition: FitWithRooFit.h:89
double initVal_expCoeffa0
void initGamma(double value, double min, double max, const TString &name="gamma", const TString &title="gamma")
std::unique_ptr< RooRealVar > fsig_
RooRealVar * sigma()
Definition: FitWithRooFit.h:90
void initA5(double value, double min, double max, const TString &name="a5", const TString &title="a5")
void initSigma(double value, double min, double max, const TString &name="sigma", const TString &title="sigma")
std::unique_ptr< RooRealVar > sigma3_
void initSigma2(double value, double min, double max, const TString &name="sigma2", const TString &title="sigma2")
std::unique_ptr< RooRealVar > mean_
std::unique_ptr< RooRealVar > mean2_
void initMean3(double value, double min, double max, const TString &name="mean3", const TString &title="mean3")
void initGaussFrac2(double value, double min, double max, const TString &name="GaussFrac2", const TString &title="GaussFrac2")
void initA1(double value, double min, double max, const TString &name="a1", const TString &title="a1")
double initVal_mean2
void initFGCB(double value, double min, double max, const TString &name="fGCB", const TString &title="fGCB")
void initA2(double value, double min, double max, const TString &name="a2", const TString &title="a2")
RooRealVar * expCoeffa0()
Definition: FitWithRooFit.h:96
void initA6(double value, double min, double max, const TString &name="a6", const TString &title="a6")
RooRealVar * a6()
std::unique_ptr< RooRealVar > a0_
void initMean2(double value, double min, double max, const TString &name="mean2", const TString &title="mean2")
std::unique_ptr< RooRealVar > a1_
RooRealVar * a3()
std::unique_ptr< RooRealVar > mean3_
std::unique_ptr< RooRealVar > a5_
RooRealVar * fsig()
Definition: FitWithRooFit.h:99
std::unique_ptr< RooRealVar > n_
std::unique_ptr< RooAbsPdf > buildSignalModel(RooRealVar *x, const TString &signalType)
Build the model for the specified signal type.
double initVal_expCoeffa1
double initVal_mean
double initVal_gamma
double initVal_alpha
double initVal_gaussFrac2
void reinitializeParameters()
std::unique_ptr< RooRealVar > a6_
double initVal_sigma3
RooRealVar * gaussFrac()
Definition: FitWithRooFit.h:94
void initN(double value, double min, double max, const TString &name="n", const TString &title="n")
RooRealVar * sigma3()
Definition: FitWithRooFit.h:92
RooRealVar * sigma2()
Definition: FitWithRooFit.h:91
std::unique_ptr< RooRealVar > sigma_
RooRealVar * mean()
Definition: FitWithRooFit.h:87
RooRealVar * gaussFrac2()
Definition: FitWithRooFit.h:95
std::unique_ptr< RooAbsPdf > buildBackgroundModel(RooRealVar *x, const TString &backgroundType)
Build the model for the specified background type.
RooRealVar * a0()
std::unique_ptr< RooRealVar > a4_
void initFsig(double value, double min, double max, const TString &name="fsig", const TString &title="signal fraction")
RooRealVar * n()
double initVal_sigma2
RooRealVar * mean2()
Definition: FitWithRooFit.h:88
Definition: value.py:1
RooRealVar * expCoeffa1()
Definition: FitWithRooFit.h:97
void initExpCoeffA1(double value, double min, double max, const TString &name="expCoeffa1", const TString &title="expCoeffa1")
std::unique_ptr< RooRealVar > gaussFrac2_
RooRealVar * gamma()
Definition: FitWithRooFit.h:93
std::unique_ptr< RooAbsPdf > buildModel(RooRealVar *x, const TString &signalType, const TString &backgroundType)
Build the model to fit.
double initVal_expCoeffa2
std::unique_ptr< RooRealVar > a2_
std::unique_ptr< RooRealVar > sigma2_
void initAlpha(double value, double min, double max, const TString &name="alpha", const TString &title="alpha")
RooRealVar * fGCB()
void fit(TH1 *histo, const TString signalType, const TString backgroundType, double xMin=0., double xMax=0., bool sumW2Error=false)
std::unique_ptr< RooDataHist > importTH1(TH1 *histo, double xMin, double xMax)
Definition: FitWithRooFit.cc:6
std::unique_ptr< RooRealVar > gaussFrac_
std::unique_ptr< RooRealVar > gamma_
RooRealVar * expCoeffa2()
Definition: FitWithRooFit.h:98
std::unique_ptr< RooRealVar > fGCB_
std::unique_ptr< RooRealVar > a3_
void initGaussFrac(double value, double min, double max, const TString &name="GaussFrac", const TString &title="GaussFrac")
std::unique_ptr< RooRealVar > expCoeffa0_
std::unique_ptr< RooRealVar > expCoeffa1_
double initVal_fGCB
void initExpCoeffA2(double value, double min, double max, const TString &name="expCoeffa2", const TString &title="expCoeffa2")
RooRealVar * a4()
double initVal_fsig
RooRealVar * alpha()
void initSigma3(double value, double min, double max, const TString &name="sigma3", const TString &title="sigma3")
RooRealVar * a1()
void initA3(double value, double min, double max, const TString &name="a3", const TString &title="a3")
RooRealVar * a2()
void initExpCoeffA0(double value, double min, double max, const TString &name="expCoeffa0", const TString &title="expCoeffa0")
double initVal_gaussFrac
std::unique_ptr< RooRealVar > expCoeffa2_
RooRealVar * a5()
std::unique_ptr< RooRealVar > alpha_
double initVal_mean3
void initMean(double value, double min, double max, const TString &name="mean", const TString &title="mean")
double initVal_sigma
void initA4(double value, double min, double max, const TString &name="a4", const TString &title="a4")