FairRoot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Ex1Sink.h
Go to the documentation of this file.
1 #ifndef EX1SINK_H
2 #define EX1SINK_H
3 
4 #include "MyHit.h"
5 #include "RootSerializer.h"
6 
7 #include <FairMQDevice.h>
8 #include <TClonesArray.h>
9 #include <TFile.h>
10 #include <TTree.h>
11 
12 class Ex1Sink : public FairMQDevice
13 {
14  public:
16  : fInput(nullptr)
17  , fFileName()
18  , fOutFile(nullptr)
19  , fTree(nullptr)
20  , fNumMsgs(0)
21  {}
22 
23  Ex1Sink(const Ex1Sink&);
24  Ex1Sink& operator=(const Ex1Sink&);
25 
26  virtual ~Ex1Sink()
27  {
28  if (fTree) {
29  fTree->Write("", TObject::kOverwrite);
30  delete fTree;
31  }
32 
33  if (fOutFile) {
34  if (fOutFile->IsOpen()) {
35  fOutFile->Close();
36  }
37  delete fOutFile;
38  }
39  }
40 
41  protected:
42  virtual void Init()
43  {
44  fNumMsgs = fConfig->GetValue<int>("num-msgs");
45  fFileName = fConfig->GetValue<std::string>("output-file");
46  fOutFile = TFile::Open(fFileName.c_str(), "RECREATE");
47  fInput = new TClonesArray("MyHit");
48  fTree = new TTree("SerializationEx1", "Test output");
49  fTree->Branch("MyHit", "TClonesArray", &fInput);
50  }
51 
52  virtual void Run()
53  {
54  int receivedMsgs = 0;
55  while (!NewStatePending()) {
56  FairMQMessagePtr msg(NewMessage());
57  if (Receive(msg, "data2") > 0) {
58  Deserialize<RootSerializer>(*msg, fInput);
59  receivedMsgs++;
60  fTree->SetBranchAddress("MyHit", &fInput);
61  fTree->Fill();
62 
63  if (fNumMsgs != 0) {
64  if (receivedMsgs == fNumMsgs) {
65  break;
66  }
67  }
68  }
69  }
70  LOG(info) << "Received " << receivedMsgs << " messages!";
71  }
72 
73  private:
74  TClonesArray* fInput;
75  std::string fFileName;
76  TFile* fOutFile;
77  TTree* fTree;
78  int fNumMsgs;
79 };
80 
81 #endif // EX1SINK_H
Ex1Sink & operator=(const Ex1Sink &)
virtual ~Ex1Sink()
Definition: Ex1Sink.h:26
Ex1Sink()
Definition: Ex1Sink.h:15
virtual void Init()
Definition: Ex1Sink.h:42
virtual void Run()
Definition: Ex1Sink.h:52