FairRoot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FairTimebasedDataHandlerT.h
Go to the documentation of this file.
1 /*
2  * FairTimebasedDataHandlerT.h
3  *
4  * \brief Container class which combines data with a relative time information with an absolute event time
5  *
6  *
7  * \date 30.10.2020
8  * \author Tobias Stockmanns <t.stockmanns@fz-juelich.de>
9  */
10 
11 #ifndef EVENTDISPLAY_FAIRTIMEBASEDDATAHANDLERT_H_
12 #define EVENTDISPLAY_FAIRTIMEBASEDDATAHANDLERT_H_
13 
14 #include "FairHit.h"
15 #include "FairMCPoint.h"
16 #include "TClonesArray.h"
17 #include "TGeoTrack.h"
18 
19 #include <algorithm>
20 #include <fairlogger/Logger.h>
21 #include <iostream>
22 #include <utility>
23 #include <vector>
24 
25 template<class T>
27 {
28  public:
29  FairTimebasedDataHandlerT(double holdTime = 10.0)
30  : fHoldTime(holdTime){};
32 
41  void FillTClonesArray(TClonesArray* inputArray, int evtIndex, double t0Event, double t0Current);
42  std::vector<std::pair<T*, double>>& GetData() { return fDataVector; };
46  void SetHoldTime(double time) { fHoldTime = time; };
47  void Reset();
48 
49  protected:
54  void RemoveOldTracks(double currentTime);
55 
56  private:
57  std::vector<std::pair<T*, double>> fDataVector;
58  int fLastEventInBuffer = -1;
59 
60  double fHoldTime;
61 
62  // TClonesArray* fTGeoArray;
63 };
64 
65 template<class T>
66 inline void FairTimebasedDataHandlerT<T>::FillTClonesArray(TClonesArray* inputArray,
67  int evtIndex,
68  double t0Event,
69  double t0Current)
70 {
71  RemoveOldTracks(t0Current);
72  if (fLastEventInBuffer != evtIndex) {
73  LOG(debug) << "Fill TClonesArray: CurrentTime " << t0Current << " Event " << evtIndex << " time: " << t0Event;
74  fLastEventInBuffer = evtIndex;
75  for (int i = 0; i < inputArray->GetEntriesFast(); i++) {
76  T* data = static_cast<T*>(inputArray->At(i)->Clone());
77  fDataVector.push_back(std::make_pair(data, t0Event)); //< not clear if a pointer or an object is better
78  }
79  }
80 }
81 
82 template<class T>
84 {
85  std::for_each(
86  fDataVector.begin(), fDataVector.end(), [](std::pair<T*, double> const& track) { delete (track.first); });
87  fDataVector.clear();
88  fLastEventInBuffer = -1;
89 }
90 
91 template<class T>
92 inline void FairTimebasedDataHandlerT<T>::RemoveOldTracks(double currentTime)
93 {
94  if (fDataVector.size() > 0) {
95  fDataVector.erase(std::remove_if(fDataVector.begin(),
96  fDataVector.end(),
97  [&](std::pair<T*, double> const& track) {
98  double t = track.first->GetTime(); // relative time of hit in ns
99  if ((t + track.second) < (currentTime - fHoldTime)) {
100  delete (track.first);
101  return true;
102  } else
103  return false;
104  }),
105  fDataVector.end());
106  }
107 }
108 
109 template<>
111 {
112  if (fDataVector.size() > 0) {
113  fDataVector.erase(std::remove_if(fDataVector.begin(),
114  fDataVector.end(),
115  [&](std::pair<TGeoTrack*, double> const& track) {
116  Double_t x, y, z, t;
117  track.first->GetLastPoint(x, y, z, t);
118  if (((t * 1e9) + track.second) < (currentTime - fHoldTime)) {
119  delete (track.first);
120  return true;
121  } else
122  return false;
123  }),
124  fDataVector.end());
125  }
126 }
127 
128 template<>
130 {
131  if (fDataVector.size() > 0) {
132  fDataVector.erase(std::remove_if(fDataVector.begin(),
133  fDataVector.end(),
134  [&](std::pair<FairMCPoint*, double> const& track) {
135  Double_t t = track.first->GetTime();
136  if (((t) + track.second) < (currentTime - fHoldTime)) {
137  delete (track.first);
138  return true;
139  } else
140  return false;
141  }),
142  fDataVector.end());
143  }
144 }
145 
146 template<>
148 {
149  if (fDataVector.size() > 0) {
150  fDataVector.erase(std::remove_if(fDataVector.begin(),
151  fDataVector.end(),
152  [&](std::pair<FairHit*, double> const& track) {
153  Double_t t = track.first->GetTimeStamp();
154  if (((t) + track.second) < (currentTime - fHoldTime)) {
155  delete (track.first);
156  return true;
157  } else
158  return false;
159  }),
160  fDataVector.end());
161  }
162 }
163 
164 #endif /* EVENTDISPLAY_FAIRGEOTRACKHANDLER_H_ */
std::vector< std::pair< T *, double > > & GetData()
FairTimebasedDataHandlerT(double holdTime=10.0)
void FillTClonesArray(TClonesArray *inputArray, int evtIndex, double t0Event, double t0Current)
void RemoveOldTracks(double currentTime)