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.
Loading...
Searching...
No Matches
OptimizationProcessor.cxx
Go to the documentation of this file.
1
6
8
9#include <cstdlib>
10#include <fstream>
11#include <iostream>
12#include <string>
13
15
17
19 try {
20 // Basic config
21 debug_ = parameters.getInteger("debug", debug_);
22 year_ = parameters.getInteger("year", year_);
23 cuts_cfgFile_ = parameters.getString("cuts_cfgFile", cuts_cfgFile_);
24 outFileName_ = parameters.getString("outFileName", outFileName_);
25 cutVariables_ = parameters.getVString("cutVariables", cutVariables_);
26
27 // Background
28 bkgVtxAnaFilename_ = parameters.getString("bkgVtxAnaFilename", bkgVtxAnaFilename_);
29 bkgVtxAnaTreename_ = parameters.getString("bkgVtxAnaTreename", bkgVtxAnaTreename_);
30 background_sf_ = parameters.getDouble("background_sf", background_sf_);
31
32 // MC Signal
33 variableHistCfgFilename_ = parameters.getString("variableHistCfgFilename", variableHistCfgFilename_);
34 signalVtxAnaFilename_ = parameters.getString("signalVtxAnaFilename", signalVtxAnaFilename_);
35 signalVtxAnaTreename_ = parameters.getString("signalVtxAnaTreename", signalVtxAnaTreename_);
36 signalMCAnaFilename_ = parameters.getString("signalMCAnaFilename", signalMCAnaFilename_);
37 signal_pdgid_ = parameters.getString("signal_pdgid", signal_pdgid_);
38 signal_sf_ = parameters.getDouble("signal_sf", signal_sf_);
39 signal_mass_ = parameters.getDouble("signal_mass", signal_mass_);
40 mass_window_nsigma_ = parameters.getDouble("mass_window_nsigma", mass_window_nsigma_);
41
42 // New Variables //
43 new_variables_ = parameters.getVString("add_new_variables", new_variables_);
44 new_variable_params_ = parameters.getVDouble("new_variable_params", new_variable_params_);
45
46 max_iteration_ = parameters.getInteger("max_iteration", max_iteration_);
47 step_size_ = parameters.getDouble("step_size", step_size_);
48 } catch (std::runtime_error& error) {
49 std::cout << error.what() << std::endl;
50 }
51}
52
53void OptimizationProcessor::initialize(std::string inFilename, std::string outFilename) {
54 std::cout << "[OptimizationProcessor] Initialize " << inFilename << std::endl;
55 // create output file
56 outFile_ = new TFile(outFilename.c_str(), "RECREATE");
57
58 // Define Mass window
61
62 std::cout << "[OptimizationProcessor]::Mass Window: " << lowMass_ << " - " << highMass_ << std::endl;
63
64 // Read signal ana vertex tuple, and convert to mutable tuple
65 signalVtxAnaFile_ = new TFile(signalVtxAnaFilename_.c_str(), "READ");
66
67 // Initialize Persistent Cut Selector. These cuts are applied to all events.
68 // Persistent Cut values are updated each iteration with the value of the best performing Test Cut in
69 // that iteration.
70 std::cout << "[OptimizationProcessor]::Initializing Set of Persistent Cuts" << std::endl;
71 persistentCutsSelector_ = new BaseSelector("persistentCuts", cuts_cfgFile_);
72 persistentCutsSelector_->LoadSelection();
73
74 // Initialize Test Cuts
75 std::cout << "[OptimizationProcessor]::Initializing Set of Test Cuts" << std::endl;
77 testCutsSelector_->LoadSelection();
78
79 // Initialize signal histograms
80 std::cout << "[OptimizationProcessor]::Initializing Signal Variable Histograms" << std::endl;
81 signalHistos_ = std::make_shared<ZBiHistos>("signal");
82 signalHistos_->debugMode(debug_);
84 signalHistos_->DefineHistos();
85
86 // Initialize background histograms
87 std::cout << "[OptimizationProcessor]::Initializing Background Variable Histograms" << std::endl;
88 bkgHistos_ = std::make_shared<ZBiHistos>("background");
89 bkgHistos_->debugMode(debug_);
90 bkgHistos_->loadHistoConfig(variableHistCfgFilename_);
91 bkgHistos_->DefineHistos();
92
93 // Initialize Test Cut histograms
94 std::cout << "[OptimizationProcessor]::Initializing Test Cut Variable Histograms" << std::endl;
95 testCutHistos_ = std::make_shared<ZBiHistos>("testCutHistos");
96 testCutHistos_->debugMode(debug_);
97 testCutHistos_->DefineHistos();
98
99 // Initialize processor histograms that summarize iterative results
100 std::cout << "[OptimizationProcessor]::Initializing Iterative Result Histograms" << std::endl;
101 processorHistos_ = std::make_shared<ZBiHistos>("zbi_processor");
102 processorHistos_->defineZBiCutflowProcessorHistograms();
103
104 // Write initial variable histograms for signal and background
105 std::cout << "[OptimizationProcessor]::Writing Initial Histograms" << std::endl;
106 signalHistos_->writeHistos(outFile_, "initial_signal");
107 bkgHistos_->writeHistos(outFile_, "initial_background");
108}
109
111 std::cout << "[OptimizationProcessor]::process()" << std::endl;
112 return true;
113}
114
116 std::cout << "[OptimizationProcessor]::finalize()" << std::endl;
117
118 processorHistos_->saveHistos(outFile_);
119 testCutHistos_->writeGraphs(outFile_, "testCutGraphs");
120 outFile_->Close();
122}
123
124double OptimizationProcessor::calculateZBi(double n_on, double n_off, double tau) {
125 double P_Bi = TMath::BetaIncomplete(1. / (1. + tau), n_on, n_off + 1);
126 // std::cout << "P_Bi: " << P_Bi << std::endl;
127 double Z_Bi = std::pow(2, 0.5) * TMath::ErfInverse(1 - 2 * P_Bi);
128 // std::cout << "Z_Bi: " << Z_Bi << std::endl;
129 return Z_Bi;
130}
131
132void OptimizationProcessor::writeTH1F(TFile* outF, std::string folder, TH1F* h) {
133 if (outF) outF->cd();
134 TDirectory* dir{nullptr};
135 if (!folder.empty()) {
136 dir = outF->mkdir(folder.c_str(), "", true);
137 dir->cd();
138 }
139 h->Write();
140}
141
142void OptimizationProcessor::writeGraph(TFile* outF, std::string folder, TGraph* g) {
143 if (outF) outF->cd();
144 TDirectory* dir{nullptr};
145 if (!folder.empty()) {
146 dir = outF->mkdir(folder.c_str(), "", true);
147 dir->cd();
148 }
149 g->Write();
150}
151
#define DECLARE_PROCESSOR(CLASS)
Macro which allows the framework to construct a producer given its name during configuration.
Definition Processor.h:139
brief description
Cutflow optimization tool.
double calculateZBi(double n_on, double n_off, double tau)
description
std::vector< double > new_variable_params_
std::shared_ptr< ZBiHistos > bkgHistos_
virtual void configure(const ParameterSet &parameters)
description
virtual void initialize(TTree *tree)
description
std::vector< std::string > cutVariables_
virtual void finalize()
description
std::vector< std::string > new_variables_
BaseSelector * persistentCutsSelector_
OptimizationProcessor(const std::string &name, Process &process)
void writeTH1F(TFile *outF, std::string folder, TH1F *h)
description
void writeGraph(TFile *outF, std::string folder, TGraph *g)
description
std::shared_ptr< ZBiHistos > processorHistos_
std::shared_ptr< ZBiHistos > signalHistos_
std::shared_ptr< ZBiHistos > testCutHistos_
virtual bool process(IEvent *event)
description
virtual bool process()
description
description
Processor(const std::string &name, Process &process)
Class constructor.
Definition Processor.cxx:10