FairRoot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FairTestDetectorFileSink.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  ********************************************************************************/
15 #ifndef FAIRTESTDETECTORFILESINK_H_
16 #define FAIRTESTDETECTORFILESINK_H_
17 
18 #include "FairTestDetectorHit.h"
20 
21 #include <FairMQDevice.h>
22 #include <FairMQLogger.h>
23 #include <Rtypes.h>
24 #include <TClonesArray.h>
25 #include <TFile.h>
26 #include <TSystem.h>
27 #include <TTree.h>
28 #include <array>
29 #include <iostream>
30 #include <memory>
31 #include <string>
32 
33 template<typename TIn, typename TPayloadIn>
34 class FairTestDetectorFileSink : public FairMQDevice
35 {
36  public:
38  : fOutput(new TClonesArray("FairTestDetectorHit"))
39  , fOutFile(nullptr)
40  , fTree("MQOut", "Test output")
41  , fReceivedMsgs(0)
42  , fInChannelName("data2")
43  , fAckChannelName("ack")
44  {
45  gSystem->ResetSignal(kSigInterrupt);
46  gSystem->ResetSignal(kSigTermination);
47  }
48 
51 
53  {
54  fTree.Write();
55  fOutFile->Close();
56  }
57 
58  virtual void InitOutputFile(const std::string& defaultId = "100")
59  {
60  std::string filename("filesink_" + defaultId + ".root");
61  fOutFile = std::unique_ptr<TFile>(TFile::Open(filename.c_str(), "recreate"));
62  fTree.Branch("Output", "TClonesArray", fOutput.get(), 64000, 99);
63  }
64 
65  protected:
66  virtual void Init()
67  {
68  fInChannelName = fConfig->GetValue<std::string>("in-channel");
69  fAckChannelName = fConfig->GetValue<std::string>("ack-channel");
70 
71  InitOutputFile(fConfig->GetValue<std::string>("data-format"));
72  }
73 
74  virtual void PostRun() { LOG(info) << "Received " << fReceivedMsgs << " messages!"; }
75 
76  virtual void InitTask();
77 
78  private:
79  std::unique_ptr<TClonesArray> fOutput;
80  std::unique_ptr<TFile> fOutFile;
81  TTree fTree;
82  int fReceivedMsgs;
83  std::string fInChannelName;
84  std::string fAckChannelName;
85 };
86 
87 // Template implementation of Run() in FairTestDetectorFileSink.tpl :
88 #include "FairTestDetectorFileSinkBin.tpl"
89 #include "FairTestDetectorFileSinkBoost.tpl"
90 #include "FairTestDetectorFileSinkFlatBuffers.tpl"
91 #include "FairTestDetectorFileSinkMsgpack.tpl"
92 #include "FairTestDetectorFileSinkProtobuf.tpl"
93 #include "FairTestDetectorFileSinkTMessage.tpl"
94 
95 #endif /* FAIRTESTDETECTORFILESINK_H_ */
FairTestDetectorFileSink operator=(const FairTestDetectorFileSink &)=delete
virtual void InitOutputFile(const std::string &defaultId="100")
virtual void InitTask()