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
TreeCutSelector.cxx
Go to the documentation of this file.
1#include "TreeCutSelector.h"
2
4
5TreeCutSelector::TreeCutSelector(const std::string& inputName) {
6 m_name = inputName;
7 m_cfgFile = "";
8 debug_ = false;
9}
10
11TreeCutSelector::TreeCutSelector(const std::string& inputName, const std::string& cfgFile) {
12 m_name = inputName;
13 m_cfgFile = cfgFile;
14 debug_ = false;
15}
16
18
20 if (m_cfgFile.empty()) {
21 std::cout << "ERROR TreeCutSelector::Configuration file not specified" << std::endl;
22 return false;
23 }
24
25 std::ifstream i_file(m_cfgFile);
26 i_file >> _h_selections;
27 if (debug_) {
28 for (auto& el : _h_selections.items()) std::cout << el.key() << " : " << el.value() << "\n";
29 }
30
31 for (auto cut : _h_selections.items()) {
32 range_cuts[cut.key()].first.first = cut.value().at("min");
33 range_cuts[cut.key()].first.second = cut.value().at("max");
34 range_cuts[cut.key()].second = cut.value().at("id");
35 labels[cut.key()] = cut.value().at("info");
36 ncuts_++;
37 }
38
39 if (debug_) {
40 for (range_cut_it it = range_cuts.begin(); it != range_cuts.end(); ++it) {
41 std::cout << it->first << " [min:] =" << it->second.first.first << " [max:] =" << it->second.first.second
42 << " [id:] =" << it->second.second << std::endl;
43 }
44 }
45
46 // makeCutFlowHisto();
47 return true;
48}
49
51 h_cf_ =
52 std::make_shared<TH1F>((m_name + "_cutflow").c_str(), (m_name + "_cutflow").c_str(), ncuts_ + 1, 0, ncuts_ + 1);
53 h_cf_->GetXaxis()->SetCanExtend(true);
54 h_cf_->Sumw2();
55 h_cf_->GetXaxis()->SetBinLabel(1, "no-cuts");
56
57 for (range_cut_it it = range_cuts.begin(); it != range_cuts.end(); ++it) {
58 h_cf_->GetXaxis()->SetBinLabel((it->second.second + 2), labels[(it->first)].c_str());
59 }
60}
61
62std::string TreeCutSelector::getCutVar(std::string cutname, bool forTree) {
63 std::string var = "";
64 if (forTree) {
65 var = _h_selections.at(cutname).at("var");
66 } else {
67 var = cutname;
68 }
69 return var;
70}
71
72std::string TreeCutSelector::getCutString(std::string cutname, bool forTree) {
73 std::string cutstring = "";
74 if (!hasCut(cutname)) {
75 std::cout << "ERROR " << cutname << " cut not implemented" << std::endl;
76 return cutstring;
77 }
78 std::string var = getCutVar(cutname);
79
80 if (cutname == "pos_z0" || cutname == "ele_z0" || cutname == "min_y0") {
81 double zoffset = range_cuts[cutname].first.first; // mm
82 double alpha = range_cuts[cutname].first.second; // rad
83 std::string zvertex = "vertex_z";
84 if (forTree) zvertex = "vertex.getZ()";
85 cutstring += "(" + zvertex + " * " + std::to_string(alpha) + " - abs(" + var + ") < " + std::to_string(alpha) +
86 " * " + std::to_string(zoffset) + ")";
87 } else {
88 if (range_cuts[cutname].first.first > -999.) {
89 cutstring += var + " >= " + std::to_string(range_cuts[cutname].first.first);
90 }
91 if (range_cuts[cutname].first.second < 999.) {
92 if (!cutstring.empty()) cutstring += " && ";
93 cutstring += var + " <= " + std::to_string(range_cuts[cutname].first.second);
94 }
95 }
96
97 return cutstring;
98}
99
100std::string TreeCutSelector::getFullCutString(std::vector<std::string> cuts_to_exclude, bool forTree) {
101 std::string full_cutstring = "";
102 for (std::map<std::string, std::pair<std::pair<double, double>, int>>::iterator it = getPointerToCuts()->begin();
103 it != getPointerToCuts()->end(); ++it) {
104 std::string cutname = it->first;
105 bool exclude = false;
106 for (std::vector<std::string>::iterator eit = cuts_to_exclude.begin(); eit != cuts_to_exclude.end(); eit++) {
107 if (*eit == cutname) {
108 exclude = true;
109 break;
110 }
111 }
112 if (exclude) continue;
113
114 std::string cutstring = getCutString(cutname, forTree);
115 if (!cutstring.empty()) {
116 if (!full_cutstring.empty()) full_cutstring += " && ";
117 full_cutstring += cutstring;
118 }
119 }
120 if (full_cutstring.empty()) full_cutstring = "1"; // no cuts, always true
121
122 full_cutstring = "(" + full_cutstring + ")";
123 return full_cutstring;
124}
125
126void TreeCutSelector::filterCuts(std::vector<std::string> cut_variable_list) {
127 // Loop over Test Cuts loaded in from json configuration
128 // for(std::map<std::string, std::pair<double,int>>::iterator it = cuts_ptr->begin(); it != cuts_ptr->end(); it++){
129 for (std::map<std::string, std::pair<std::pair<double, double>, int>>::iterator it = getPointerToCuts()->begin();
130 it != getPointerToCuts()->end();) {
131 std::string cutname = it->first;
132 bool found = false;
133
134 // Confirm that cut variable is in list of configurable cut variables
135 for (std::vector<std::string>::iterator iit = cut_variable_list.begin(); iit != cut_variable_list.end();
136 iit++) {
137 if ((std::string)*iit == cutname) {
138 found = true;
139 break;
140 }
141 }
142
143 // If Test Cut Variable does not exist, remove the Test Cut from the list of cuts
144 if (!found) {
145 it = getPointerToCuts()->erase(it);
146 } else
147 ++it;
148 }
149}
bool debug_
description
std::string getCutString(std::string cutname, bool forTree=false)
std::map< std::string, std::pair< std::pair< double, double >, int > > * getPointerToCuts()
get pointer to the base class cuts
json _h_selections
description
std::string getFullCutString(std::vector< std::string > cuts_to_exclude={}, bool forTree=false)
int ncuts_
description
bool hasCut(const std::string &cutname)
std::string getCutVar(std::string cutname, bool forTree=false)
void filterCuts(std::vector< std::string > cut_variable_list)
virtual ~TreeCutSelector()
std::shared_ptr< TH1F > h_cf_
description
std::map< std::string, std::pair< std::pair< double, double >, int > >::iterator range_cut_it
description
void makeCutFlowHisto()
description
std::map< std::string, std::pair< std::pair< double, double >, int > > range_cuts
std::string m_cfgFile
description
std::string m_name
description
std::map< std::string, std::string > labels
description