TTTT Analysis  0.1
MiniTreeDataSet.hpp
Go to the documentation of this file.
1 
31 #ifndef minitreedataset_h
32 #define minitreedataset_h
33 
34 #include <string>
35 #include <tuple>
36 
37 #include "filval/filval.hpp"
38 #include "filval_root/filval_root.hpp"
39 #include "MiniTree.hpp"
40 
41 using namespace std;
42 using namespace fv;
43 using namespace fv::root;
44 
45 class MiniTreeDataSet : public DataSet,
46  public MiniTree{
47  private:
48  std::string input_filename;
49  std::string output_filename;
50  TFile* input_file;
51  TFile* output_file;
52  long next_entry;
53  long nentries;
54  bool load_next(){
55  if (next_entry >= nentries) return false;
56  fChain->GetEntry(next_entry);
57  ++next_entry;
58  return true;
59  }
60  int get_events(){
61  return nentries;
62  }
63  int get_current_event(){
64  return next_entry-1;
65  }
66 
67  public:
68  MiniTreeDataSet(const std::string& input_filename, const std::string output_filename)
69  :input_filename(input_filename),
70  output_filename(output_filename),
71  next_entry(0) {
72  input_file = TFile::Open(input_filename.c_str());
73  Init((TTree*) input_file->Get("tree"));
74  nentries = fChain->GetEntriesFast();
75  output_file = TFile::Open(output_filename.c_str(), "RECREATE");
76  }
77 
78  ~MiniTreeDataSet(){
79  input_file->Close();
80  output_file->Close();
81  }
82 
83  template <typename T>
84  Value<T>* track_branch(const std::string& bname){
85  TBranch* branch = fChain->GetBranch(bname.c_str());
86  if (branch == nullptr){
87  CRITICAL("Branch: " << bname << " does not exist in input tree.", -1);
88  }
89  T* bref = (T*) branch->GetAddress();
90  fChain->SetBranchStatus(bname.c_str(), true);
91  INFO("Registering branch \"" << bname
92  << "\" with address " << bref
93  << " and type " << typeid(bref).name());
94  return new ObservedValue<T>(bname, bref);
95  }
96 
97  template <typename T>
98  Value<T*>* track_branch_ptr(const std::string& bname){
99  TBranch* branch = fChain->GetBranch(bname.c_str());
100  if (branch == nullptr){
101  CRITICAL("Branch: " << bname << " does not exist in input tree.", -1);
102  }
103  T* bref = (T*) branch->GetAddress();
104  fChain->SetBranchStatus(bname.c_str(), true);
105  INFO("Registering pointer branch \"" << bname
106  << "\" with address " << bref
107  << " and type " << typeid(bref).name());
108  return new PointerValue<T>(bname, bref);
109  }
110 
111  template <typename T>
112  WrapperVector<T>* track_branch_vec(const std::string& size_bname, const std::string& bname){
113  track_branch_ptr<T>(bname);
114  return new WrapperVector<T>(lookup<int>(size_bname), lookup<T*>(bname), bname);
115  }
116 
117  void save_all(){
118  output_file->cd();
119  for(auto container : containers){
120  container.second->save_as("outfile", SaveOption::ROOT);
121  }
122  }
123 };
124 #endif // minitreedataset_h
A std::vector wrapper around a C-style array.
Definition: value.hpp:446
A value supplied by the dataset, not derived.
Definition: value.hpp:372
The namespace containing all filval classes and functions.
Definition: api.hpp:6
A generic value.
Definition: value.hpp:352
Definition: api.hpp:8
A Value of a pointer.
Definition: value.hpp:923