dataset.hpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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::util;
  41. using namespace fv::root;
  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. bool load_next(){
  56. if (next_entry >= nentries) return false;
  57. tree_obj->LoadTree(next_entry);
  58. tree_obj->GetEntry(next_entry);
  59. ++next_entry;
  60. return true;
  61. }
  62. int get_events(){
  63. return nentries;
  64. }
  65. int get_current_event(){
  66. return next_entry-1;
  67. }
  68. fv::util::DataFileDescriptor& get_current_file(){
  69. TFile* file = tree_obj->fChain->GetCurrentFile();
  70. std::string filename = file->GetName();
  71. return file_descriptors.at(filename);
  72. }
  73. /* void save_event_count(){ */
  74. /* std::map<std::string,int> event_counts; */
  75. /* std::map<std::string,float> xsecs; */
  76. /* string fname, label; */
  77. /* for(auto& p : input_labels){ */
  78. /* std::tie(fname, label) = p; */
  79. /* TFile f(fname.c_str()); */
  80. /* TH1D* count = (TH1D*)f.Get("Count"); */
  81. /* event_counts[label] = (int)count->GetBinContent(1); */
  82. /* TTree* tree = (TTree*)f.Get("tree"); */
  83. /* TBranch* b = tree->GetBranch("xsec"); */
  84. /* float xsec; */
  85. /* b->SetAddress(&xsec); */
  86. /* b->GetEntry(1); */
  87. /* xsecs[label] = xsec; */
  88. /* } */
  89. /* output_file->cd(); */
  90. /* gDirectory->WriteObjectAny(&event_counts, "std::map<std::string,int>", "_event_counts"); */
  91. /* gDirectory->WriteObjectAny(&xsecs, "std::map<std::string,float>", "_xsecs"); */
  92. /* } */
  93. public:
  94. TreeDataSet(const std::string& output_filename, const std::vector<DataFileDescriptor>& dfds,
  95. const std::string& tree_name)
  96. :DataSet(),
  97. output_filename(output_filename),
  98. next_entry(0) {
  99. TChain* chain = new TChain(tree_name.c_str());
  100. int cnt = 0;
  101. for(const DataFileDescriptor& dfd : dfds){
  102. file_descriptors[dfd.filename] = dfd;
  103. /* file_descriptors.insert(std::make_pair(dfd.filename, dfd)); */
  104. /* file_descriptors.emplace(std::make_pair(dfd.filename, dfd)); */
  105. file_descriptors[dfd.filename].file_number = cnt++;
  106. chain->Add(dfd.filename.c_str());
  107. input_labels[dfd.filename] = dfd.label;
  108. input_categories[dfd.filename] = dfd.category;
  109. }
  110. tree_obj = new TREE_CLASS(chain);
  111. nentries = tree_obj->fChain->GetEntries();
  112. output_file = TFile::Open(output_filename.c_str(), "RECREATE");
  113. set_branch_status("*", false);
  114. }
  115. ~TreeDataSet(){
  116. /* save_event_count_and_xsection(); */
  117. output_file->Close();
  118. }
  119. const std::string& get_current_event_category() const{
  120. TFile* file = tree_obj->fChain->GetCurrentFile();
  121. std::string filename = file->GetName();
  122. return input_categories.at(filename);
  123. }
  124. const std::string& get_current_event_label() const{
  125. TFile* file = tree_obj->fChain->GetCurrentFile();
  126. std::string filename = file->GetName();
  127. return input_labels.at(filename);
  128. }
  129. template <typename T>
  130. Value<T>* track_branch(const std::string& bname){
  131. TBranch* branch = tree_obj->fChain->GetBranch(bname.c_str());
  132. if (branch == nullptr){
  133. CRITICAL("Branch: " << bname << " does not exist in input tree.", -1);
  134. }
  135. T* bref = (T*) branch->GetAddress();
  136. set_branch_status(bname, true);
  137. INFO("Registering branch \"" << bname
  138. << "\" with address " << bref
  139. << " and type " << typeid(bref).name());
  140. return new ObservedValue<T>(bname, bref);
  141. }
  142. template <typename T>
  143. Value<T*>* track_branch_ptr(const std::string& bname){
  144. TBranch* branch = tree_obj->fChain->GetBranch(bname.c_str());
  145. if (branch == nullptr){
  146. CRITICAL("Branch: " << bname << " does not exist in input tree.", -1);
  147. }
  148. T* bref = (T*) branch->GetAddress();
  149. set_branch_status(bname, true);
  150. INFO("Registering pointer branch \"" << bname
  151. << "\" with address " << bref
  152. << " and type " << typeid(bref).name());
  153. return new PointerValue<T>(bname, bref);
  154. }
  155. void set_branch_status(const std::string& bname, bool status){
  156. unsigned int found=0;
  157. tree_obj->fChain->SetBranchStatus(bname.c_str(), true, &found);
  158. if (!found){
  159. DataFileDescriptor& dfd = get_current_file();
  160. CRITICAL("Branch: " << bname << " does not exist in input tree of file "
  161. << dfd.filename, -1);
  162. }
  163. }
  164. template <typename T>
  165. Value<T>* track_branch_obj(const std::string& bname){
  166. TBranch* branch = tree_obj->fChain->GetBranch(bname.c_str());
  167. if (branch == nullptr){
  168. CRITICAL("Branch: " << bname << " does not exist in input tree.", -1);
  169. }
  170. T** bref = (T**) branch->GetAddress();
  171. set_branch_status(bname, true);
  172. INFO("Registering object branch \"" << bname
  173. << "\" with address " << bref
  174. << " and type " << typeid(bref).name());
  175. return new ObjectValue<T>(bname, bref);
  176. }
  177. template <typename T>
  178. decltype(auto) track_branch_vec(const std::string& size_bname, const std::string& value_bname){
  179. track_branch_ptr<T>(value_bname);
  180. return wrapper_vector<T>(lookup<int>(size_bname), lookup<T*>(value_bname), value_bname);
  181. }
  182. void save_all(){
  183. output_file->cd();
  184. // Save the value names for each container to enable looking up
  185. // what values are plotted
  186. std::map<string,string> value_lookup = this->get_container_name_value_map();
  187. gDirectory->WriteObjectAny(&value_lookup, "std::map<std::string,std::string>", "_value_lookup");
  188. std::map<string,string> fn_impl_lookup = this->get_function_name_impl_map();
  189. gDirectory->WriteObjectAny(&fn_impl_lookup, "std::map<std::string,std::string>", "_function_impl_lookup");
  190. for(auto container : containers){
  191. container.second->save_as("outfile", SaveOption::ROOT);
  192. }
  193. }
  194. };
  195. #endif // root_dataset_h