FairRoot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FairSourceMQInterface.h
Go to the documentation of this file.
1 /********************************************************************************
2  * Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
3  * *
4  * This software is distributed under the terms of the *
5  * GNU Lesser General Public Licence (LGPL) version 3, *
6  * copied verbatim in the file "LICENSE" *
7  ********************************************************************************/
8 
9 #ifndef FAIRSOURCEMQINTERFACE_H
10 #define FAIRSOURCEMQINTERFACE_H
11 
12 #include "BaseSourcePolicy.h"
13 #include "FairFileSource.h"
14 #include "FairRunAna.h"
15 
16 #include <FairMQLogger.h>
17 #include <functional>
18 #include <type_traits>
19 
20 template<typename T, typename U>
21 using enable_if_match = typename std::enable_if<std::is_same<T, U>::value, int>::type;
22 
23 template<typename FairSourceType, typename DataType>
24 class FairSourceMQInterface : public BaseSourcePolicy<FairSourceMQInterface<FairSourceType, DataType>>
25 {
26  typedef DataType* DataType_ptr;
28 
29  public:
31  : BaseSourcePolicy<FairSourceMQInterface<FairSourceType, DataType>>()
32  , fSource(nullptr)
33  , fInput(nullptr)
34  , fIndex(0)
35  , fMaxIndex(-1)
36  , fClassName("")
37  , fBranchName("")
38  , fSourceName("")
39  , fRunAna(nullptr)
40  {}
41 
44 
46  {
47  delete fInput;
48  fInput = nullptr;
49 
50  delete fSource;
51  fSource = nullptr;
52 
53  delete fRunAna;
54  fRunAna = nullptr;
55  }
56 
57  int64_t GetNumberOfEvent() { return fMaxIndex; }
58 
59  void SetFileProperties(const std::string& filename, const std::string& branchname)
60  {
61  fSourceName = filename;
62  fBranchName = branchname;
63  }
64 
65  //______________________________________________________________________________
66  // FairFileSource
67 
68  template<typename T = FairSourceType, enable_if_match<T, FairFileSource> = 0>
69  void InitSource()
70  {
71  fRunAna = new FairRunAna();
72  fSource = new FairSourceType(fSourceName.c_str());
73  fSource->Init();
74  fSource->ActivateObject(reinterpret_cast<TObject**>(&fInput), fBranchName.c_str());
75  // fSource->ActivateObject(dynamic_cast<TObject*>(fData),fBranchName.c_str());
76  fMaxIndex = fSource->CheckMaxEventNo();
77  }
78 
79  template<typename T = FairSourceType, enable_if_match<T, FairFileSource> = 0>
80  void SetIndex(int64_t eventIdx)
81  {
82  fIndex = eventIdx;
83  }
84 
85  template<typename T = FairSourceType, enable_if_match<T, FairFileSource> = 0>
86  DataType_ptr GetOutData()
87  {
88  fSource->ReadEvent(fIndex);
89  return fInput;
90  }
91 
92  void GetOutData(DataType_ptr& data, int64_t evtIdx)
93  {
94  fSource->ReadEvent(evtIdx);
95  data = fInput;
96  }
97 
98  /*required for MQ*/
99  void deserialize_impl(int64_t evtIdx) { fSource->ReadEvent(evtIdx); }
100 
101  protected:
102  FairSourceType* fSource;
103  DataType_ptr fInput;
104  std::unique_ptr<DataType> fInput2;
105  int64_t fIndex;
106  int64_t fMaxIndex;
107  std::string fClassName;
108  std::string fBranchName;
109  std::string fSourceName;
111 };
112 
113 #endif
void GetOutData(DataType_ptr &data, int64_t evtIdx)
std::unique_ptr< DataType > fInput2
FairSourceMQInterface operator=(const FairSourceMQInterface &)=delete
typename std::enable_if< std::is_same< T, U >::value, int >::type enable_if_match
void SetFileProperties(const std::string &filename, const std::string &branchname)
void deserialize_impl(int64_t evtIdx)
void SetIndex(int64_t eventIdx)