dataset.hpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 minitreedataset_h
  32. #define minitreedataset_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->GetEntry(next_entry);
  56. ++next_entry;
  57. return true;
  58. }
  59. int get_events(){
  60. return nentries;
  61. }
  62. int get_current_event(){
  63. return next_entry-1;
  64. }
  65. void save_event_count_and_xsection(){
  66. return; //TODO: Rewrite
  67. std::map<std::string,int> event_counts;
  68. std::map<std::string,float> xsecs;
  69. string fname, label;
  70. for(auto& p : input_labels){
  71. std::tie(fname, label) = p;
  72. TFile f(fname.c_str());
  73. TH1D* count = (TH1D*)f.Get("Count");
  74. event_counts[label] = (int)count->GetBinContent(1);
  75. TTree* tree = (TTree*)f.Get("tree");
  76. TBranch* b = tree->GetBranch("xsec");
  77. float xsec;
  78. b->SetAddress(&xsec);
  79. b->GetEntry(1);
  80. xsecs[label] = xsec;
  81. }
  82. output_file->cd();
  83. gDirectory->WriteObjectAny(&event_counts, "std::map<std::string,int>", "_event_counts");
  84. gDirectory->WriteObjectAny(&xsecs, "std::map<std::string,float>", "_xsecs");
  85. }
  86. public:
  87. TreeDataSet(const std::string& output_filename,
  88. const std::string& input_filename,
  89. const std::string& data_label,
  90. const std::string& tree_name="tree")
  91. :DataSet(),
  92. input_categories({ {input_filename, "signal"} }),
  93. input_labels({ {input_filename, data_label} }),
  94. output_filename(output_filename),
  95. next_entry(0) {
  96. TChain* chain = new TChain(tree_name.c_str());
  97. chain->Add(input_filename.c_str());
  98. tree_obj = new TREE_CLASS(chain);
  99. nentries = tree_obj->fChain->GetEntries();
  100. output_file = TFile::Open(output_filename.c_str(), "RECREATE");
  101. tree_obj->fChain->SetBranchStatus("*", false);
  102. }
  103. // TODO: Rewrite this constructor
  104. /* MiniTreeDataSet(const std::string& output_filename, const std::map<std::string,std::string>& filenames_with_labels) */
  105. /* :DataSet(), */
  106. /* input_categories(filenames_with_labels), */
  107. /* output_filename(output_filename), */
  108. /* next_entry(0) { */
  109. /* TChain* chain = new TChain("tree"); */
  110. /* for(auto& p : filenames_with_labels){ */
  111. /* std::string filename; */
  112. /* std::tie(filename, std::ignore) = p; */
  113. /* chain->Add(filename.c_str()); */
  114. /* } */
  115. /* Init(chain); */
  116. /* nentries = fChain->GetEntries(); */
  117. /* output_file = TFile::Open(output_filename.c_str(), "RECREATE"); */
  118. /* this->fChain->SetBranchStatus("*", false); */
  119. /* } */
  120. ~TreeDataSet(){
  121. save_event_count_and_xsection();
  122. output_file->Close();
  123. }
  124. const std::string& get_current_event_label() const{
  125. TFile* file = tree_obj->fChain->GetFile();
  126. std::string filename = file->GetName();
  127. return input_categories.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. tree_obj->fChain->SetBranchStatus(bname.c_str(), 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. tree_obj->fChain->SetBranchStatus(bname.c_str(), 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. template <typename T>
  156. decltype(auto) track_branch_vec(const std::string& size_bname, const std::string& value_bname){
  157. track_branch_ptr<T>(value_bname);
  158. return wrapper_vector<T>(lookup<int>(size_bname), lookup<T*>(value_bname), value_bname);
  159. }
  160. void save_all(){
  161. output_file->cd();
  162. // Save the value names for each container to enable looking up
  163. // what values are plotted
  164. std::map<string,string> value_lookup = this->get_container_name_value_map();
  165. gDirectory->WriteObjectAny(&value_lookup, "std::map<std::string,std::string>", "_value_lookup");
  166. std::map<string,string> fn_impl_lookup = this->get_function_name_impl_map();
  167. gDirectory->WriteObjectAny(&fn_impl_lookup, "std::map<std::string,std::string>", "_function_impl_lookup");
  168. for(auto container : containers){
  169. container.second->save_as("outfile", SaveOption::ROOT);
  170. }
  171. }
  172. };
  173. #endif // minitreedataset_h