MiniTreeDataSet.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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::string input_filename;
  45. std::string output_filename;
  46. TFile* input_file;
  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& input_filename, const std::string output_filename)
  64. :input_filename(input_filename),
  65. output_filename(output_filename),
  66. next_entry(0) {
  67. input_file = TFile::Open(input_filename.c_str());
  68. Init((TTree*) input_file->Get("tree"));
  69. nentries = fChain->GetEntriesFast();
  70. output_file = TFile::Open(output_filename.c_str(), "RECREATE");
  71. this->fChain->SetBranchStatus("*", false);
  72. }
  73. ~MiniTreeDataSet(){
  74. input_file->Close();
  75. output_file->Close();
  76. }
  77. template <typename T>
  78. Value<T>* track_branch(const std::string& bname){
  79. TBranch* branch = fChain->GetBranch(bname.c_str());
  80. if (branch == nullptr){
  81. CRITICAL("Branch: " << bname << " does not exist in input tree.", -1);
  82. }
  83. T* bref = (T*) branch->GetAddress();
  84. fChain->SetBranchStatus(bname.c_str(), true);
  85. INFO("Registering branch \"" << bname
  86. << "\" with address " << bref
  87. << " and type " << typeid(bref).name());
  88. return new ObservedValue<T>(bname, bref);
  89. }
  90. template <typename T>
  91. Value<T*>* track_branch_ptr(const std::string& bname){
  92. TBranch* branch = fChain->GetBranch(bname.c_str());
  93. if (branch == nullptr){
  94. CRITICAL("Branch: " << bname << " does not exist in input tree.", -1);
  95. }
  96. T* bref = (T*) branch->GetAddress();
  97. fChain->SetBranchStatus(bname.c_str(), true);
  98. INFO("Registering pointer branch \"" << bname
  99. << "\" with address " << bref
  100. << " and type " << typeid(bref).name());
  101. return new PointerValue<T>(bname, bref);
  102. }
  103. template <typename T>
  104. decltype(auto) track_branch_vec(const std::string& size_bname, const std::string& value_bname){
  105. track_branch_ptr<T>(value_bname);
  106. return wrapper_vector<T>(lookup<int>(size_bname), lookup<T*>(value_bname), value_bname);
  107. }
  108. void save_all(){
  109. output_file->cd();
  110. for(auto container : containers){
  111. container.second->save_as("outfile", SaveOption::ROOT);
  112. }
  113. // Save the value names for each container to enable looking up
  114. // what values are plotted
  115. std::map<string,string> value_lookup = this->get_container_name_value_map();
  116. gDirectory->WriteObjectAny(&value_lookup, "std::map<std::string,std::string>", "_value_lookup");
  117. std::map<string,string> fn_impl_lookup = this->get_function_name_impl_map();
  118. gDirectory->WriteObjectAny(&fn_impl_lookup, "std::map<std::string,std::string>", "_function_impl_lookup");
  119. }
  120. };
  121. #endif // minitreedataset_h