hpstr
The Heavy Photon Search Toolkit for Reconstruction (hpstr) provides an interface to physics data from the HPS experiment saved in the LCIO format and converts it into an ROOT based format.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
BhToysHistoProcessor.cxx
Go to the documentation of this file.
1
8
9BhToysHistoProcessor::BhToysHistoProcessor(const std::string& name, Process& process)
10 : Processor(name, process) {
11 }
12
14
16 std::cout << "Configuring BhToysHistoProcessor" << std::endl;
17 try {
18 debug_ = parameters.getInteger("debug");
19 massSpectrum_ = parameters.getString("massSpectrum");
20 mass_hypo_ = parameters.getDouble("mass_hypo");
21 win_factor_ = parameters.getInteger("win_factor");
22 poly_order_ = parameters.getInteger("poly_order");
23 toy_poly_order_ = parameters.getInteger("toy_poly_order");
24 seed_ = parameters.getInteger("seed");
25 nToys_ = parameters.getInteger("nToys");
26 toy_sig_samples_ = parameters.getInteger("toy_sig_samples");
27 bkg_mult_ = parameters.getInteger("toy_bkg_mult");
28 res_scale_ = parameters.getDouble("res_scale");
29 signal_shape_h_name_ = parameters.getString("signal_shape_h_name", "");
30 signal_shape_h_file_ = parameters.getString("signal_shape_h_file", "");
31 bkg_model_ = parameters.getInteger("bkg_model");
32 //asymptotic_limit_ = parameters.getBoolean("asymptoticLimit");
33 } catch(std::runtime_error& error) {
34 std::cout << error.what() << std::endl;
35 }
36}
37
38void BhToysHistoProcessor::initialize(std::string inFilename, std::string outFilename) {
39 // Init Files
40 inF_ = new TFile(inFilename.c_str());
41
42 // Get mass spectrum from file
43 mass_spec_h = (TH1*) inF_->Get(massSpectrum_.c_str());
44
45 // Initialize the signal histogram, if a file name and histogram name are provided.
46 std::cout << "Signal Shape File :: " << signal_shape_h_file_ << std::endl;
47 std::cout << "Signal Shape Hist :: " << signal_shape_h_name_ << std::endl;
49 TFile *file = new TFile(signal_shape_h_file_.c_str());
50 signal_shape_h_ = (TH1*) file->Get(signal_shape_h_name_.c_str());
51 } else if(signal_shape_h_file_ != "" && signal_shape_h_name_ == "") {
52 std::cout << "[BumpHunter] :: !! WARNING !! Signal injection file, but no histogram, specified! Defaulting to Gaussian.";
53 } else if(signal_shape_h_file_ == "" && signal_shape_h_name_ != "") {
54 std::cout << "[BumpHunter] :: !! WARNING !! Signal injection histogram, but no file, specified! Defaulting to Gaussian.";
55 }
56
57 // Get the appropriate background model.
58 FitFunction::BkgModel bkg_fit_model;
59 std::cout << "Background Model ID: " << bkg_model_ << std::endl;
60 switch(bkg_model_) {
61 case 0: bkg_fit_model = FitFunction::BkgModel::EXP_CHEBYSHEV;
62 break;
63 case 1: bkg_fit_model = FitFunction::BkgModel::EXP_CHEBYSHEV;
64 break;
65 case 2: bkg_fit_model = FitFunction::BkgModel::LEGENDRE;
66 break;
67 case 3: bkg_fit_model = FitFunction::BkgModel::EXP_LEGENDRE;
68 break;
69 default: bkg_fit_model = FitFunction::BkgModel::EXP_CHEBYSHEV;
70 }
71
72 // If the toy fit order is -1, it is undefined.
74
75 // Init bump hunter manager
77 bump_hunter_->setBounds(mass_spec_h->GetXaxis()->GetBinUpEdge(mass_spec_h->FindFirstBinAbove()),
78 mass_spec_h->GetXaxis()->GetBinLowEdge(mass_spec_h->FindLastBinAbove()));
80
81 // Init FlatTupleMaker
82 flat_tuple_ = new FlatTupleMaker(outFilename.c_str(), "fit_toys");
83
84 // Setup flat tuple branches
85 flat_tuple_->addVariable("bkg_total");
86 flat_tuple_->addVariable("corr_mass");
87 flat_tuple_->addVariable("mass_hypo");
88 flat_tuple_->addVariable("poly_order");
89 flat_tuple_->addVariable("win_factor");
90 flat_tuple_->addVariable("window_size");
91 flat_tuple_->addVariable("resolution_scale");
92 flat_tuple_->addVariable("bkg_model");
93
94 flat_tuple_->addVariable("bkg_chi2_prob");
95 flat_tuple_->addVariable("bkgsig_chi2_prob");
96 flat_tuple_->addVariable("toyfit_chi2_prob");
97 flat_tuple_->addVariable("bkg_edm");
98 flat_tuple_->addVariable("bkg_minuit_status");
99 flat_tuple_->addVariable("bkg_nll");
100
101 flat_tuple_->addVariable("edm");
102 flat_tuple_->addVariable("minuit_status");
103 flat_tuple_->addVariable("nll");
104 flat_tuple_->addVariable("p_value");
106 flat_tuple_->addVariable("bkg_rate_mass_hypo");
107 flat_tuple_->addVariable("bkg_rate_mass_hypo_err");
108 flat_tuple_->addVariable("sig_yield");
109 flat_tuple_->addVariable("sig_yield_err");
110 flat_tuple_->addVariable("upper_limit");
111 flat_tuple_->addVariable("ul_p_value");
112 flat_tuple_->addVariable("ul_minuit_status");
113 flat_tuple_->addVector("ul_nlls");
114 flat_tuple_->addVector("ul_sig_yields");
115
116 flat_tuple_->addVariable("seed");
117 flat_tuple_->addVariable("toy_sig_samples");
118 flat_tuple_->addVariable("toy_bkg_mult");
119 flat_tuple_->addVector("toy_model_index");
120 flat_tuple_->addVector("toy_bkg_chi2_prob");
121 flat_tuple_->addVector("toy_bkg_edm");
122 flat_tuple_->addVector("toy_bkg_minuit_status");
123 flat_tuple_->addVector("toy_bkg_nll");
124 flat_tuple_->addVector("toy_minuit_status");
125 flat_tuple_->addVector("toy_nll");
126 flat_tuple_->addVector("toy_p_value");
127 flat_tuple_->addVector("toy_q0");
128 flat_tuple_->addVector("toy_bkg_rate_mass_hypo");
129 flat_tuple_->addVector("toy_bkg_rate_mass_hypo_err");
130 flat_tuple_->addVector("toy_sig_yield");
131 flat_tuple_->addVector("toy_sig_yield_err");
132 flat_tuple_->addVector("toy_upper_limit");
133
134}
135
137 std::cout << "Running on mass spectrum: " << massSpectrum_ << std::endl;
138 std::cout << "Running with polynomial order: " << poly_order_ << std::endl;
139 std::cout << "Running with window factor: " << win_factor_ << std::endl;
140 std::cout << "Running on mass hypo: " << mass_hypo_ << std::endl;
141
142 // Search for a resonance at the given mass hypothesis
144 mass_spec_h->Write();
145
146 // Get the result of the background fit
147 TFitResultPtr bkg_result = result->getBkgFitResult();
148
149 std::cout << "Filling Fit Results " << std::endl;
150 // Get the result of the signal+background fit
151 TFitResultPtr sig_result = result->getCompFitResult();
152
153 // Set the Fit Parameters in the flat tuple
154 flat_tuple_->setVariableValue("bkg_total", result->getIntegral());
155 flat_tuple_->setVariableValue("corr_mass", result->getCorrectedMass());
156 flat_tuple_->setVariableValue("mass_hypo", result->getMass());
159 flat_tuple_->setVariableValue("window_size", result->getWindowSize());
160 flat_tuple_->setVariableValue("resolution_scale", res_scale_);
162
163 // Set the Fit Results in the flat tuple
164 flat_tuple_->setVariableValue("bkg_chi2_prob", bkg_result->Prob());
165 flat_tuple_->setVariableValue("bkgsig_chi2_prob", sig_result->Prob());
166 flat_tuple_->setVariableValue("toyfit_chi2_prob", result->getBkgToysFitResult()->Prob());
167 flat_tuple_->setVariableValue("bkg_edm", bkg_result->Edm());
168 flat_tuple_->setVariableValue("bkg_minuit_status", bkg_result->Status());
169 flat_tuple_->setVariableValue("bkg_nll", bkg_result->MinFcnValue());
170
171 flat_tuple_->setVariableValue("edm", sig_result->Edm());
172 flat_tuple_->setVariableValue("minuit_status", sig_result->Status());
173 flat_tuple_->setVariableValue("nll", sig_result->MinFcnValue());
174 flat_tuple_->setVariableValue("p_value", result->getPValue());
175 flat_tuple_->setVariableValue("q0", result->getQ0());
176 flat_tuple_->setVariableValue("bkg_rate_mass_hypo", result->getFullBkgRate());
177 flat_tuple_->setVariableValue("bkg_rate_mass_hypo_err", result->getFullBkgRateError());
178 flat_tuple_->setVariableValue("sig_yield", result->getSignalYield());
179 flat_tuple_->setVariableValue("sig_yield_err", result->getSignalYieldErr());
180 flat_tuple_->setVariableValue("upper_limit", result->getUpperLimit());
181 flat_tuple_->setVariableValue("upper_limit_p_value", result->getUpperLimitPValue());
182
183 for(auto& likelihood : result->getLikelihoods()) {
184 flat_tuple_->addToVector("nlls", likelihood);
185 }
186
187 for(auto& yield : result->getSignalYields()) {
188 flat_tuple_->addToVector("sig_yields", yield);
189 }
190
191 std::vector<HpsFitResult*> toy_results;
193 flat_tuple_->setVariableValue("toy_bkg_mult", bkg_mult_);
194 flat_tuple_->setVariableValue("toy_sig_samples", toy_sig_samples_);
195
196 if(nToys_ > 0) {
197 std::cout << "Generating " << nToys_ << " Toys" << std::endl;
198 std::cout << " Signal Injection :: " << toy_sig_samples_ << std::endl;
199 if(signal_shape_h_ != NULL) {
200 std::cout << " Signal Shape :: " << signal_shape_h_name_.c_str() << std::endl;
201 } else {
202 std::cout << " Signal Shape :: Gaussian" << std::endl;
203 }
204 std::cout << " Background Multiplier :: " << bkg_mult_ << std::endl;
206
207 int toyFitN = 0;
208 for(TH1* hist : toys_hist) {
209 std::cout << "Fitting Toy " << toyFitN << std::endl;
210 toy_results.push_back(bump_hunter_->performSearch(hist, mass_hypo_, false, false));
211 toyFitN++;
212 }
213 }
214
215 int toyModelIndex = 0;
216 for(auto& toy_result : toy_results) {
217 // Get the result of the background fit
218 TFitResultPtr toy_bkg_result = toy_result->getBkgFitResult();
219
220 flat_tuple_->addToVector("toy_bkg_chi2_prob", toy_bkg_result->Prob());
221 flat_tuple_->addToVector("toy_bkg_edm", toy_bkg_result->Edm());
222 flat_tuple_->addToVector("toy_bkg_minuit_status", toy_bkg_result->Status());
223 flat_tuple_->addToVector("toy_bkg_nll", toy_bkg_result->MinFcnValue());
224
225 // Get the result of the signal+background fit
226 TFitResultPtr toy_sig_result = toy_result->getCompFitResult();
227
228 // Retrieve all of the result of interest.
229 flat_tuple_->addToVector("toy_minuit_status", toy_sig_result->Status());
230 flat_tuple_->addToVector("toy_nll", toy_sig_result->MinFcnValue());
231 flat_tuple_->addToVector("toy_p_value", toy_result->getPValue());
232 flat_tuple_->addToVector("toy_q0", toy_result->getQ0());
233 flat_tuple_->addToVector("toy_bkg_rate_mass_hypo", toy_result->getFullBkgRate());
234 flat_tuple_->addToVector("toy_bkg_rate_mass_hypo_err", toy_result->getFullBkgRateError());
235 flat_tuple_->addToVector("toy_sig_yield", toy_result->getSignalYield());
236 flat_tuple_->addToVector("toy_sig_yield_err", toy_result->getSignalYieldErr());
237 flat_tuple_->addToVector("toy_upper_limit", toy_result->getUpperLimit());
238 flat_tuple_->addToVector("toy_model_index", toyModelIndex);
239 toyModelIndex++;
240 }
241
242 // Fill and write the flat tuple
243 flat_tuple_->fill();
245
246 return true;
247}
248
250 inF_->Close();
251 delete inF_;
252 //delete flat_tuple_;
253 delete bump_hunter_;
254}
255
#define DECLARE_PROCESSOR(CLASS)
Macro which allows the framework to construct a producer given its name during configuration.
Definition Processor.h:139
Insert description here. more details.
virtual void configure(const ParameterSet &parameters)
Configure using given parameters.
int bkg_model_
What background model type to use.
int toy_poly_order_
Order of polynomial used to create the toy generator function.
virtual void finalize()
description
TH1 * signal_shape_h_
The signal shape histogram to use.
double mass_hypo_
The signal hypothesis to use in the fit.
int poly_order_
Order of polynomial used to model the background.
BumpHunter * bump_hunter_
The bump hunter manager.
int nToys_
Number of toys to throw and fit.
double res_scale_
The factor by which to scale the mass resolution function.
virtual void initialize(std::string inFilename, std::string outFilename)
description
BhToysHistoProcessor(const std::string &name, Process &process)
Constructor.
FlatTupleMaker * flat_tuple_
The flat tuple manager.
TFile * inF_
description
std::string signal_shape_h_file_
The signal shpae histogram file path, if defined.
std::string signal_shape_h_name_
The signal shape histogram name to use, if defined.
TH1 * mass_spec_h
The mass spectrum to fit.
bool asymptotic_limit_
Whether to use the asymptotic upper limit or the power constrained. Defaults to asymptotic.
virtual bool process()
description
std::string massSpectrum_
The name of the mass spectrum to fit.
description
Definition BumpHunter.h:49
void enableDebug(bool debug=true)
Enable/disable debug.
Definition BumpHunter.h:117
void setBounds(double low_bound, double high_bound)
Set the histogram bounds.
std::vector< TH1 * > generateToys(TH1 *histogram, double n_toys, int seed, int toy_sig_samples, int bkg_mult=1, TH1 *signal_hist=nullptr)
description
HpsFitResult * performSearch(TH1 *histogram, double mass_hypothesis, bool skip_bkg_fit, bool skip_ul)
Perform a search for a resonance at the given mass hypothesis.
BkgModel
description
Definition FitFunction.h:39
description
void addVector(std::string vector_name)
description
void addVariable(std::string variable_name)
description
void close()
description
void addToVector(std::string variable_name, double value)
description
void setVariableValue(std::string variable_name, double value)
description
void fill()
description
description
double getIntegral()
Get the integral within the fit window.
TFitResultPtr getBkgFitResult()
Get background fit result.
TFitResultPtr getCompFitResult()
Get the complete fit result.
float getSignalYieldErr()
Get the error of the signal yield.
double getUpperLimit()
Get the upper fit limit.
double getUpperLimitPValue()
Get the p-value at the upper limit.
double getWindowSize()
Get the size of the fit window.
double getCorrectedMass() const
Get the corrected mass.
double getPValue()
description
std::vector< double > getLikelihoods()
Get the likelihoods.
double getFullBkgRate()
Get the background rate obtained from the signal+background hit at the mass hypo.
float getSignalYield()
Get the signal yield obtained from the signal+background fit.
double getQ0()
description
double getFullBkgRateError()
Get the background rate error from the signal+background fit at the mass hypo.
TFitResultPtr getBkgToysFitResult()
Get background toy model fit result.
std::vector< double > getSignalYields()
Get the signal yields.
double getMass() const
Get the mass hypothesis for the fit.
description
Base class for all event processing components.
Definition Processor.h:34