31 #ifndef minitreedataset_h 32 #define minitreedataset_h 37 #include "filval/filval.hpp" 38 #include "filval_root/filval_root.hpp" 45 class MiniTreeDataSet :
public DataSet,
49 std::map<std::string,std::string> input_categories;
51 std::map<std::string,std::string> input_labels;
52 std::string output_filename;
57 if (next_entry >= nentries)
return false;
58 fChain->GetEntry(next_entry);
65 int get_current_event(){
69 void save_event_count_and_xsection(){
70 std::map<std::string,int> event_counts;
71 std::map<std::string,float> xsecs;
73 for(
auto& p : input_labels){
74 std::tie(fname, label) = p;
75 TFile f(fname.c_str());
76 TH1D* count = (TH1D*)f.Get(
"Count");
77 event_counts[label] = (int)count->GetBinContent(1);
79 TTree* tree = (TTree*)f.Get(
"tree");
80 TBranch* b = tree->GetBranch(
"xsec");
87 gDirectory->WriteObjectAny(&event_counts,
"std::map<std::string,int>",
"_event_counts");
88 gDirectory->WriteObjectAny(&xsecs,
"std::map<std::string,float>",
"_xsecs");
92 MiniTreeDataSet(
const std::string& output_filename,
const std::string input_filename,
const std::string data_label)
94 input_categories({ {input_filename,
"signal"} }),
95 input_labels({ {input_filename, data_label} }),
96 output_filename(output_filename),
98 TChain* chain =
new TChain(
"tree");
99 chain->Add(input_filename.c_str());
101 nentries = fChain->GetEntries();
102 output_file = TFile::Open(output_filename.c_str(),
"RECREATE");
103 this->fChain->SetBranchStatus(
"*",
false);
106 MiniTreeDataSet(
const std::string& output_filename,
const std::map<std::string,std::string>& filenames_with_labels)
108 input_categories(filenames_with_labels),
109 output_filename(output_filename),
111 TChain* chain =
new TChain(
"tree");
112 for(
auto& p : filenames_with_labels){
113 std::string filename;
114 std::tie(filename, std::ignore) = p;
115 chain->Add(filename.c_str());
118 nentries = fChain->GetEntries();
119 output_file = TFile::Open(output_filename.c_str(),
"RECREATE");
120 this->fChain->SetBranchStatus(
"*",
false);
124 save_event_count_and_xsection();
125 output_file->Close();
128 const std::string& get_current_event_label()
const{
129 TFile* file = fChain->GetFile();
130 std::string filename = file->GetName();
131 return input_categories.at(filename);
134 template <
typename T>
135 Value<T>* track_branch(
const std::string& bname){
136 TBranch* branch = fChain->GetBranch(bname.c_str());
137 if (branch ==
nullptr){
138 CRITICAL(
"Branch: " << bname <<
" does not exist in input tree.", -1);
140 T* bref = (T*) branch->GetAddress();
141 fChain->SetBranchStatus(bname.c_str(),
true);
142 INFO(
"Registering branch \"" << bname
143 <<
"\" with address " << bref
144 <<
" and type " <<
typeid(bref).name());
148 template <
typename T>
149 Value<T*>* track_branch_ptr(
const std::string& bname){
150 TBranch* branch = fChain->GetBranch(bname.c_str());
151 if (branch ==
nullptr){
152 CRITICAL(
"Branch: " << bname <<
" does not exist in input tree.", -1);
154 T* bref = (T*) branch->GetAddress();
155 fChain->SetBranchStatus(bname.c_str(),
true);
156 INFO(
"Registering pointer branch \"" << bname
157 <<
"\" with address " << bref
158 <<
" and type " <<
typeid(bref).name());
162 template <
typename T>
163 decltype(
auto) track_branch_vec(const
std::
string& size_bname, const
std::
string& value_bname){
164 track_branch_ptr<T>(value_bname);
165 return wrapper_vector<T>(lookup<int>(size_bname), lookup<T*>(value_bname), value_bname);
173 std::map<string,string> value_lookup = this->get_container_name_value_map();
174 gDirectory->WriteObjectAny(&value_lookup,
"std::map<std::string,std::string>",
"_value_lookup");
176 std::map<string,string> fn_impl_lookup = this->get_function_name_impl_map();
177 gDirectory->WriteObjectAny(&fn_impl_lookup,
"std::map<std::string,std::string>",
"_function_impl_lookup");
179 for(
auto container : containers){
180 container.second->save_as(
"outfile", SaveOption::ROOT);
184 #endif // minitreedataset_h
A value supplied by the dataset, not derived.
The namespace containing all filval classes and functions.