MiniTreeDataSet.hpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #ifndef minitreedataset_h
  2. #define minitreedataset_h
  3. #include <string>
  4. #include <tuple>
  5. #include "filval/filval.hpp"
  6. #include "filval_root/filval_root.hpp"
  7. #include "MiniTree.hpp"
  8. using namespace std;
  9. using namespace fv;
  10. using namespace fv::root;
  11. class MiniTreeDataSet : public DataSet,
  12. public MiniTree{
  13. private:
  14. std::string input_filename;
  15. std::string output_filename;
  16. TFile* input_file;
  17. TFile* output_file;
  18. long next_entry;
  19. long nentries;
  20. bool load_next(){
  21. if (next_entry >= nentries) return false;
  22. fChain->GetEntry(next_entry);
  23. ++next_entry;
  24. return true;
  25. }
  26. int get_events(){
  27. return nentries;
  28. }
  29. int get_current_event(){
  30. return next_entry-1;
  31. }
  32. public:
  33. MiniTreeDataSet(const std::string& input_filename, const std::string output_filename)
  34. :input_filename(input_filename),
  35. output_filename(output_filename),
  36. next_entry(0) {
  37. input_file = TFile::Open(input_filename.c_str());
  38. Init((TTree*) input_file->Get("tree"));
  39. nentries = fChain->GetEntriesFast();
  40. output_file = TFile::Open(output_filename.c_str(), "UPDATE");
  41. }
  42. ~MiniTreeDataSet(){
  43. input_file->Close();
  44. output_file->Close();
  45. }
  46. template <typename T>
  47. Value<T>* track_branch(const std::string& bname){
  48. TBranch* branch = fChain->GetBranch(bname.c_str());
  49. if (branch == nullptr){
  50. CRITICAL("Branch: " << bname << " does not exist in input tree.", -1);
  51. }
  52. T* bref = (T*) branch->GetAddress();
  53. fChain->SetBranchStatus(bname.c_str(), true);
  54. INFO("Registering branch \"" << bname
  55. << "\" with address " << bref
  56. << " and type " << typeid(bref).name());
  57. return new ObservedValue<T>(bname, bref);
  58. }
  59. template <typename T>
  60. Value<T*>* track_branch_ptr(const std::string& bname){
  61. TBranch* branch = fChain->GetBranch(bname.c_str());
  62. if (branch == nullptr){
  63. CRITICAL("Branch: " << bname << " does not exist in input tree.", -1);
  64. }
  65. T* bref = (T*) branch->GetAddress();
  66. fChain->SetBranchStatus(bname.c_str(), true);
  67. INFO("Registering pointer branch \"" << bname
  68. << "\" with address " << bref
  69. << " and type " << typeid(bref).name());
  70. return new PointerValue<T>(bname, bref);
  71. }
  72. template <typename T>
  73. WrapperVector<T>* track_branch_vec(const std::string& size_bname, const std::string& bname){
  74. track_branch_ptr<T>(bname);
  75. return new WrapperVector<T>(lookup<int>(size_bname), lookup<T*>(bname), bname);
  76. }
  77. void save_all(){
  78. output_file->cd();
  79. for(auto container : containers){
  80. container.second->save_as("outfile", SaveOption::ROOT);
  81. }
  82. }
  83. };
  84. #endif // minitreedataset_h