EpIC 1.1.0
Monte Carlo generator for exclusive processes
Loading...
Searching...
No Matches
GeneratorService.h
1/*
2 * GeneratorService.h
3 *
4 * Created on: Feb 8, 2021
5 * Author: Pawel Sznajder (NCBJ)
6 */
7
8#ifndef SERVICES_GENERATORSERVICE_H_
9#define SERVICES_GENERATORSERVICE_H_
10
11#include <ElementaryUtils/logger/CustomException.h>
12#include <ElementaryUtils/string_utils/Formatter.h>
13#include <ElementaryUtils/string_utils/StringUtils.h>
14#include <partons/beans/automation/BaseObjectData.h>
15#include <ctime>
16#include <memory>
17#include <string>
18#include <chrono>
19#include <TFile.h>
20#include <TH1.h>
21#include <TH2.h>
22
23#include "../automation/MonteCarloTask.h"
24#include "../beans/containers/ExperimentalConditions.h"
25#include "../beans/containers/GeneralConfiguration.h"
26#include "../beans/containers/GenerationInformation.h"
27#include "../beans/other/EventGeneratorInterface.h"
28#include "../Epic.h"
29#include "../EpicVersion.h"
30#include "../managers/ModuleObjectFactory.h"
31#include "../modules/event_generator/EventGeneratorModule.h"
32#include "../modules/writer/WriterModule.h"
33#include "../modules/random_generator/RandomNumberGSL.h"
34
35namespace EPIC {
36
44template<class kinRange, class procModule, class kinModule,
45 class rcModule>
46class GeneratorService: public PARTONS::BaseObject,
48
49public:
53 GeneratorService(const std::string &className) :
54 PARTONS::BaseObject(className), EventGeneratorInterface(), m_pProcessModule(
56 nullptr), m_pRCModule(nullptr), m_pWriterModule(nullptr), m_histogramFile(
57 nullptr) {
61 }
62
67 PARTONS::BaseObject(other), EventGeneratorInterface(other), m_pProcessModule(
71 other.m_pWriterModule) {
72
76
79
80 if (other.m_randomNumberModule == nullptr) {
81 m_randomNumberModule = nullptr;
82 } else {
83 m_randomNumberModule = std::make_shared<RandomNumberGSL>(
85 *(std::static_pointer_cast<RandomNumberGSL>(
86 other.m_randomNumberModule))));
87 }
88
89 if (other.m_histogramFile == nullptr) {
90 m_histogramFile = nullptr;
91 } else {
92 m_histogramFile = TFile::Open(other.m_histogramFile->GetName(),
93 "RECREATE", "EpIC debugging histograms");
94 }
95 }
96
97 virtual GeneratorService *clone() const = 0;
98
103 }
104
108 void computeTask(const MonteCarloTask &task) {
109
119
121
122 run();
123
125 }
126
130 virtual void run() = 0;
131
136
137 GenerationInformation generationInformation;
138
139 generationInformation.setGeneratorName("EPIC");
140 generationInformation.setGeneratorVersion(
141 ElemUtils::Formatter() << EPIC_VERSION_MAJOR << "."
142 << EPIC_VERSION_MINOR << "." << EPIC_VERSION_PATCH);
143 generationInformation.setDescription(
145
146 generationInformation.setServiceName(getClassName());
147 generationInformation.setNEvents(m_generalConfiguration.getNEvents());
148 generationInformation.setIntegratedCrossSection(
149 m_pEventGeneratorModule->getIntegral());
150 generationInformation.setGenerationDate(getCurrentDate());
151
152 generationInformation.setInitializationTime(
153 1.E-6
154 * std::chrono::duration_cast<std::chrono::microseconds>(
156 - m_debugTimeInitialization.first).count());
157 generationInformation.setGenerationTime(
158 1.E-6
159 * std::chrono::duration_cast<std::chrono::microseconds>(
161 - m_debugTimeGeneration.first).count());
162
163 addAdditionalGenerationConfiguration(generationInformation);
164
165 m_pWriterModule->saveGenerationInformation(generationInformation);
166
167 info(__func__,
168 ElemUtils::Formatter() << "Generation information:\n"
169 << generationInformation.toString() << '\n');
170 }
171
176 GenerationInformation& generationInformation) {
177
178 std::stringstream ss;
179
181 generationInformation.addAdditionalInfo(
182 std::make_pair("lepton_polarisation", ss.str()));
183
184 ss.str("");
185 ss.clear();
188 << '|'
190 generationInformation.addAdditionalInfo(
191 std::make_pair("hadron_polarisation", ss.str()));
192 }
193
194 //********************************************************
195 //*** SETTERS AND GETTERS ********************************
196 //********************************************************
197
203 }
204
209 const GeneralConfiguration &generalConfiguration) {
210 m_generalConfiguration = generalConfiguration;
211 }
212
216 const kinRange &getKinematicRanges() const {
217 return m_kinematicRanges;
218 }
219
223 void setKinematicRanges(const kinRange &kinematicRanges) {
224 m_kinematicRanges = kinematicRanges;
225 }
226
232 }
233
238 const ExperimentalConditions &experimentalConditions) {
239 m_experimentalConditions = experimentalConditions;
240 }
241
246 procModule *getProcessModule() const {
247 return m_pProcessModule;
248 }
249
253 void setProcessModule(procModule *processModule) {
254 m_pProcessModule = processModule;
255 }
256
260 const std::shared_ptr<EventGeneratorModule> &getEventGeneratorModule() const {
262 }
263
268 const std::shared_ptr<EventGeneratorModule> &pEventGeneratorModule) {
269 m_pEventGeneratorModule = pEventGeneratorModule;
270 }
271
275 const std::shared_ptr<kinModule> &getKinematicModule() const {
276 return m_pKinematicModule;
277 }
278
283 const std::shared_ptr<kinModule> &pKinematicModule) {
284 m_pKinematicModule = pKinematicModule;
285 }
286
290 const std::shared_ptr<rcModule> &getRCModule() const {
291 return m_pRCModule;
292 }
293
297 void setRCModule(const std::shared_ptr<rcModule> &pRCModule) {
298 m_pRCModule = pRCModule;
299 }
300
304 const std::shared_ptr<WriterModule> &getWriterModule() const {
305 return m_pWriterModule;
306 }
307
311 void setWriterModule(const std::shared_ptr<WriterModule> &pWriterModule) {
312 m_pWriterModule = pWriterModule;
313 }
314
318 const std::string& getScenarioDescription() const {
320 }
321
325 void setScenarioDescription(const std::string& scenarioDescription) {
326 m_scenarioDescription = scenarioDescription;
327 }
328
332 const std::string& getScenarioDate() const {
333 return m_scenarioDate;
334 }
335
339 void setScenarioDate(const std::string& scenarioDate) {
340 m_scenarioDate = scenarioDate;
341 }
342
343protected:
344
349
351
352 info(__func__,
353 ElemUtils::Formatter() << "General configuration:\n"
354 << m_generalConfiguration.toString());
355 }
356
361 const MonteCarloTask &task) = 0;
362
367
369
370 info(__func__,
371 ElemUtils::Formatter() << "Experimental conditions:\n"
372 << m_experimentalConditions.toString());
373 }
374
378 virtual void getKinematicRangesFromTask(const MonteCarloTask &task) = 0;
379
383 virtual void getProcessModuleFromTask(const MonteCarloTask &task) = 0;
384
389
390 // check if available
391 if (ElemUtils::StringUtils::equals(
392 task.getGeneratorConfiguration().getModuleType(),
394
395 // configure
398 task.getGeneratorConfiguration().getModuleClassName());
399
400 m_pEventGeneratorModule->configure(
401 task.getGeneratorConfiguration().getParameters());
402
403 m_pEventGeneratorModule->prepareSubModules(
404 task.getGeneratorConfiguration().getSubModules());
405 } else {
406 throw ElemUtils::CustomException(getClassName(), __func__,
407 ElemUtils::Formatter() << "You have not provided any "
409 }
410
411 info(__func__,
412 ElemUtils::Formatter() << "Event generator module: "
413 << m_pEventGeneratorModule->toString());
414 }
415
419 virtual void getKinematicModuleFromTask(const MonteCarloTask &task) = 0;
420
424 virtual void getRCModuleFromTask(const MonteCarloTask &task) = 0;
425
430
431 // check if available
432 if (ElemUtils::StringUtils::equals(
433 task.getWriterConfiguration().getModuleType(),
435
436 // configure
439 task.getWriterConfiguration().getModuleClassName());
440
441 m_pWriterModule->configure(
442 task.getWriterConfiguration().getParameters());
443
444 m_pWriterModule->prepareSubModules(
445 task.getWriterConfiguration().getSubModules());
446 } else {
447 throw ElemUtils::CustomException(getClassName(), __func__,
448 ElemUtils::Formatter() << "You have not provided any "
450 }
451
452 info(__func__,
453 ElemUtils::Formatter() << "Writer module: "
454 << m_pWriterModule->toString());
455 }
456
460 virtual void isServiceWellConfigured() const {
461
462 if (m_pProcessModule == nullptr) {
463 throw ElemUtils::CustomException(getClassName(), __func__,
464 "Pointer to process module in null");
465 }
466
467 if (m_pKinematicModule == nullptr) {
468 throw ElemUtils::CustomException(getClassName(), __func__,
469 "Pointer to kineamtic module in null");
470 }
471
472 if (m_pRCModule == nullptr) {
473 throw ElemUtils::CustomException(getClassName(), __func__,
474 "Pointer to radiative correction module in null");
475 }
476
477 if (m_pEventGeneratorModule == nullptr) {
478 throw ElemUtils::CustomException(getClassName(), __func__,
479 "Pointer to event generator module in null");
480 }
481
482 if (m_pWriterModule == nullptr) {
483 throw ElemUtils::CustomException(getClassName(), __func__,
484 "Pointer to writer module in null");
485 }
486
488 throw ElemUtils::CustomException(getClassName(), __func__,
489 "Number of events to be generated is: 0");
490 }
491 }
492
496 std::string getCurrentDate() const {
497
498 time_t now = time(0);
499 return std::string(ctime(&now));
500 }
501
506 ExperimentalConditions& experimentalConditions) const {
507
508 //get
509 const NumA::Vector3D& currentHadronPolarisation =
510 experimentalConditions.getHadronPolarisation();
511
512 //check
513 if (fabs(currentHadronPolarisation.getX()) == 1.
514 || fabs(currentHadronPolarisation.getY()) == 1.
515 || fabs(currentHadronPolarisation.getZ()) == 1.)
516 return;
517
518 //generate
519 double cosThetaPol = m_randomNumberModule->diceFlat(-1., 1.);
520 double thetaPol = acos(cosThetaPol);
521 double phiPol = m_randomNumberModule->diceFlat(0., 2 * M_PI);
522
523 //set
524 experimentalConditions.setHadronPolarisation(
525 NumA::Vector3D(sin(thetaPol) * cos(phiPol),
526 sin(thetaPol) * sin(phiPol), cos(thetaPol)));
527 }
528
532 virtual void transformVariables(std::vector<double>& variables) const = 0;
533
537 virtual void transformRanges(std::vector<KinematicRange>& ranges) const = 0;
538
542 virtual double getJacobian(const std::vector<double>& variables) const = 0;
543
548
549 m_histogramFile = TFile::Open(
551 "RECREATE", "EpIC debugging histograms");
552
553 if (m_histogramFile->IsOpen()) {
554 info(__func__,
555 ElemUtils::Formatter()
556 << "Histogram file created successfully: "
558
560 } else {
561 warn(__func__,
562 ElemUtils::Formatter()
563 << "Failed to create histogram file: "
565 }
566 }
567
571 virtual void bookHistograms() {
572 }
573
578
579 if (m_histogramFile->IsOpen()) {
580
581 std::vector<TH1*>::const_iterator it;
582
583 for (it = m_histograms.begin(); it != m_histograms.end(); it++) {
584 m_histogramFile->WriteTObject(*it);
585 }
586
587 m_histogramFile->Close();
588
589 info(__func__,
590 ElemUtils::Formatter() << "Histogram file closed after saving "
591 << m_histograms.size() << " histograms");
592 }
593 }
594
596 procModule *m_pProcessModule;
597 std::shared_ptr<kinModule> m_pKinematicModule;
598 std::shared_ptr<rcModule> m_pRCModule;
599
602 std::shared_ptr<EventGeneratorModule> m_pEventGeneratorModule;
603 std::shared_ptr<WriterModule> m_pWriterModule;
604
605 std::string m_scenarioDate;
607
608 std::pair<std::chrono::steady_clock::time_point,
609 std::chrono::steady_clock::time_point> m_debugTimeInitialization;
610 std::pair<std::chrono::steady_clock::time_point,
611 std::chrono::steady_clock::time_point> m_debugTimeGeneration;
612
613 std::shared_ptr<RandomNumberModule> m_randomNumberModule;
614
616 std::vector<TH1*> m_histograms;
617};
618
619} /* namespace EPIC */
620
621#endif /* SERVICES_GENERATORSERVICE_H_ */
ModuleObjectFactory * getModuleObjectFactory() const
static Epic * getInstance()
Interface to EventGeneratorModule.
Definition: EventGeneratorInterface.h:22
static const std::string EVENT_GENERATOR_MODULE_CLASS_NAME
Class name used in parsing XML.
Definition: EventGeneratorModule.h:38
Container to store experimental conditions.
Definition: ExperimentalConditions.h:29
void setHadronPolarisation(const NumA::Vector3D &hadronPolarisation)
static ExperimentalConditions fromTask(const MonteCarloTask &task)
const NumA::Vector3D & getHadronPolarisation() const
Container to store general configuration of generator.
Definition: GeneralConfiguration.h:29
static GeneralConfiguration fromTask(const MonteCarloTask &task)
const std::string & getHistogramFilePath() const
Container to store run related information.
Definition: GenerationInformation.h:25
void setNEvents(int nEvents)
void setInitializationTime(double initializationTime)
void setGenerationTime(double generationTime)
void addAdditionalInfo(const std::pair< std::string, std::string > &additionalInfo)
void setGenerationDate(const std::string &generationDate)
void setGeneratorName(const std::string &generatorName)
void setServiceName(const std::string &serviceName)
void setGeneratorVersion(const std::string &generatorVersion)
void setDescription(const std::string &description)
void setIntegratedCrossSection(const std::pair< double, double > &integratedCrossSection)
Template for services handling generation of MC events.
Definition: GeneratorService.h:47
virtual void addAdditionalGenerationConfiguration(GenerationInformation &generationInformation)
Definition: GeneratorService.h:175
const std::shared_ptr< rcModule > & getRCModule() const
Definition: GeneratorService.h:290
virtual void run()=0
std::pair< std::chrono::steady_clock::time_point, std::chrono::steady_clock::time_point > m_debugTimeGeneration
Timer spent for generation.
Definition: GeneratorService.h:611
const std::shared_ptr< kinModule > & getKinematicModule() const
Definition: GeneratorService.h:275
void setProcessModule(procModule *processModule)
Definition: GeneratorService.h:253
GeneralConfiguration m_generalConfiguration
General configuration.
Definition: GeneratorService.h:600
virtual void transformRanges(std::vector< KinematicRange > &ranges) const =0
void setScenarioDate(const std::string &scenarioDate)
Definition: GeneratorService.h:339
virtual void bookHistograms()
Definition: GeneratorService.h:571
void setRCModule(const std::shared_ptr< rcModule > &pRCModule)
Definition: GeneratorService.h:297
GeneratorService(const GeneratorService &other)
Definition: GeneratorService.h:66
const std::string & getScenarioDate() const
Definition: GeneratorService.h:332
virtual ~GeneratorService()
Definition: GeneratorService.h:102
GeneratorService(const std::string &className)
Definition: GeneratorService.h:53
std::shared_ptr< WriterModule > m_pWriterModule
Writer module.
Definition: GeneratorService.h:603
void getWriterModuleFromTask(const MonteCarloTask &task)
Definition: GeneratorService.h:429
void openHistogramFile()
Definition: GeneratorService.h:547
void getGeneralConfigurationFromTask(const MonteCarloTask &task)
Definition: GeneratorService.h:348
procModule * getProcessModule() const
Definition: GeneratorService.h:246
const std::shared_ptr< WriterModule > & getWriterModule() const
Definition: GeneratorService.h:304
TFile * m_histogramFile
File containing histograms.
Definition: GeneratorService.h:615
void computeTask(const MonteCarloTask &task)
Definition: GeneratorService.h:108
kinRange m_kinematicRanges
Kinematic ranges.
Definition: GeneratorService.h:595
virtual void isServiceWellConfigured() const
Definition: GeneratorService.h:460
void setGeneralConfiguration(const GeneralConfiguration &generalConfiguration)
Definition: GeneratorService.h:208
const std::shared_ptr< EventGeneratorModule > & getEventGeneratorModule() const
Definition: GeneratorService.h:260
void getExperimentalConditionsFromTask(const MonteCarloTask &task)
Definition: GeneratorService.h:366
std::string getCurrentDate() const
Definition: GeneratorService.h:496
virtual void getProcessModuleFromTask(const MonteCarloTask &task)=0
std::string m_scenarioDescription
Scenario description.
Definition: GeneratorService.h:606
std::shared_ptr< EventGeneratorModule > m_pEventGeneratorModule
Event generator module.
Definition: GeneratorService.h:602
procModule * m_pProcessModule
Process module.
Definition: GeneratorService.h:596
void setScenarioDescription(const std::string &scenarioDescription)
Definition: GeneratorService.h:325
virtual void getKinematicRangesFromTask(const MonteCarloTask &task)=0
virtual double getJacobian(const std::vector< double > &variables) const =0
void setKinematicRanges(const kinRange &kinematicRanges)
Definition: GeneratorService.h:223
std::shared_ptr< rcModule > m_pRCModule
Radiative correction module.
Definition: GeneratorService.h:598
void printAndSaveGenerationConfiguration()
Definition: GeneratorService.h:135
ExperimentalConditions m_experimentalConditions
Experimental conditions.
Definition: GeneratorService.h:601
std::shared_ptr< kinModule > m_pKinematicModule
Kinematic module.
Definition: GeneratorService.h:597
virtual void getKinematicModuleFromTask(const MonteCarloTask &task)=0
void setExperimentalConditions(const ExperimentalConditions &experimentalConditions)
Definition: GeneratorService.h:237
virtual void getRCModuleFromTask(const MonteCarloTask &task)=0
void setKinematicModule(const std::shared_ptr< kinModule > &pKinematicModule)
Definition: GeneratorService.h:282
std::pair< std::chrono::steady_clock::time_point, std::chrono::steady_clock::time_point > m_debugTimeInitialization
Timer spent for initialization.
Definition: GeneratorService.h:609
void setWriterModule(const std::shared_ptr< WriterModule > &pWriterModule)
Definition: GeneratorService.h:311
virtual void getAdditionalGeneralConfigurationFromTask(const MonteCarloTask &task)=0
void getEventGeneratorModuleFromTask(const MonteCarloTask &task)
Definition: GeneratorService.h:388
std::string m_scenarioDate
Scenario date.
Definition: GeneratorService.h:605
const ExperimentalConditions & getExperimentalConditions() const
Definition: GeneratorService.h:230
const GeneralConfiguration & getGeneralConfiguration() const
Definition: GeneratorService.h:201
void setEventGeneratorModule(const std::shared_ptr< EventGeneratorModule > &pEventGeneratorModule)
Definition: GeneratorService.h:267
const std::string & getScenarioDescription() const
Definition: GeneratorService.h:318
void checkTargetPolarisation(ExperimentalConditions &experimentalConditions) const
Definition: GeneratorService.h:505
std::shared_ptr< RandomNumberModule > m_randomNumberModule
Random number module.
Definition: GeneratorService.h:613
const kinRange & getKinematicRanges() const
Definition: GeneratorService.h:216
virtual void transformVariables(std::vector< double > &variables) const =0
void closeHistogramFile()
Definition: GeneratorService.h:577
std::shared_ptr< RandomNumberModule > newRandomNumberModule(unsigned int classId)
std::shared_ptr< WriterModule > newWriterModule(unsigned int classId)
std::shared_ptr< EventGeneratorModule > newEventGeneratorModule(unsigned int classId)
Container to store information from xml scenario for a single task.
Definition: MonteCarloTask.h:23
const PARTONS::BaseObjectData & getWriterConfiguration() const
const PARTONS::BaseObjectData & getGeneratorConfiguration() const
Generation of random numbers based on GSL.
Definition: RandomNumberGSL.h:27
static const unsigned int classId
Definition: RandomNumberGSL.h:34
static const std::string WRITER_MODULE_CLASS_NAME
Class name used in parsing XML.
Definition: WriterModule.h:35