FairRoot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FairMQSimDevice.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 "FairMQSimDevice.h"
16 
17 #include "FairModule.h"
18 #include "FairParSet.h"
19 #include "FairPrimaryGenerator.h"
20 #include "FairRunSim.h"
21 #include "FairRuntimeDb.h"
22 
23 #include <FairMQDevice.h>
24 #include <FairMQLogger.h>
25 #include <TCollection.h>
26 #include <TList.h>
27 #include <TObjArray.h>
28 #include <cstdio> // printf
29 
30 using namespace std;
31 
33  : FairMQRunDevice()
34  , fSimDeviceId(0)
35  , fUpdateChannelName("updateChannel")
36  , fRunInitialized(false)
37  , fRunSim(nullptr)
38  , fNofEvents(1)
39  , fTransportName("TGeant3")
40  , fMaterialsFile("")
41  , fMagneticField(nullptr)
42  , fDetectorArray(nullptr)
43  , fPrimaryGenerator(nullptr)
44  , fStoreTrajFlag(false)
45  , fTaskArray(nullptr)
46  , fFirstParameter(nullptr)
47  , fSecondParameter(nullptr)
48  , fSink(nullptr)
49 {}
50 
52 {
53  fRunSim = new FairRunSim();
54 
55  fRunSim->SetSink(fSink);
56 
57  if (fFirstParameter || fSecondParameter) {
58  FairRuntimeDb* rtdb = fRunSim->GetRuntimeDb();
59  if (fFirstParameter)
60  rtdb->setFirstInput(fFirstParameter);
61  if (fSecondParameter)
62  rtdb->setSecondInput(fSecondParameter);
63  }
64 
65  fRunSim->SetName(fTransportName.data());
66  // fRunSim->SetSimulationConfig(new FairVMCConfig());
67  fRunSim->SetIsMT(kFALSE);
68 
69  if (fUserConfig.Length() > 0)
70  fRunSim->SetUserConfig(fUserConfig);
71  if (fUserCuts.Length() > 0)
72  fRunSim->SetUserCuts(fUserCuts);
73 
74  // ----- Create media -------------------------------------------------
75  fRunSim->SetMaterials(fMaterialsFile.data());
76 
77  // ----- Magnetic field -------------------------------------------
78  if (fMagneticField)
79  fRunSim->SetField(fMagneticField);
80 
81  // ----- Create geometry ----------------------------------------------
82  for (int idet = 0; idet < fDetectorArray->GetEntries(); idet++) {
83  fRunSim->AddModule((FairModule*)(fDetectorArray->At(idet)));
84  }
85 }
86 
88 {
89  // ----- Negotiate the run number -------------------------------------
90  // ----- via the fUpdateChannelName --------------------------------
91  // ----- ask the fParamMQServer ------------------------------------
92  // ----- receive the run number and sampler id ---------------------
93  std::string* askForRunNumber = new string("ReportSimDevice");
94  FairMQMessagePtr req(NewMessage(
95  const_cast<char*>(askForRunNumber->c_str()),
96  askForRunNumber->length(),
97  [](void* /*data*/, void* object) { delete static_cast<string*>(object); },
98  askForRunNumber));
99  FairMQMessagePtr rep(NewMessage());
100 
101  unsigned int runId = 0;
102  if (Send(req, fUpdateChannelName) > 0) {
103  if (Receive(rep, fUpdateChannelName) > 0) {
104  std::string repString = string(static_cast<char*>(rep->GetData()), rep->GetSize());
105  LOG(info) << " -> " << repString.data();
106  runId = stoi(repString);
107  repString = repString.substr(repString.find_first_of('_') + 1, repString.length());
108  fSimDeviceId = stoi(repString);
109  LOG(info) << "runId = " << runId << " /// fSimDeviceId = " << fSimDeviceId;
110  }
111  }
112 
113  if (fPrimaryGenerator) {
114  fPrimaryGenerator->SetEventNr(fSimDeviceId
115  * fNofEvents); // run n simulations with same run id - offset the event number
116  fRunSim->SetGenerator(fPrimaryGenerator);
117  }
118 
119  fRunSim->SetStoreTraj(fStoreTrajFlag);
120 
121  // ----- Set tasks ----------------------------------------------------
122  if (fTaskArray) {
123  for (int itask = 0; itask < fTaskArray->GetEntries(); itask++) {
124  fRunSim->AddTask((FairTask*)(fTaskArray->At(itask)));
125  }
126  }
127 
128  // ----- Initialize simulation run ------------------------------------
129  fRunSim->SetRunId(runId); // run n simulations with same run id - offset the event number
130  fRunSim->Init();
131 
132  fRunInitialized = true;
133 }
134 
136 {
137  if (!fRunInitialized) {
138  InitializeRun();
139  }
140 }
141 
143 {
144  if (fSimDeviceId == 0)
145  UpdateParameterServer();
146  fRunSim->Run(fNofEvents);
147  return false;
148 }
149 
150 void FairMQSimDevice::UpdateParameterServer()
151 {
152  FairRuntimeDb* rtdb = fRunSim->GetRuntimeDb();
153 
154  printf("FairMQSimDevice::UpdateParameterServer() (%d containers)\n", rtdb->getListOfContainers()->GetEntries());
155 
156  // send the parameters to be saved
157  TIter next(rtdb->getListOfContainers());
158  FairParSet* cont;
159  while ((cont = static_cast<FairParSet*>(next()))) {
160  std::string ridString = std::string("RUNID") + std::to_string(fRunSim->GetRunId()) + std::string("RUNID")
161  + std::string(cont->getDescription());
162  cont->setDescription(ridString.data());
163  FairMQRunDevice::SendObject(cont, fUpdateChannelName);
164  }
165 
166  printf("FairMQSimDevice::UpdateParameterServer() finished\n");
167 }
168 
170 {
171  if (NewStatePending()) {
172  fRunSim->StopMCRun();
173  }
175 }
list of container factories
Definition: FairRuntimeDb.h:24
virtual void PreRun()
void SetField(FairField *field)
Definition: FairRunSim.cxx:308
virtual void InitTask()
virtual bool ConditionalRun()
Bool_t setSecondInput(FairParIo *)
void SetRunId(UInt_t runId)
Definition: FairRun.h:102
void SetGenerator(FairPrimaryGenerator *Gen)
Definition: FairRunSim.cxx:310
void SetSink(FairSink *tempSink)
Definition: FairRun.h:84
TList * getListOfContainers()
void SendObject(TObject *obj, const std::string &chan)
void StopMCRun()
Definition: FairRunSim.h:190
void SetIsMT(Bool_t isMT)
Definition: FairRunSim.h:184
virtual void SendBranches()
virtual void SendBranches()
void SetEventNr(Int_t evtNr)
void SetUserCuts(const TString &Cuts)
Definition: FairRunSim.h:153
virtual void Init()
Definition: FairRunSim.cxx:143
FairRuntimeDb * GetRuntimeDb(void)
Definition: FairRun.h:80
void SetMaterials(const char *MatFileName)
Definition: FairRunSim.cxx:312
void SetUserConfig(const TString &Config)
Definition: FairRunSim.h:150
virtual void AddTask(FairTask *t)
Definition: FairRun.cxx:70
virtual void Run(Int_t NEvents=0, Int_t NotUsed=0)
Definition: FairRunSim.cxx:306
void AddModule(FairModule *Mod)
Definition: FairRunSim.cxx:118
Bool_t setFirstInput(FairParIo *)
void setDescription(const char *s)
Definition: FairParSet.h:80
Int_t GetRunId()
Definition: FairRun.h:97
void SetStoreTraj(Bool_t storeTraj=kTRUE)
Definition: FairRunSim.h:92