MiniTreeDataSet.hpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 "filval/filval.hpp"
  36. #include "filval_root/filval_root.hpp"
  37. #include "MiniTree.hpp"
  38. using namespace std;
  39. using namespace fv;
  40. using namespace fv::root;
  41. class MiniTreeDataSet : public DataSet,
  42. public MiniTree{
  43. private:
  44. std::map<std::string,std::string> input_filenames_with_labels;
  45. /* std::vector<TFile*> input_files; */
  46. std::string output_filename;
  47. TFile* output_file;
  48. long next_entry;
  49. long nentries;
  50. bool load_next(){
  51. if (next_entry >= nentries) return false;
  52. fChain->GetEntry(next_entry);
  53. ++next_entry;
  54. return true;
  55. }
  56. int get_events(){
  57. return nentries;
  58. }
  59. int get_current_event(){
  60. return next_entry-1;
  61. }
  62. public:
  63. MiniTreeDataSet(const std::string& output_filename, const std::string input_filename)
  64. :DataSet(),
  65. input_filenames_with_labels({ {input_filename, "signal"} }),
  66. output_filename(output_filename),
  67. next_entry(0) {
  68. TChain* chain = new TChain("tree");
  69. chain->Add(input_filename.c_str());
  70. Init(chain);
  71. nentries = fChain->GetEntries();
  72. output_file = TFile::Open(output_filename.c_str(), "RECREATE");
  73. this->fChain->SetBranchStatus("*", false);
  74. }
  75. MiniTreeDataSet(const std::string& output_filename, const std::map<std::string,std::string>& filenames_with_labels)
  76. :DataSet(),
  77. input_filenames_with_labels(filenames_with_labels),
  78. output_filename(output_filename),
  79. next_entry(0) {
  80. TChain* chain = new TChain("tree");
  81. for(auto& p : filenames_with_labels){
  82. std::string filename;
  83. std::tie(filename, std::ignore) = p;
  84. chain->Add(filename.c_str());
  85. }
  86. Init(chain);
  87. nentries = fChain->GetEntries();
  88. output_file = TFile::Open(output_filename.c_str(), "RECREATE");
  89. this->fChain->SetBranchStatus("*", false);
  90. }
  91. ~MiniTreeDataSet(){
  92. output_file->Close();
  93. }
  94. const std::string& get_current_event_label() const{
  95. TFile* file = fChain->GetFile();
  96. std::string filename = file->GetName();
  97. return input_filenames_with_labels.at(filename);
  98. }
  99. template <typename T>
  100. Value<T>* track_branch(const std::string& bname){
  101. TBranch* branch = fChain->GetBranch(bname.c_str());
  102. if (branch == nullptr){
  103. CRITICAL("Branch: " << bname << " does not exist in input tree.", -1);
  104. }
  105. T* bref = (T*) branch->GetAddress();
  106. fChain->SetBranchStatus(bname.c_str(), true);
  107. INFO("Registering branch \"" << bname
  108. << "\" with address " << bref
  109. << " and type " << typeid(bref).name());
  110. return new ObservedValue<T>(bname, bref);
  111. }
  112. template <typename T>
  113. Value<T*>* track_branch_ptr(const std::string& bname){
  114. TBranch* branch = fChain->GetBranch(bname.c_str());
  115. if (branch == nullptr){
  116. CRITICAL("Branch: " << bname << " does not exist in input tree.", -1);
  117. }
  118. T* bref = (T*) branch->GetAddress();
  119. fChain->SetBranchStatus(bname.c_str(), true);
  120. INFO("Registering pointer branch \"" << bname
  121. << "\" with address " << bref
  122. << " and type " << typeid(bref).name());
  123. return new PointerValue<T>(bname, bref);
  124. }
  125. template <typename T>
  126. decltype(auto) track_branch_vec(const std::string& size_bname, const std::string& value_bname){
  127. track_branch_ptr<T>(value_bname);
  128. return wrapper_vector<T>(lookup<int>(size_bname), lookup<T*>(value_bname), value_bname);
  129. }
  130. void save_all(){
  131. output_file->cd();
  132. // Save the value names for each container to enable looking up
  133. // what values are plotted
  134. std::map<string,string> value_lookup = this->get_container_name_value_map();
  135. gDirectory->WriteObjectAny(&value_lookup, "std::map<std::string,std::string>", "_value_lookup");
  136. std::map<string,string> fn_impl_lookup = this->get_function_name_impl_map();
  137. gDirectory->WriteObjectAny(&fn_impl_lookup, "std::map<std::string,std::string>", "_function_impl_lookup");
  138. for(auto container : containers){
  139. container.second->save_as("outfile", SaveOption::ROOT);
  140. }
  141. }
  142. };
  143. #endif // minitreedataset_h