FairRoot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SimpleTreeReader.h
Go to the documentation of this file.
1 /*
2  * File: SimpleTreeReader.h
3  * Author: winckler
4  *
5  * Created on November 25, 2014, 11:17 AM
6  */
7 
8 #ifndef SIMPLEROOTSAMPLER_H
9 #define SIMPLEROOTSAMPLER_H
10 
11 // std
12 #include <functional>
13 #include <stdint.h>
14 #include <string>
15 #include <vector>
16 
17 // ROOT
18 #include <Rtypes.h>
19 #include <TClonesArray.h>
20 #include <TFile.h>
21 #include <TTree.h>
22 
23 // FairRoot
24 #include <FairMQLogger.h>
25 #include <FairMQMessage.h>
26 
27 template<typename DataType>
29 {
30  public:
32  : fSendHeader()
33  , fGetSocketNumber()
34  , fGetCurrentIndex()
35  , fInput(nullptr)
36  , fFileName("")
37  , fTreeName("")
38  , fBranchName("")
39  , fInputFile(nullptr)
40  , fTree(nullptr)
41  , fIndex(0)
42  , fIndexMax(0)
43  {}
44 
47 
49  {
50  if (fInputFile) {
51  fInputFile->Close();
52  delete fInputFile;
53  }
54  }
55 
56  void SetFileProperties(const std::string& filename, const std::string& treename, const std::string& branchname)
57  {
58  fFileName = filename;
59  fTreeName = treename;
60  fBranchName = branchname;
61  }
62 
63  // template < std::enable_if<std::is_base_of<TObject, DataType>::value,int> = 0>
64  void InitSource()
65  {
66  fInputFile = TFile::Open(fFileName.c_str(), "READ");
67  if (fInputFile) {
68  fTree = static_cast<TTree*>(fInputFile->Get(fTreeName.c_str()));
69  if (fTree) {
70  fTree->SetBranchAddress(fBranchName.c_str(), &fInput);
71  fIndexMax = fTree->GetEntries();
72  } else {
73  LOG(error) << "Could not find tree " << fTreeName;
74  }
75  } else {
76  LOG(error) << "Could not open file " << fFileName << " in SimpleTreeReader::InitSource()";
77  }
78  }
79 
81  {
82  fSendHeader(0); // callback that does the zmq multipart AND increment the current index (Event number) in
83  // generic sampler
84  }
85 
86  void SetIndex(int64_t Event) { fIndex = Event; }
87 
88  DataType* GetOutData() { return GetOutData(fIndex); }
89 
90  DataType* GetOutData(int64_t Event)
91  {
92  fTree->GetEntry(Event);
93  return fInput;
94  }
95  void GetOutData(DataType*& data, int64_t Event)
96  {
97  fTree->GetEntry(Event);
98  data = fInput;
99  }
100  void deserialize_impl(DataType*& data, int64_t Event)
101  {
102  /*required for MQ*/
103  fTree->GetEntry(Event);
104  data = fInput;
105  }
106  void deserialize_impl(int64_t Event)
107  {
108  /*required for MQ*/
109  fTree->GetEntry(Event);
110  }
111 
113  {
114  if (fTree) {
115  return fIndexMax;
116  } else {
117  return 0;
118  }
119  }
120 
121  template<typename T>
122  std::vector<std::vector<T>> GetDataVector()
123  {
124  std::vector<std::vector<T>> allObj;
125  std::vector<T> tempObj;
126 
127  if (std::is_same<DataType, TClonesArray>::value) {
128  for (int64_t i = 0; i < fTree->GetEntries(); i++) {
129  tempObj.clear();
130  fTree->GetEntry(i);
131  for (int64_t iobj = 0; iobj < fInput->GetEntriesFast(); ++iobj) {
132  T* data = reinterpret_cast<T*>(fInput->At(iobj));
133  if (!data) {
134  continue;
135  }
136  tempObj.push_back(*data);
137  }
138  allObj.push_back(tempObj);
139  }
140  } else {
141  for (int64_t i = 0; i < fTree->GetEntries(); i++) {
142  tempObj.clear();
143  fTree->GetEntry(i);
144  T data = *fInput;
145  tempObj.push_back(data);
146  allObj.push_back(tempObj);
147  }
148  }
149  return allObj;
150  }
151 
152  // provides a callback to the Sampler.
153  void BindSendHeader(std::function<void(int)> callback) { fSendHeader = callback; }
154 
155  void BindGetSocketNumber(std::function<int()> callback) { fGetSocketNumber = callback; }
156 
157  void BindGetCurrentIndex(std::function<int()> callback) { fGetCurrentIndex = callback; }
158 
159  protected:
160  DataType* fInput; // data type of the branch you want to extract
161 
162  private:
163  std::function<void(int)> fSendHeader; // function pointer for the Sampler callback.
164  std::function<int()> fGetSocketNumber; // function pointer for the Sampler callback.
165  std::function<int()> fGetCurrentIndex; // function pointer for the Sampler callback.
166 
167  std::string fFileName;
168  std::string fTreeName;
169  std::string fBranchName;
170  TFile* fInputFile;
171  TTree* fTree;
172  int64_t fIndex;
173  int64_t fIndexMax;
174 };
175 
176 template<typename T>
178 
179 #endif /* SIMPLEROOTSAMPLER_H */
BaseSimpleTreeReader operator=(const BaseSimpleTreeReader &)=delete
void GetOutData(DataType *&data, int64_t Event)
void SetIndex(int64_t Event)
void deserialize_impl(int64_t Event)
void BindSendHeader(std::function< void(int)> callback)
void deserialize_impl(DataType *&data, int64_t Event)
virtual ~BaseSimpleTreeReader()
std::vector< std::vector< T > > GetDataVector()
void BindGetSocketNumber(std::function< int()> callback)
void SetFileProperties(const std::string &filename, const std::string &treename, const std::string &branchname)
void BindGetCurrentIndex(std::function< int()> callback)
DataType * GetOutData(int64_t Event)