HerdSoftware  0.3.2
GGSDataProvider.h
Go to the documentation of this file.
1 /*
2  * GGSDataProvider.h
3  *
4  * Created on: 14 Nov 2018
5  * Author: Nicola Mori
6  */
7 
10 #ifndef HERD_GGSDATAPROVIDER_H_
11 #define HERD_GGSDATAPROVIDER_H_
12 
13 // HerdSoftware headers
15 #include "dataobjects/CaloHits.h"
21 #include "dataobjects/MCTruth.h"
31 
32 // HS readers
40 
46 
47 #ifndef GGSSMARTLOG_H_
48 #define HERD_GGSDATAPROVIDER_H_UNDEF_LOGGING
49 #endif
50 
51 // GGS headers
52 #include "montecarlo/dataobjs/GGSTHits.h"
53 #include "montecarlo/readers/GGSTRootReader.h"
54 
55 class GGSTHitsReader;
56 class GGSTMCTruthReader;
57 class GGSTHadrIntReader;
58 class GGSTPrimaryDisReader;
59 
60 #ifdef HERD_GGSDATAPROVIDER_H_UNDEF_LOGGING
61 // Remove the definitions of GGS output macros (would conflict with the same macros defined in EventAnalysis)
62 // We only do this if GGSSmartLog.h is being parsed in this TU, otherwise there would be nothing to undef
63 // (or we could even be undefining the EA macros)
64 #undef COUT
65 #undef CCOUT
66 #undef ENDL
67 #endif
68 
69 // EventAnalysis headers
70 #include "data/DataProvider.h"
71 
72 // ROOT headers
73 #include "TGeoManager.h"
74 
75 // Standard C++ hedaders
76 #include <functional>
77 #include <vector>
78 
79 using namespace EA;
80 
81 namespace Herd {
82 // clang-format off
123 // clang-format on
124 
125 class GGSDataProvider : public DataProvider {
126 public:
133  GGSDataProvider(const std::string &name, const std::string &source);
134 
140  bool Connect() override;
141 
142  // avoid other overloads from base class to be hidden by derived version
143  using DataProvider::SetCurrentEvent;
144 
150  bool SetCurrentEvent(unsigned int event) override;
151 
152  // avoid other overloads from base class to be hidden by derived version
153  using DataProvider::GetObject;
154 
165  RetrievalResult GetObject(const std::string &name, ObjectCategory category, ObjectWrapper &wrapper,
166  std::string &actualName) override;
167 
189  std::vector<std::string> FreeObjects(const std::vector<std::string> &objs, Memory::Status memStatus) override;
190 
191 private:
192  unsigned int m_currEv;
193  unsigned int m_currReadEv;
194  // Utility class that wraps a std::function<bool()> and adds automatic caching,
195  // for actually calling a function only once.
197  public:
199  template <typename T> CachedFunction(T func) : function{func} {}
200  template <typename T> CachedFunction &operator=(T func) {
201  function = func;
202  return *this;
203  }
204  bool operator()() {
205  if (called) {
206  return result;
207  }
208  called = true;
209  return (result = function());
210  }
211 
212  private:
213  std::function<bool()> function;
214  bool called = false;
215  bool result = true;
216  };
217  // generic struct to describe an object
218  struct ObjDescriptor {
219  std::string name;
220  std::vector<std::string> aliases;
221  ObjectCategory category;
222  // these closures should capture "this" so they can see everything in the dataprovider
224  std::function<RetrievalResult(ObjectWrapper &)> get;
225  std::function<void()> post_load;
226  // a list of objects this object depends on
227  std::vector<ObjDescriptor> prerequisites;
228  };
229 
230  std::vector<ObjDescriptor> m_objDescriptors = {};
231 
232  bool LoadPrerequisites(ObjDescriptor &objDesc);
233 
234  // GGS
235  std::shared_ptr<GGSTRootReader> m_reader;
236 
237  // HS readers
238  std::unique_ptr<Readers::GGSMCGenReader> m_mcGenReader = nullptr;
239  std::unique_ptr<Readers::GGSTGeoReader> m_mcTGeoReader = nullptr;
240  std::unique_ptr<Readers::ParametricGeoCssReader> m_mcPGCssReader = nullptr;
241  std::unique_ptr<Readers::ParametricGeoCaloReader> m_mcPGCaloReader = nullptr;
242  std::unique_ptr<Readers::ParametricGeoFitReader> m_mcPGFitReader = nullptr;
243  std::unique_ptr<Readers::ParametricGeoPsdReader> m_mcPGPsdReader = nullptr;
244  std::unique_ptr<Readers::ParametricGeoSiliconDetectorReader> m_mcPGStkReader = nullptr;
245  std::unique_ptr<Readers::ParametricGeoSiliconDetectorReader> m_mcPGScdReader = nullptr;
246 
247  std::unique_ptr<Readers::GGSMCTruthReader> m_mcTruthReader = nullptr;
248  std::unique_ptr<Readers::GGSCaloHitsReader> m_caloHitsReader = nullptr;
249  std::unique_ptr<Readers::GGSSiliconDetectorHitsReader> m_stkHitsReader = nullptr;
250  std::unique_ptr<Readers::GGSSiliconDetectorHitsReader> m_scdHitsReader = nullptr;
251  std::unique_ptr<Readers::GGSFitHitsReader> m_fitHitsReader = nullptr;
252  std::unique_ptr<Readers::GGSPsdHitsReader> m_psdHitsReader = nullptr;
253 };
254 
255 } // namespace Herd
256 
257 #endif /* HERD_GGSDATAPROVIDER_H_ */
CachedFunction & operator=(T func)
Definition: GGSDataProvider.h:200
CssGeoParams.h CssGeoParams class declaration.
Definition: CaloPDCalibrationAlgo.h:24
Data provider for Geant4 Monte Carlo data.
Definition: GGSDataProvider.h:125
CachedFunction(T func)
Definition: GGSDataProvider.h:199
CachedFunction is_available
Definition: GGSDataProvider.h:223
Definition: GGSDataProvider.h:218
std::vector< std::string > aliases
Definition: GGSDataProvider.h:220
CachedFunction()
Definition: GGSDataProvider.h:198
std::string name
Definition: GGSDataProvider.h:219
bool operator()()
Definition: GGSDataProvider.h:204
Definition: GGSDataProvider.h:196
ObjectCategory category
Definition: GGSDataProvider.h:221
std::function< void()> post_load
Definition: GGSDataProvider.h:225
unsigned int m_currReadEv
Definition: GGSDataProvider.h:193
std::vector< ObjDescriptor > prerequisites
Definition: GGSDataProvider.h:227
unsigned int m_currEv
Definition: GGSDataProvider.h:192
std::shared_ptr< GGSTRootReader > m_reader
Definition: GGSDataProvider.h:235