FairRoot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Ex1Sampler.h
Go to the documentation of this file.
1 #ifndef EX1SAMPLER_H
2 #define EX1SAMPLER_H
3 
4 #include "MyDigi.h"
5 #include "RootSerializer.h"
6 
7 #include <FairMQDevice.h>
8 #include <Rtypes.h>
9 #include <TClonesArray.h>
10 #include <TFile.h>
11 #include <TTree.h>
12 
13 class Ex1Sampler : public FairMQDevice
14 {
15  public:
17  : fInput(nullptr)
18  , fTree(nullptr)
19  , fFileName()
20  , fInputFile(nullptr)
21  {}
22 
23  Ex1Sampler(const Ex1Sampler&);
25 
26  virtual ~Ex1Sampler()
27  {
28  if (fInputFile) {
29  fInputFile->Close();
30  delete fInputFile;
31  }
32  }
33 
34  protected:
35  virtual void Init()
36  {
37  fFileName = fConfig->GetValue<std::string>("input-file");
38  fInputFile = TFile::Open(fFileName.c_str(), "READ");
39  if (fInputFile) {
40  fTree = static_cast<TTree*>(fInputFile->Get("cbmsim"));
41  if (fTree) {
42  fTree->SetBranchAddress("MyDigi", &fInput);
43  } else {
44  LOG(error) << "Could not find tree 'MyDigi'";
45  }
46  } else {
47  LOG(error) << "Could not open file " << fFileName << " in SimpleTreeReader::InitSource()";
48  }
49  }
50 
51  virtual void Run()
52  {
53  uint64_t sentMsgs = 0;
54  const uint64_t numEvents = fTree->GetEntries();
55  LOG(info) << "Number of events to process: " << numEvents;
56 
57  for (uint64_t i = 0; i < numEvents; i++) {
58  FairMQMessagePtr msg(NewMessage());
59  fTree->GetEntry(i);
60  Serialize<RootSerializer>(*msg, fInput);
61  Send(msg, "data1");
62  sentMsgs++;
63  if (NewStatePending()) {
64  break;
65  }
66  }
67 
68  LOG(info) << "Sent " << sentMsgs << " messages!";
69  }
70 
71  private:
72  TClonesArray* fInput;
73  TTree* fTree;
74  std::string fFileName;
75  TFile* fInputFile;
76 };
77 
78 #endif // EX1SAMPLER_H
Ex1Sampler & operator=(const Ex1Sampler &)
virtual ~Ex1Sampler()
Definition: Ex1Sampler.h:26
virtual void Run()
Definition: Ex1Sampler.h:51
virtual void Init()
Definition: Ex1Sampler.h:35