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