FairRoot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FairMQPixelFileSink.cxx
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 #include "FairMQPixelFileSink.h"
16 
17 #include "RootSerializer.h"
18 
19 #include <FairMQLogger.h>
20 #include <TFile.h>
21 #include <TObject.h>
22 #include <TTree.h>
23 #include <cstdlib>
24 #include <memory>
25 #include <vector>
26 
27 using namespace std;
28 
30  : FairMQDevice()
31  , fInputChannelName("data-in")
32  , fAckChannelName("")
33  , fFileName()
34  , fTreeName()
35  , fFileOption()
36  , fFlowMode(false)
37  , fWrite(false)
38  , fOutFile(nullptr)
39  , fTree(nullptr)
40  , fNObjects(0)
41  , fOutputObjects(new TObject*[1000])
42 {}
43 
45 {
46  fFileName = fConfig->GetValue<std::string>("file-name");
47  fInputChannelName = fConfig->GetValue<std::string>("in-channel");
48  fAckChannelName = fConfig->GetValue<std::string>("ack-channel");
49 
50  LOG(info) << "SHOULD CREATE THE FILE AND TREE";
51  fFileOption = "RECREATE";
52  fTreeName = "cbmsim";
53 
54  if (::getenv("DDS_SESSION_ID")) {
55  std::string DDS_SESSION_ID = ::getenv("DDS_SESSION_ID");
56  if (fFileName.length() > 5) {
57  DDS_SESSION_ID = "." + DDS_SESSION_ID + ".root";
58  fFileName.replace(fFileName.length() - 5, 5, DDS_SESSION_ID.c_str());
59  }
60  }
61 
62  fOutFile = TFile::Open(fFileName.c_str(), fFileOption.c_str());
63 
64  OnData(fInputChannelName, &FairMQPixelFileSink::StoreData);
65 }
66 
67 bool FairMQPixelFileSink::StoreData(FairMQParts& parts, int /*index*/)
68 {
69  bool creatingTree = false;
70  std::vector<TObject*> tempObjects;
71  if (!fTree) {
72  creatingTree = true;
73  fTree = new TTree(fTreeName.c_str(), "/cbmout");
74  }
75 
76  for (int ipart = 0; ipart < parts.Size(); ipart++) {
77  fOutputObjects[ipart] = nullptr;
78  Deserialize<RootSerializer>(*parts.At(ipart), fOutputObjects[ipart]);
79  tempObjects.push_back(fOutputObjects[ipart]);
80  if (creatingTree)
81  fTree->Branch(tempObjects.back()->GetName(), tempObjects.back()->ClassName(), &fOutputObjects[ipart]);
82  fTree->SetBranchAddress(tempObjects.back()->GetName(), &fOutputObjects[ipart]);
83  }
84  // LOG(INFO) << "Finished branches";
85  fTree->Fill();
86 
87  for (unsigned int ipart = 0; ipart < tempObjects.size(); ipart++) {
88  if (tempObjects[ipart]) {
89  delete tempObjects[ipart];
90  }
91  }
92  tempObjects.clear();
93 
94  if (fAckChannelName != "") {
95  unique_ptr<FairMQMessage> msg(NewMessage());
96  Send(msg, fAckChannelName);
97  }
98  return true;
99 }
100 
102 {
103  if (fTree) {
104  fTree->Write();
105  }
106 
107  if (fOutFile) {
108  if (fOutFile->IsOpen()) {
109  fOutFile->Close();
110  }
111  }
112 }
113 
115 {
116  if (fTree) {
117  delete fTree;
118  }
119 
120  if (fOutFile) {
121  delete fOutFile;
122  }
123 }
bool StoreData(FairMQParts &, int)