dataset.hpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 label, eg. "TTTT", or "TTZ"
  45. std::map<std::string,std::string> input_labels;
  46. // Maps filenames to data category. Either "signal" or "background"
  47. std::map<std::string,std::string> input_categories;
  48. std::string output_filename;
  49. std::vector<fv::util::DataFileDescriptor> dfds;
  50. TFile* output_file;
  51. TREE_CLASS* tree_obj;
  52. long next_entry;
  53. long nentries;
  54. bool load_next(){
  55. if (next_entry >= nentries) return false;
  56. tree_obj->LoadTree(next_entry);
  57. tree_obj->GetEntry(next_entry);
  58. ++next_entry;
  59. return true;
  60. }
  61. int get_events(){
  62. return nentries;
  63. }
  64. int get_current_event(){
  65. return next_entry-1;
  66. }
  67. /* void save_event_count(){ */
  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, const std::vector<fv::util::DataFileDescriptor>& dfds,
  89. const std::string& tree_name)
  90. :DataSet(),
  91. dfds(dfds),
  92. output_filename(output_filename),
  93. next_entry(0) {
  94. TChain* chain = new TChain(tree_name.c_str());
  95. for(auto& dfd : dfds){
  96. chain->Add(dfd.filename.c_str());
  97. input_labels[dfd.filename] = dfd.label;
  98. input_categories[dfd.filename] = dfd.category;
  99. }
  100. tree_obj = new TREE_CLASS(chain);
  101. nentries = tree_obj->fChain->GetEntries();
  102. output_file = TFile::Open(output_filename.c_str(), "RECREATE");
  103. tree_obj->fChain->SetBranchStatus("*", false);
  104. }
  105. ~TreeDataSet(){
  106. /* save_event_count_and_xsection(); */
  107. output_file->Close();
  108. }
  109. const std::string& get_current_event_category() const{
  110. TFile* file = tree_obj->fChain->GetFile();
  111. std::string filename = file->GetName();
  112. return input_categories.at(filename);
  113. }
  114. template <typename T>
  115. Value<T>* track_branch(const std::string& bname){
  116. TBranch* branch = tree_obj->fChain->GetBranch(bname.c_str());
  117. if (branch == nullptr){
  118. CRITICAL("Branch: " << bname << " does not exist in input tree.", -1);
  119. }
  120. T* bref = (T*) branch->GetAddress();
  121. tree_obj->fChain->SetBranchStatus(bname.c_str(), true);
  122. INFO("Registering branch \"" << bname
  123. << "\" with address " << bref
  124. << " and type " << typeid(bref).name());
  125. return new ObservedValue<T>(bname, bref);
  126. }
  127. template <typename T>
  128. Value<T*>* track_branch_ptr(const std::string& bname){
  129. TBranch* branch = tree_obj->fChain->GetBranch(bname.c_str());
  130. if (branch == nullptr){
  131. CRITICAL("Branch: " << bname << " does not exist in input tree.", -1);
  132. }
  133. T* bref = (T*) branch->GetAddress();
  134. tree_obj->fChain->SetBranchStatus(bname.c_str(), true);
  135. INFO("Registering pointer branch \"" << bname
  136. << "\" with address " << bref
  137. << " and type " << typeid(bref).name());
  138. return new PointerValue<T>(bname, bref);
  139. }
  140. template <typename T>
  141. Value<T>* track_branch_obj(const std::string& bname){
  142. TBranch* branch = tree_obj->fChain->GetBranch(bname.c_str());
  143. if (branch == nullptr){
  144. CRITICAL("Branch: " << bname << " does not exist in input tree.", -1);
  145. }
  146. T** bref = (T**) branch->GetAddress();
  147. tree_obj->fChain->SetBranchStatus(bname.c_str(), true);
  148. INFO("Registering object branch \"" << bname
  149. << "\" with address " << bref
  150. << " and type " << typeid(bref).name());
  151. return new ObjectValue<T>(bname, bref);
  152. }
  153. template <typename T>
  154. decltype(auto) track_branch_vec(const std::string& size_bname, const std::string& value_bname){
  155. track_branch_ptr<T>(value_bname);
  156. return wrapper_vector<T>(lookup<int>(size_bname), lookup<T*>(value_bname), value_bname);
  157. }
  158. void save_all(){
  159. output_file->cd();
  160. // Save the value names for each container to enable looking up
  161. // what values are plotted
  162. std::map<string,string> value_lookup = this->get_container_name_value_map();
  163. gDirectory->WriteObjectAny(&value_lookup, "std::map<std::string,std::string>", "_value_lookup");
  164. std::map<string,string> fn_impl_lookup = this->get_function_name_impl_map();
  165. gDirectory->WriteObjectAny(&fn_impl_lookup, "std::map<std::string,std::string>", "_function_impl_lookup");
  166. for(auto container : containers){
  167. container.second->save_as("outfile", SaveOption::ROOT);
  168. }
  169. }
  170. };
  171. #endif // root_dataset_h