dataset.hpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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/filval.hpp"
  37. #include "filval/root/filval.hpp"
  38. using namespace std;
  39. using namespace fv;
  40. using namespace fv::root;
  41. template<typename TREE_CLASS>
  42. class TreeDataSet : public DataSet{
  43. private:
  44. // Maps filenames to data category. Either "signal" or "background"
  45. std::map<std::string,std::string> input_categories;
  46. // Maps filenames to data label, eg. "TTTT", or "TTZ"
  47. std::map<std::string,std::string> input_labels;
  48. std::string output_filename;
  49. TFile* output_file;
  50. TREE_CLASS* tree_obj;
  51. long next_entry;
  52. long nentries;
  53. bool load_next(){
  54. if (next_entry >= nentries) return false;
  55. tree_obj->LoadTree(next_entry);
  56. tree_obj->GetEntry(next_entry);
  57. ++next_entry;
  58. return true;
  59. }
  60. int get_events(){
  61. return nentries;
  62. }
  63. int get_current_event(){
  64. return next_entry-1;
  65. }
  66. void save_event_count_and_xsection(){
  67. return; //TODO: Rewrite
  68. std::map<std::string,int> event_counts;
  69. std::map<std::string,float> xsecs;
  70. string fname, label;
  71. for(auto& p : input_labels){
  72. std::tie(fname, label) = p;
  73. TFile f(fname.c_str());
  74. TH1D* count = (TH1D*)f.Get("Count");
  75. event_counts[label] = (int)count->GetBinContent(1);
  76. TTree* tree = (TTree*)f.Get("tree");
  77. TBranch* b = tree->GetBranch("xsec");
  78. float xsec;
  79. b->SetAddress(&xsec);
  80. b->GetEntry(1);
  81. xsecs[label] = xsec;
  82. }
  83. output_file->cd();
  84. gDirectory->WriteObjectAny(&event_counts, "std::map<std::string,int>", "_event_counts");
  85. gDirectory->WriteObjectAny(&xsecs, "std::map<std::string,float>", "_xsecs");
  86. }
  87. public:
  88. TreeDataSet(const std::string& output_filename,
  89. const std::string& input_filename,
  90. const std::string& data_label,
  91. const std::string& tree_name="tree")
  92. :DataSet(),
  93. input_categories({ {input_filename, "signal"} }),
  94. input_labels({ {input_filename, data_label} }),
  95. output_filename(output_filename),
  96. next_entry(0) {
  97. TChain* chain = new TChain(tree_name.c_str());
  98. chain->Add(input_filename.c_str());
  99. tree_obj = new TREE_CLASS(chain);
  100. nentries = tree_obj->fChain->GetEntries();
  101. output_file = TFile::Open(output_filename.c_str(), "RECREATE");
  102. tree_obj->fChain->SetBranchStatus("*", false);
  103. /* print_branch_statuses(); */
  104. }
  105. void print_branch_statuses(){
  106. TObjArray* obj_arr = tree_obj->fChain->GetListOfBranches();
  107. for(TIter iter=obj_arr->begin(); iter!= obj_arr->end(); ++iter){
  108. TBranch* br = (TBranch*)*iter;
  109. std::cout << br->GetName() << ": "
  110. << tree_obj->fChain->GetBranchStatus(br->GetName()) << std::endl;
  111. }
  112. }
  113. // TODO: Rewrite this constructor
  114. /* MiniTreeDataSet(const std::string& output_filename, const std::map<std::string,std::string>& filenames_with_labels) */
  115. /* :DataSet(), */
  116. /* input_categories(filenames_with_labels), */
  117. /* output_filename(output_filename), */
  118. /* next_entry(0) { */
  119. /* TChain* chain = new TChain("tree"); */
  120. /* for(auto& p : filenames_with_labels){ */
  121. /* std::string filename; */
  122. /* std::tie(filename, std::ignore) = p; */
  123. /* chain->Add(filename.c_str()); */
  124. /* } */
  125. /* Init(chain); */
  126. /* nentries = fChain->GetEntries(); */
  127. /* output_file = TFile::Open(output_filename.c_str(), "RECREATE"); */
  128. /* this->fChain->SetBranchStatus("*", false); */
  129. /* } */
  130. ~TreeDataSet(){
  131. save_event_count_and_xsection();
  132. output_file->Close();
  133. }
  134. const std::string& get_current_event_label() const{
  135. TFile* file = tree_obj->fChain->GetFile();
  136. std::string filename = file->GetName();
  137. return input_categories.at(filename);
  138. }
  139. template <typename T>
  140. Value<T>* track_branch(const std::string& bname){
  141. TBranch* branch = tree_obj->fChain->GetBranch(bname.c_str());
  142. if (branch == nullptr){
  143. CRITICAL("Branch: " << bname << " does not exist in input tree.", -1);
  144. }
  145. T* bref = (T*) branch->GetAddress();
  146. tree_obj->fChain->SetBranchStatus(bname.c_str(), true);
  147. INFO("Registering branch \"" << bname
  148. << "\" with address " << bref
  149. << " and type " << typeid(bref).name());
  150. return new ObservedValue<T>(bname, bref);
  151. }
  152. template <typename T>
  153. Value<T*>* track_branch_ptr(const std::string& bname){
  154. TBranch* branch = tree_obj->fChain->GetBranch(bname.c_str());
  155. if (branch == nullptr){
  156. CRITICAL("Branch: " << bname << " does not exist in input tree.", -1);
  157. }
  158. T* bref = (T*) branch->GetAddress();
  159. tree_obj->fChain->SetBranchStatus(bname.c_str(), true);
  160. INFO("Registering pointer branch \"" << bname
  161. << "\" with address " << bref
  162. << " and type " << typeid(bref).name());
  163. return new PointerValue<T>(bname, bref);
  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.", -1);
  170. }
  171. T** bref = (T**) branch->GetAddress();
  172. tree_obj->fChain->SetBranchStatus(bname.c_str(), 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. template <typename T>
  179. decltype(auto) track_branch_vec(const std::string& size_bname, const std::string& value_bname){
  180. track_branch_ptr<T>(value_bname);
  181. return wrapper_vector<T>(lookup<int>(size_bname), lookup<T*>(value_bname), value_bname);
  182. }
  183. void save_all(){
  184. output_file->cd();
  185. // Save the value names for each container to enable looking up
  186. // what values are plotted
  187. std::map<string,string> value_lookup = this->get_container_name_value_map();
  188. gDirectory->WriteObjectAny(&value_lookup, "std::map<std::string,std::string>", "_value_lookup");
  189. std::map<string,string> fn_impl_lookup = this->get_function_name_impl_map();
  190. gDirectory->WriteObjectAny(&fn_impl_lookup, "std::map<std::string,std::string>", "_function_impl_lookup");
  191. for(auto container : containers){
  192. container.second->save_as("outfile", SaveOption::ROOT);
  193. }
  194. }
  195. };
  196. #endif // root_dataset_h