root_dataset.hpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /**
  2. * @file
  3. * @author Caleb Fangmeier <caleb@fangmeier.tech>
  4. * @version 0.1
  5. *
  6. * @section LICENSE
  7. *
  8. *
  9. * MIT License
  10. *
  11. * Copyright (c) 2017 Caleb Fangmeier
  12. *
  13. * Permission is hereby granted, free of charge, to any person obtaining a copy
  14. * of this software and associated documentation files (the "Software"), to deal
  15. * in the Software without restriction, including without limitation the rights
  16. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  17. * copies of the Software, and to permit persons to whom the Software is
  18. * furnished to do so, subject to the following conditions:
  19. *
  20. * The above copyright notice and this permission notice shall be included in all
  21. * copies or substantial portions of the Software.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  26. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  28. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  29. * SOFTWARE.
  30. */
  31. #ifndef root_dataset_h
  32. #define root_dataset_h
  33. #include <string>
  34. #include <tuple>
  35. #include "TChain.h"
  36. #include "filval.hpp"
  37. #include "root_filval.hpp"
  38. namespace fv_root {
  39. using namespace std;
  40. using namespace fv;
  41. using namespace fv_util;
  42. template<typename TREE_CLASS>
  43. class TreeDataSet : public DataSet {
  44. private:
  45. // Maps filenames to data label, eg. "TTTT", or "TTZ"
  46. std::map<std::string, std::string> input_labels;
  47. // Maps filenames to data category. Either "signal" or "background"
  48. std::map<std::string, std::string> input_categories;
  49. std::string output_filename;
  50. std::map<std::string, DataFileDescriptor> file_descriptors;
  51. TFile *output_file;
  52. TREE_CLASS *tree_obj;
  53. long next_entry;
  54. long nentries;
  55. int get_events() {
  56. if (this->max_events) return std::min(this->max_events, nentries);
  57. else return nentries;
  58. }
  59. fv_util::DataFileDescriptor &get_current_file() {
  60. TFile *file = tree_obj->fChain->GetCurrentFile();
  61. std::string filename = file->GetName();
  62. return file_descriptors.at(filename);
  63. }
  64. bool load_next() {
  65. if (next_entry >= get_events()) return false;
  66. tree_obj->LoadTree(next_entry);
  67. tree_obj->GetEntry(next_entry);
  68. ++next_entry;
  69. return true;
  70. }
  71. /* void save_event_count(){ */
  72. /* std::map<std::string,int> event_counts; */
  73. /* std::map<std::string,float> xsecs; */
  74. /* string fname, label; */
  75. /* for(auto& p : input_labels){ */
  76. /* std::tie(fname, label) = p; */
  77. /* TFile f(fname.c_str()); */
  78. /* TH1D* count = (TH1D*)f.Get("Count"); */
  79. /* event_counts[label] = (int)count->GetBinContent(1); */
  80. /* TTree* tree = (TTree*)f.Get("tree"); */
  81. /* TBranch* b = tree->GetBranch("xsec"); */
  82. /* float xsec; */
  83. /* b->SetAddress(&xsec); */
  84. /* b->GetEntry(1); */
  85. /* xsecs[label] = xsec; */
  86. /* } */
  87. /* output_file->cd(); */
  88. /* gDirectory->WriteObjectAny(&event_counts, "std::map<std::string,int>", "_event_counts"); */
  89. /* gDirectory->WriteObjectAny(&xsecs, "std::map<std::string,float>", "_xsecs"); */
  90. /* } */
  91. public:
  92. TreeDataSet(const std::string &output_filename, const std::vector<DataFileDescriptor> &dfds,
  93. const std::string &tree_name)
  94. : DataSet(),
  95. output_filename(output_filename),
  96. next_entry(0) {
  97. TChain *chain = new TChain(tree_name.c_str());
  98. int cnt = 0;
  99. for (const DataFileDescriptor &dfd : dfds) {
  100. file_descriptors[dfd.filename] = dfd;
  101. /* file_descriptors.insert(std::make_pair(dfd.filename, dfd)); */
  102. /* file_descriptors.emplace(std::make_pair(dfd.filename, dfd)); */
  103. file_descriptors[dfd.filename].file_number = cnt++;
  104. chain->Add(dfd.filename.c_str());
  105. input_labels[dfd.filename] = dfd.label;
  106. input_categories[dfd.filename] = dfd.category;
  107. }
  108. tree_obj = new TREE_CLASS(chain);
  109. nentries = tree_obj->fChain->GetEntries();
  110. output_file = TFile::Open(output_filename.c_str(), "RECREATE");
  111. set_branch_status("*", false);
  112. }
  113. ~TreeDataSet() {
  114. /* save_event_count_and_xsection(); */
  115. output_file->Close();
  116. }
  117. int get_current_event() {
  118. return next_entry - 1;
  119. }
  120. const std::string &get_current_event_category() const {
  121. TFile *file = tree_obj->fChain->GetCurrentFile();
  122. std::string filename = file->GetName();
  123. return input_categories.at(filename);
  124. }
  125. const std::string &get_current_event_label() const {
  126. TFile *file = tree_obj->fChain->GetCurrentFile();
  127. std::string filename = file->GetName();
  128. return input_labels.at(filename);
  129. }
  130. template<typename T>
  131. Value<T> *track_branch(const std::string &bname) {
  132. TBranch *branch = tree_obj->fChain->GetBranch(bname.c_str());
  133. if (branch == nullptr) {
  134. CRITICAL("Branch: " << bname << " does not exist in input tree.");
  135. }
  136. T *bref = (T *) branch->GetAddress();
  137. set_branch_status(bname, true);
  138. INFO("Registering branch \"" << bname
  139. << "\" with address " << bref
  140. << " and type " << typeid(bref).name());
  141. return new ObservedValue<T>(bname, bref);
  142. }
  143. template<typename T>
  144. Value<T *> *track_branch_ptr(const std::string &bname) {
  145. TBranch *branch = tree_obj->fChain->GetBranch(bname.c_str());
  146. if (branch == nullptr) {
  147. CRITICAL("Branch: " << bname << " does not exist in input tree.");
  148. }
  149. T *bref = (T *) branch->GetAddress();
  150. set_branch_status(bname, true);
  151. INFO("Registering pointer branch \"" << bname
  152. << "\" with address " << bref
  153. << " and type " << typeid(bref).name());
  154. return new PointerValue<T>(bname, bref);
  155. }
  156. void set_branch_status(const std::string &bname, bool status) {
  157. unsigned int found = 0;
  158. tree_obj->fChain->SetBranchStatus(bname.c_str(), true, &found);
  159. if (!found) {
  160. DataFileDescriptor &dfd = get_current_file();
  161. CRITICAL("Branch: " << bname << " does not exist in input tree of file "
  162. << dfd.filename);
  163. }
  164. }
  165. template<typename T>
  166. Value<T> *track_branch_obj(const std::string &bname) {
  167. TBranch *branch = tree_obj->fChain->GetBranch(bname.c_str());
  168. if (branch == nullptr) {
  169. CRITICAL("Branch: " << bname << " does not exist in input tree.");
  170. }
  171. T **bref = (T **) branch->GetAddress();
  172. set_branch_status(bname, true);
  173. INFO("Registering object branch \"" << bname
  174. << "\" with address " << bref
  175. << " and type " << typeid(bref).name());
  176. return new ObjectValue<T>(bname, bref);
  177. }
  178. void save_config() {
  179. if (the_config == nullptr) return;
  180. TObjString config(the_config->as_string().c_str());
  181. output_file->cd();
  182. config.Write("_config");
  183. }
  184. void save_all() {
  185. output_file->cd();
  186. // Save the value names for each container to enable looking up
  187. // what values are plotted
  188. std::map<string, string> value_lookup = this->get_container_name_value_map();
  189. gDirectory->WriteObjectAny(&value_lookup, "std::map<std::string,std::string>", "_value_lookup");
  190. for (auto container : containers) {
  191. container.second->save_as("outfile", SaveOption::ROOT);
  192. }
  193. save_config();
  194. }
  195. };
  196. }
  197. #endif // root_dataset_h