FairRoot/PandaRoot
PndMvdMQFileSink.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 version 3 (LGPL) version 3, *
6  * copied verbatim in the file "LICENSE" *
7  ********************************************************************************/
15 #include <PndMvdMQFileSink.h>
16 
17 #include <boost/thread.hpp>
18 #include <boost/bind.hpp>
19 
20 #include "FairMQLogger.h"
21 
22 #include "TMessage.h"
23 
24 using namespace std;
25 
26 // special class to expose protected TMessage constructor
27 class Ex9TMessage : public TMessage
28 {
29  public:
30  Ex9TMessage(void* buf, Int_t len)
31  : TMessage(buf, len)
32  {
33  ResetBit(kIsOwner);
34  }
35 };
36 
38  : FairMQDevice()
39  , fFileName()
40  , fTreeName()
41 
42  , fBranchNames()
43  , fClassNames()
44  , fFileOption()
45  , fFlowMode(false)
46  , fWrite(false)
47 
48  , fOutFile(NULL)
49  , fTree(NULL)
50  , fNObjects(0)
51  , fOutputObjects(new TObject*[1000])
52  , fFolder(NULL)
53 {
54 }
55 
57 {
58  LOG(INFO) << "SHOULD CREATE THE FILE AND TREE";
59  // fFileName = "/Users/karabowi/fairroot/pixel9_dev/FairRoot/examples/MQ/9-PixelDetector/macros/tmpOut.root";
60  fFileOption = "RECREATE";
61  fTreeName = "pndsim";
62 
63  // fBranchNames.push_back("EventHeader.");
64  // fClassNames .push_back("FairEventHeader");
65  // fBranchNames.push_back("PixelHits");
66  // fClassNames .push_back("TClonesArray(PixelHit)");
67 
68  fOutFile = TFile::Open(fFileName.c_str(),fFileOption.c_str());
69 
70  fTree = new TTree(fTreeName.c_str(), "/cbmout");
71 
72  fFolder = new TFolder("cbmout", "Main Output Folder");
73  TFolder* foldEventHeader = fFolder->AddFolder("EvtHeader","EvtHeader");
74  TFolder* foldPixel = fFolder->AddFolder("Pixel","Pixel");
75 
76  TList* BranchNameList = new TList();
77 
78  LOG(INFO) << "PndMvdMQFileSink: " << fBranchNames.size();
79 
80  for ( fNObjects = 0 ; fNObjects < fBranchNames.size() ; fNObjects++ ) {
81  if ( fClassNames[fNObjects].find("TClonesArray(") == 0 ) {
82  fClassNames [fNObjects] = fClassNames[fNObjects].substr(13,fClassNames[fNObjects].length()-12-2);
83  fOutputObjects [fNObjects] = new TClonesArray(fClassNames[fNObjects].c_str());
84  fTree->Branch(fBranchNames[fNObjects].c_str(),"TClonesArray", &fOutputObjects[fNObjects]);
85  foldPixel->Add(fOutputObjects[fNObjects]);
86  LOG(INFO) << fNObjects << " : " << fBranchNames[fNObjects];
87  BranchNameList->AddLast(new TObjString(fBranchNames[fNObjects].c_str()));
88  }
89  else if ( fClassNames[fNObjects].find("FairEventHeader") == 0 ) {
90  fOutputObjects [fNObjects] = new FairEventHeader();
91  fTree->Branch(fBranchNames[fNObjects].c_str(),"FairEventHeader", &fOutputObjects[fNObjects]);
92  foldEventHeader->Add(fOutputObjects[fNObjects]);
93  BranchNameList->AddLast(new TObjString(fBranchNames[fNObjects].c_str()));
94  }
95  else {
96  LOG(ERROR) << "!!! Unknown output object \"" << fClassNames[fNObjects] << "\" !!!";
97  }
98  }
99 
100  fFolder->Write();
101  BranchNameList->Write("BranchList", TObject::kSingleKey);
102  BranchNameList->Delete();
103  delete BranchNameList;
104 
105 }
106 
108 {
109  while (CheckCurrentState(RUNNING))
110  {
111  FairMQParts parts;
112 
113  if (Receive(parts, "data-in") >= 0)
114  {
115 
116  TObject* tempObjects[10];
117  for ( int ipart = 0 ; ipart < parts.Size() ; ipart++ )
118  {
119  Ex9TMessage tm(parts.At(ipart)->GetData(), parts.At(ipart)->GetSize());
120  tempObjects[ipart] = (TObject*)tm.ReadObject(tm.GetClass());
121  for ( unsigned int ibr = 0 ; ibr < fBranchNames.size() ; ibr++ )
122  {
123  if ( strcmp(tempObjects[ipart]->GetName(),fBranchNames[ibr].c_str()) == 0 )
124  {
125  fOutputObjects[ibr] = tempObjects[ipart];
126  fTree->SetBranchAddress(fBranchNames[ibr].c_str(),&fOutputObjects[ibr]);
127  }
128  }
129  }
130  fTree->Fill();
131  }
132  else
133  {
134  LOG(INFO) << "oops!";
135  }
136  }
137 }
138 
139 void PndMvdMQFileSink::SetProperty(const int key, const std::string& value)
140 {
141  switch (key)
142  {
143  case OutputFileName :
144  SetOutputFileName(value);
145  break;
146 
147  default:
148  FairMQDevice::SetProperty(key, value);
149  break;
150  }
151 }
152 
153 void PndMvdMQFileSink::SetProperty(const int key, const int value)
154 {
155  FairMQDevice::SetProperty(key, value);
156 }
157 
158 std::string PndMvdMQFileSink::GetProperty(const int key, const std::string& default_)
159 {
160  switch (key)
161  {
162  case OutputFileName :
163  return GetOutputFileName();
164 
165  default:
166  return FairMQDevice::GetProperty(key, default_);
167  }
168 }
169 
170 int PndMvdMQFileSink::GetProperty(const int key, const int value)
171 {
172  return FairMQDevice::GetProperty(key, value);
173 }
174 
176 {
177  if (fTree)
178  {
179  fTree->Write();
180  delete fTree;
181  }
182 
183  if (fOutFile)
184  {
185  if (fOutFile->IsOpen())
186  fOutFile->Close();
187  delete fOutFile;
188  }
189 }
std::string GetOutputFileName()
std::string fFileName
void SetProperty(const int key, const std::string &value)
TString fOutFile
Definition: run_full.C:10
std::string GetProperty(const int key, const std::string &default_="")
std::string fFileOption
virtual void Init()
Ex9TMessage(void *buf, Int_t len)
TObject ** fOutputObjects
std::string fTreeName
std::vector< std::string > fBranchNames
void SetOutputFileName(std::string tempString)
virtual void Run()
return buf
unsigned int fNObjects
std::vector< std::string > fClassNames