MiniTreeDataSet.hpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. void save_all(){
  73. output_file->cd();
  74. for(auto container : containers){
  75. container.second->save_as("outfile", SaveOption::ROOT);
  76. }
  77. }
  78. };
  79. #endif // minitreedataset_h