FairRoot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FairMQRunDevice.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 "FairMQRunDevice.h"
16 
17 #include "FairOnlineSink.h"
18 #include "FairRootManager.h"
19 #include "RootSerializer.h"
20 
21 #include <FairMQLogger.h>
22 #include <FairMQMessage.h>
23 #include <Rtypes.h>
24 #include <TList.h>
25 #include <TObjString.h>
26 #include <cstdio> // printf
27 
28 using namespace std;
29 
30 #include <mutex> // std::mutex
31 std::mutex mtx; // mutex for critical section
32 
33 void FairMQRunDevice::SendObject(TObject* obj, const std::string& chan)
34 {
35  FairMQMessagePtr mess(NewMessage());
36  Serialize<RootSerializer>(*mess, obj);
37 
38  FairMQMessagePtr rep(NewMessage());
39 
40  printf("sending %s", obj->GetName());
41  if (Send(mess, chan) > 0) {
42  if (Receive(rep, chan) > 0) {
43  std::string repString = string(static_cast<char*>(rep->GetData()), rep->GetSize());
44  LOG(info) << " -> " << repString.data();
45  }
46  }
47 }
48 
50 {
52  LOG(debug) << "called FairMQRunDevice::SendBranches()!!!!";
53 
54  TList* branchNameList = FairRootManager::Instance()->GetBranchNameList();
55  TObjString* ObjStr;
56 
57  for (auto& mi : fChannels) {
58  LOG(debug) << "trying channel >" << mi.first.data() << "<";
59 
60  FairMQParts parts;
61 
62  for (Int_t t = 0; t < branchNameList->GetEntries(); t++) {
63  ObjStr = static_cast<TObjString*>(branchNameList->TList::At(t));
64  LOG(debug) << " branch >" << ObjStr->GetString().Data() << "<";
65  std::string modifiedBranchName = std::string("#") + ObjStr->GetString().Data() + "#";
66  if (mi.first.find(modifiedBranchName) != std::string::npos || mi.first.find("#all#") != std::string::npos) {
67  if ((static_cast<FairOnlineSink*>(FairRootManager::Instance()->GetSink()))
68  ->IsPersistentBranchAny(ObjStr->GetString())) {
69  LOG(debug) << "Branch \"" << ObjStr->GetString() << "\" is persistent ANY";
70  if (ObjStr->GetString().CompareTo("MCTrack") == 0) {
71  TClonesArray** mcTrackArray =
73  ->GetPersistentBranchAny<TClonesArray**>(ObjStr->GetString());
74  if (mcTrackArray) {
75  (*mcTrackArray)->SetName("MCTrack");
76  LOG(debug) << "[" << FairRootManager::Instance()->GetInstanceId() << "] mcTrack "
77  << mcTrackArray << " /// *mcTrackArray " << *mcTrackArray
78  << " /// *mcTrackArray->GetName() " << (*mcTrackArray)->GetName();
79  TObject* objClone = (*mcTrackArray)->Clone();
80  LOG(debug) << "FairMQRunDevice::SendBranches() the track array has "
81  << ((TClonesArray*)(objClone))->GetEntries() << " entries.";
82  FairMQMessagePtr mess(NewMessage());
83  Serialize<RootSerializer>(*mess, objClone);
84  parts.AddPart(std::move(mess));
85  LOG(debug) << "channel >" << mi.first.data() << "< --> >" << ObjStr->GetString().Data()
86  << "<";
87  }
88  } else {
89  LOG(warning) << "FairMQRunDevice::SendBranches() hasn't got knowledge how to send any branch \""
90  << ObjStr->GetString().Data() << "\"";
91  continue;
92  }
93  } else {
94  TObject* object = FairRootManager::Instance()->GetObject(ObjStr->GetString());
95  if (object) {
96  TObject* objClone = object->Clone();
97  FairMQMessagePtr mess(NewMessage());
98  Serialize<RootSerializer>(*mess, objClone);
99  parts.AddPart(std::move(mess));
100  LOG(debug) << "channel >" << mi.first.data() << "< --> >" << ObjStr->GetString().Data() << "<";
101  } else {
102  LOG(fatal) << "Object " << ObjStr->GetString() << " NOT FOUND!!!";
103  }
104  }
105  }
106  }
107  if (parts.Size() > 0) {
108  std::unique_lock<std::mutex> lock(mtx);
109  Send(parts, mi.first.data());
110  }
111  }
112 }
static FairRootManager * Instance()
TObject * GetObject(const char *BrName)
std::mutex mtx
void SendObject(TObject *obj, const std::string &chan)
FairSink * GetSink()
TList * GetBranchNameList()
virtual void SendBranches()
Int_t GetInstanceId() const