GlobalEngineUserTestService_service.cc
Go to the documentation of this file.
1 /**
2  * @file GlobalEngineUserService_service.cc
3  * @brief Test service registering its own engine (implementation file)
4  * @author Gianluca Petrillo
5  * @date March 22, 2016
6  * @see GlobalEngineUserTestService.h
7  *
8  */
9 
10 
11 // my header
13 
14 // framework libraries
20 
21 // supporting libraries
23 
24 // ROOT
25 #include "TClass.h"
26 #include "TRandom3.h"
27 
28 
29 //------------------------------------------------------------------------------
30 //--- testing::GlobalEngineUserTestService
31 //---
33  { "GlobalEngineUserServiceEngine" };
34 
37  : instanceNames (pset.get<std::vector<std::string>>("instances", {}))
38  , perEventSeeds (pset.get<bool> ("perEventSeeds", false))
39 {
40 
41  //
42  // input parameters check
43  //
44  if (instanceNames.empty()) {
45  instanceNames.push_back("GlobalEngineUserTestService");
46  mf::LogInfo("GlobalEngineUserTestService")
47  << "Using a default engine instance name: '" << instanceNames.back()
48  << "'";
49  }
50 
51  //
52  // create all our engines (uninteresting temporary seeds are set)
53  //
54  for (std::string instanceName: instanceNames) {
55  engines.emplace_back(std::make_unique<TRandom3>());
56  engines.back()->SetTitle(instanceName.c_str());
57  } // for
58 
59  //
60  // register all the engines
61  //
63  // NuRandomService::createEngine() can't be called here
64  // because it needs a EngineCreator: good
65 
66  for (auto& engine: engines) {
67 
68  // NuRandomService::registerEngine() should instead succeed;
69  // rndm::NuRandomService::TRandomSeeder is optionally declared (inline)
70  // in NuRandomService.h
71  auto seed = Seeds.registerEngine
72  (rndm::NuRandomService::TRandomSeeder(engine.get()), engine->GetTitle());
73 
74  mf::LogInfo("GlobalEngineUserTestService")
75  << "Registered my random engine "
76  << engine->IsA()->GetName() << "[" << ((void*) engine.get()) << "]"
77  << " with seed " << seed;
78 
79  } // for
80 
81  //
82  // let's check that the seeds were actually set
83  //
84  if (perEventSeeds) {
85  mf::LogWarning("GlobalEngineUserTestService")
86  << "Check of seeds on construction skipped because policy is per event.";
87  }
88  else CheckAllSeeds();
89 
90  //
91  // register callbacks
92  //
93  reg.sPreModuleConstruction.watch (this, &GlobalEngineUserTestService::preModuleConstruction );
94  reg.sPostModuleConstruction.watch(this, &GlobalEngineUserTestService::postModuleConstruction );
95  reg.sPreModuleBeginRun.watch (this, &GlobalEngineUserTestService::preModuleBeginRun );
96  reg.sPostModuleBeginRun.watch (this, &GlobalEngineUserTestService::postModuleBeginRun );
97  reg.sPreProcessEvent.watch (this, &GlobalEngineUserTestService::preProcessEvent );
98  reg.sPreModule.watch (this, &GlobalEngineUserTestService::preModule );
99  reg.sPostModule.watch (this, &GlobalEngineUserTestService::postModule );
100  reg.sPostProcessEvent.watch (this, &GlobalEngineUserTestService::postProcessEvent );
101  reg.sPreModuleEndJob.watch (this, &GlobalEngineUserTestService::preModuleEndJob );
102  reg.sPostModuleEndJob.watch (this, &GlobalEngineUserTestService::postModuleEndJob );
103 
104 } // testing::GlobalEngineUserTestService::GlobalEngineUserTestService
105 
106 
107 //------------------------------------------------------------------------------
108 
110 
112 
113  auto expectedSeed = Seeds.getGlobalCurrentSeed(engine.GetTitle());
114  auto actualSeed = engine.GetSeed();
115 
116  if (actualSeed != expectedSeed) {
117  mf::LogError("GlobalEngineUserTestService")
118  << "Engine " << engine.IsA()->GetName() << "[" << ((void*) &engine)
119  << "](\"" << engine.GetTitle() << "\") has seed " << actualSeed
120  << " (expected: " << expectedSeed << ")";
121  }
122  else {
123  mf::LogVerbatim("GlobalEngineUserTestService")
124  << "Engine " << engine.IsA()->GetName() << "[" << ((void*) &engine)
125  << "](\"" << engine.GetTitle() << "\") has seed " << actualSeed
126  << " (as expected)";
127  }
128  if (actualSeed != expectedSeed) {
130  << "Unexpected seed " << actualSeed << "!\n";
131  }
132 } // testing::GlobalEngineUserTestService::CheckSeed()
133 
134 
136  mf::LogVerbatim("GlobalEngineUserTestService")
137  << "Checking all " << engines.size() << " seeds";
138  for (auto& engine: engines) CheckSeed(*engine);
139 } // testing::GlobalEngineUserTestService::CheckAllSeeds()
140 
141 
142 //------------------------------------------------------------------------------
143 
146 {
147  if (!perEventSeeds) {
148  MF_LOG_DEBUG("GlobalEngineUserTestService")
149  << "GlobalEngineUserTestService::preModuleConstruction()";
150  CheckAllSeeds();
151  }
152 } // testing::GlobalEngineUserTestService::preModuleConstruction()
153 
154 
157 {
158  if (!perEventSeeds) {
159  MF_LOG_DEBUG("GlobalEngineUserTestService")
160  << "GlobalEngineUserTestService::postModuleConstruction()";
161  CheckAllSeeds();
162  }
163 } // testing::GlobalEngineUserTestService::postModuleConstruction()
164 
165 
168 {
169  if (!perEventSeeds) {
170  MF_LOG_DEBUG("GlobalEngineUserTestService")
171  << "GlobalEngineUserTestService::preModuleBeginRun()";
172  CheckAllSeeds();
173  }
174 } // testing::GlobalEngineUserTestService::preModuleBeginRun()
175 
176 
179 {
180  if (!perEventSeeds) {
181  MF_LOG_DEBUG("GlobalEngineUserTestService")
182  << "GlobalEngineUserTestService::postModuleBeginRun()";
183  CheckAllSeeds();
184  }
185 } // testing::GlobalEngineUserTestService::postModuleBeginRun()
186 
187 
189  MF_LOG_DEBUG("GlobalEngineUserTestService")
190  << "GlobalEngineUserTestService::preProcessEvent()";
191  CheckAllSeeds();
192 } // testing::GlobalEngineUserTestService::preProcessEvent()
193 
194 
197 {
198  MF_LOG_DEBUG("GlobalEngineUserTestService")
199  << "GlobalEngineUserTestService::preModule()";
200  CheckAllSeeds();
201 } // testing::GlobalEngineUserTestService::preModule()
202 
203 
206 {
207  if (!perEventSeeds) {
208  MF_LOG_DEBUG("GlobalEngineUserTestService")
209  << "GlobalEngineUserTestService::postModule()";
210  CheckAllSeeds();
211  }
212 } // testing::GlobalEngineUserTestService::postModule()
213 
214 
216  MF_LOG_DEBUG("GlobalEngineUserTestService")
217  << "GlobalEngineUserTestService::postProcessEvent()";
218  CheckAllSeeds();
219 } // testing::GlobalEngineUserTestService::postProcessEvent()
220 
221 
224 {
225  if (!perEventSeeds) {
226  MF_LOG_DEBUG("GlobalEngineUserTestService")
227  << "GlobalEngineUserTestService::preModuleEndJob()";
228  CheckAllSeeds();
229  }
230 } // testing::GlobalEngineUserTestService::preModuleBeginRun()
231 
232 
235 {
236  if (!perEventSeeds) {
237  MF_LOG_DEBUG("GlobalEngineUserTestService")
238  << "GlobalEngineUserTestService::postModuleEndJob()";
239  CheckAllSeeds();
240  }
241 } // testing::GlobalEngineUserTestService::preModuleBeginRun()
242 
243 
244 //------------------------------------------------------------------------------
245 
247 
248 //------------------------------------------------------------------------------
void postProcessEvent(art::Event const &, art::ScheduleContext)
MaybeLogger_< ELseverityLevel::ELsev_info, true > LogVerbatim
GlobalSignal< detail::SignalResponseType::FIFO, void(ModuleContext const &)> sPreModuleBeginRun
Test service registering its own ßs.
static const std::string GlobalInstanceName
Name used for the global engine instance.
std::string string
Definition: nybbler.cc:12
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
#define DEFINE_ART_SERVICE(svc)
Definition: ServiceMacros.h:88
GlobalSignal< detail::SignalResponseType::FIFO, void(ModuleDescription const &)> sPreModuleConstruction
GlobalSignal< detail::SignalResponseType::FIFO, void(ModuleContext const &)> sPreModule
void preModuleConstruction(art::ModuleDescription const &)
void postModuleConstruction(art::ModuleDescription const &)
MaybeLogger_< ELseverityLevel::ELsev_error, false > LogError
GlobalSignal< detail::SignalResponseType::LIFO, void(ModuleDescription const &)> sPostModuleConstruction
void preModuleEndJob(art::ModuleDescription const &)
std::vector< std::unique_ptr< TRandom > > engines
our random generator engines
void preProcessEvent(art::Event const &evt, art::ScheduleContext)
static void CheckSeed(TRandom const &engine)
Throws an exception if the seed is not expected.
T get(std::string const &key) const
Definition: ParameterSet.h:231
void postModuleEndJob(art::ModuleDescription const &)
GlobalSignal< detail::SignalResponseType::LIFO, void(ModuleContext const &)> sPostModuleBeginRun
GlobalEngineUserTestService(fhicl::ParameterSet const &pset, art::ActivityRegistry &reg)
GlobalSignal< detail::SignalResponseType::FIFO, void(Event const &, ScheduleContext)> sPreProcessEvent
GlobalSignal< detail::SignalResponseType::FIFO, void(ModuleDescription const &)> sPreModuleEndJob
void CheckAllSeeds() const
Checks all engines by CheckSeed() calls.
bool perEventSeeds
whether to skip seed check in constructor
GlobalSignal< detail::SignalResponseType::LIFO, void(Event const &, ScheduleContext)> sPostProcessEvent
Test service registering its own engine.
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
GlobalSignal< detail::SignalResponseType::LIFO, void(ModuleContext const &)> sPostModule
MaybeLogger_< ELseverityLevel::ELsev_warning, false > LogWarning
#define MF_LOG_DEBUG(id)
GlobalSignal< detail::SignalResponseType::LIFO, void(ModuleDescription const &)> sPostModuleEndJob
An art service to assist in the distribution of guaranteed unique seeds to all engines within an art ...
seed_t registerEngine(SeedMaster_t::Seeder_t seeder, std::string instance="")
Registers an existing engine with NuRandomService.