LDMX Software
simcore::GenieReweightProducer Class Reference

Public Member Functions

 GenieReweightProducer (const std::string &name, framework::Process &process)
 
virtual void configure (framework::config::Parameters &)
 Callback for the EventProcessor to configure itself from the given set of parameters.
 
virtual void onNewRun (const ldmx::RunHeader &runHeader)
 Callback for the EventProcessor to take any necessary action when the run being processed changes.
 
virtual void produce (framework::Event &event)
 Process the event and put new data products into it.
 
- Public Member Functions inherited from framework::Producer
 Producer (const std::string &name, Process &process)
 Class constructor.
 
virtual void process (Event &event) final
 Processing an event for a Producer is calling produce.
 
- Public Member Functions inherited from framework::EventProcessor
 DECLARE_FACTORY (EventProcessor, EventProcessor *, const std::string &, Process &)
 declare that we have a factory for this class
 
 EventProcessor (const std::string &name, Process &process)
 Class constructor.
 
virtual ~EventProcessor ()=default
 Class destructor.
 
virtual void beforeNewRun (ldmx::RunHeader &run_header)
 Callback for Producers to add parameters to the run header before conditions are initialized.
 
virtual void onFileOpen (EventFile &event_file)
 Callback for the EventProcessor to take any necessary action when a new event input ROOT file is opened.
 
virtual void onFileClose (EventFile &event_file)
 Callback for the EventProcessor to take any necessary action when a event input ROOT file is closed.
 
virtual void onProcessStart ()
 Callback for the EventProcessor to take any necessary action when the processing of events starts, such as creating histograms.
 
virtual void onProcessEnd ()
 Callback for the EventProcessor to take any necessary action when the processing of events finishes, such as calculating job-summary quantities.
 
template<class T >
const T & getCondition (const std::string &condition_name)
 Access a conditions object for the current event.
 
TDirectory * getHistoDirectory ()
 Access/create a directory in the histogram file for this event processor to create histograms and analysis tuples.
 
void setStorageHint (framework::StorageControl::Hint hint)
 Mark the current event as having the given storage control hint from this module_.
 
void setStorageHint (framework::StorageControl::Hint hint, const std::string &purposeString)
 Mark the current event as having the given storage control hint from this module and the given purpose string.
 
int getLogFrequency () const
 Get the current logging frequency from the process.
 
int getRunNumber () const
 Get the run number from the process.
 
std::string getName () const
 Get the processor name.
 
void createHistograms (const std::vector< framework::config::Parameters > &histos)
 Internal function which is used to create histograms passed from the python configuration @parma histos vector of Parameters that configure histograms to create.
 

Private Member Functions

void reinitializeGenieReweight ()
 
void reconfigureGenieReweight (size_t)
 

Static Private Member Functions

static genie::rew::EGSyst variationTypeToGenieDial (const ldmx::EventWeights::VariationType &vtype)
 

Private Attributes

std::string hepmc3_coll_name_
 
std::string hepmc3_pass_name_
 
std::string event_weights_coll_name_
 
int seed_
 
size_t n_weights_
 
std::string message_threshold_file_
 
std::string tune_
 
std::map< ldmx::EventWeights::VariationType, std::vector< double > > variation_map_
 
std::unique_ptr< genie::HepMC3Converter > hep_mc3_converter_
 
std::unique_ptr< genie::rew::GReWeight > genie_rw_
 

Additional Inherited Members

- Protected Member Functions inherited from framework::EventProcessor
void abortEvent ()
 Abort the event immediately.
 
- Protected Attributes inherited from framework::EventProcessor
HistogramPool histograms_
 helper object for making and filling histograms
 
NtupleManager & ntuple_ {NtupleManager::getInstance()}
 Manager for any ntuples.
 
logging::logger the_log_
 The logger for this EventProcessor.
 

Detailed Description

Definition at line 27 of file GenieReweightProducer.h.

Constructor & Destructor Documentation

◆ GenieReweightProducer()

simcore::GenieReweightProducer::GenieReweightProducer ( const std::string & name,
framework::Process & process )

Definition at line 29 of file GenieReweightProducer.cxx.

31 : Producer(name, process) {
32 hep_mc3_converter_ = std::make_unique<genie::HepMC3Converter>();
33 genie_rw_ = std::make_unique<genie::rew::GReWeight>();
34}
Producer(const std::string &name, Process &process)
Class constructor.
virtual void process(Event &event) final
Processing an event for a Producer is calling produce.

Member Function Documentation

◆ configure()

void simcore::GenieReweightProducer::configure ( framework::config::Parameters & parameters)
virtual

Callback for the EventProcessor to configure itself from the given set of parameters.

The parameters a processor has access to are the member variables of the python class in the sequence that has class_name equal to the EventProcessor class name.

For an example, look at MyProcessor.

Parameters
parametersParameters for configuration.

Reimplemented from framework::EventProcessor.

Definition at line 36 of file GenieReweightProducer.cxx.

36 {
37 hepmc3_coll_name_ = ps.get<std::string>("hepmc3_coll_name");
38 hepmc3_pass_name_ = ps.get<std::string>("hepmc3_pass_name");
39 event_weights_coll_name_ = ps.get<std::string>("event_weights_coll_name");
40 seed_ = ps.get<int>("seed");
41 n_weights_ = static_cast<size_t>(ps.get<int>("n_weights"));
42 auto var_types_strings = ps.get<std::vector<std::string> >("var_types");
43
44 message_threshold_file_ = ps.get<std::string>("message_threshold_file");
45
46 std::default_random_engine generator(seed_);
47 std::normal_distribution<double> normal_distribution;
48
49 for (auto const& vt_str : var_types_strings) {
50 auto vtype = ldmx::EventWeights::stringToVariationType(vt_str);
51 for (size_t i_w = 0; i_w < n_weights_; ++i_w)
52 variation_map_[vtype].push_back(normal_distribution(generator));
53 }
54}

References framework::config::Parameters::get().

◆ onNewRun()

void simcore::GenieReweightProducer::onNewRun ( const ldmx::RunHeader & run_header)
virtual

Callback for the EventProcessor to take any necessary action when the run being processed changes.

Parameters
run_headerThe RunHeader containing run information.

Reimplemented from framework::EventProcessor.

Definition at line 76 of file GenieReweightProducer.cxx.

76 {
77 const std::string genie_tune_par = "GenieTune";
78 std::string new_tune;
79 for (auto par : runHeader.getStringParameters())
80 if (par.first.size() >= genie_tune_par.size() &&
81 par.first.compare(par.first.size() - genie_tune_par.size(),
82 genie_tune_par.size(), genie_tune_par) == 0) {
83 new_tune = par.second;
84 break;
85 }
86
87 if (tune_ != new_tune) {
88 ldmx_log(debug) << "Found new tune " << new_tune << " (used to be " << tune_
89 << ")" << std::endl;
90 tune_ = new_tune;
91 reinitializeGenieReweight();
92 }
93}

References ldmx::RunHeader::getStringParameters().

◆ produce()

void simcore::GenieReweightProducer::produce ( framework::Event & event)
virtual

Process the event and put new data products into it.

Parameters
eventThe Event to process.

Implements framework::Producer.

Definition at line 135 of file GenieReweightProducer.cxx.

135 {
136 // When GENIE runs as a G4VDiscreteProcess rather than a PrimaryGenerator,
137 // not every event will have an electronuclear interaction. Guard against
138 // a missing or empty HepMC3 collection.
139 if (!event.exists(hepmc3_coll_name_, hepmc3_pass_name_)) {
140 ldmx::EventWeights ev_weights(variation_map_);
141 for (size_t i_w = 0; i_w < n_weights_; ++i_w) {
142 ev_weights.addWeight(1.0);
143 }
144 event.add(event_weights_coll_name_, ev_weights);
145 return;
146 }
147
148 const auto& hepmc3_col = event.getObject<std::vector<ldmx::HepMC3GenEvent> >(
149 hepmc3_coll_name_, hepmc3_pass_name_);
150
151 if (hepmc3_col.empty()) {
152 ldmx::EventWeights ev_weights(variation_map_);
153 for (size_t i_w = 0; i_w < n_weights_; ++i_w) {
154 ev_weights.addWeight(1.0);
155 }
156 event.add(event_weights_coll_name_, ev_weights);
157 return;
158 }
159
160 // create an output weights
161 ldmx::EventWeights ev_weights(variation_map_);
162
163 for (size_t i_w = 0; i_w < n_weights_; ++i_w) {
164 double running_weight = 1;
165
166 reconfigureGenieReweight(i_w);
167
168 // setup a loop here ... but we're going to force only looping over one
169 // interaction if it exists for now.
170 for (size_t i_ev = 0; i_ev < 1; ++i_ev) {
171 auto const& hepmc3_ev = hepmc3_col.at(i_ev);
172 // fill our event data into a HepMC3GenEvent
173 HepMC3::GenEvent hepmc3_genev;
174 hepmc3_genev.read_data(hepmc3_ev);
175
176 // print it out to check it ...
177 if (i_w == 0) ldmx_log(debug) << hepmc3_genev;
178
179 // now convert to genie event record
180 auto genie_ev_record_ptr = hep_mc3_converter_->RetrieveGHEP(hepmc3_genev);
181
182 // print that out too ...
183 if (i_w == 0) ldmx_log(debug) << *genie_ev_record_ptr;
184
185 // auto this_weight = 1.0 + var_value*0.05;
186 auto this_weight = genie_rw_->CalcWeight(*genie_ev_record_ptr);
187
188 running_weight = running_weight * this_weight;
189
190 } // end loop over interactions in event
191
192 ev_weights.addWeight(running_weight);
193
194 } // end loop over weights
195
196 ldmx_log(trace) << ev_weights;
197
198 event.add(event_weights_coll_name_, ev_weights);
199}
bool exists(const std::string &name, const std::string &passName, bool unique=true) const
Check for the existence of an object or collection with the given name and pass name in the event.
Definition Event.cxx:107

References framework::Event::exists().

◆ reconfigureGenieReweight()

void simcore::GenieReweightProducer::reconfigureGenieReweight ( size_t i_w)
private

Definition at line 95 of file GenieReweightProducer.cxx.

95 {
96 auto& syst = genie_rw_->Systematics();
97 for (auto var : variation_map_) {
98 if (var.first ==
99 ldmx::EventWeights::VariationType::kGENIE_INukeTwkDial_MFP_pi)
100 syst.Set(genie::rew::GSyst::FromString("MFP_pi"), var.second[i_w]);
101 else if (var.first ==
102 ldmx::EventWeights::VariationType::kGENIE_INukeTwkDial_MFP_N)
103 syst.Set(genie::rew::GSyst::FromString("MFP_N"), var.second[i_w]);
104 else if (var.first ==
105 ldmx::EventWeights::VariationType::kGENIE_INukeTwkDial_FrCEx_pi)
106 syst.Set(genie::rew::GSyst::FromString("FrCEx_pi"), var.second[i_w]);
107 else if (var.first ==
108 ldmx::EventWeights::VariationType::kGENIE_INukeTwkDial_FrInel_pi)
109 syst.Set(genie::rew::GSyst::FromString("FrInel_pi"), var.second[i_w]);
110 else if (var.first ==
111 ldmx::EventWeights::VariationType::kGENIE_INukeTwkDial_FrAbs_pi)
112 syst.Set(genie::rew::GSyst::FromString("FrAbs_pi"), var.second[i_w]);
113 else if (var.first ==
114 ldmx::EventWeights::VariationType::kGENIE_INukeTwkDial_FrPiProd_pi)
115 syst.Set(genie::rew::GSyst::FromString("FrPiProd_pi"), var.second[i_w]);
116 else if (var.first ==
117 ldmx::EventWeights::VariationType::kGENIE_INukeTwkDial_FrCEx_N)
118 syst.Set(genie::rew::GSyst::FromString("FrCEx_N"), var.second[i_w]);
119 else if (var.first ==
120 ldmx::EventWeights::VariationType::kGENIE_INukeTwkDial_FrInel_N)
121 syst.Set(genie::rew::GSyst::FromString("FrInel_N"), var.second[i_w]);
122 else if (var.first ==
123 ldmx::EventWeights::VariationType::kGENIE_INukeTwkDial_FrAbs_N)
124 syst.Set(genie::rew::GSyst::FromString("FrAbs_N"), var.second[i_w]);
125 else if (var.first ==
126 ldmx::EventWeights::VariationType::kGENIE_INukeTwkDial_FrPiProd_N)
127 syst.Set(genie::rew::GSyst::FromString("FrPiProd_N"), var.second[i_w]);
128 else if (var.first ==
129 ldmx::EventWeights::VariationType::kGENIE_HadrNuclTwkDial_FormZone)
130 syst.Set(genie::rew::GSyst::FromString("FormZone"), var.second[i_w]);
131 }
132 genie_rw_->Reconfigure();
133}

◆ reinitializeGenieReweight()

void simcore::GenieReweightProducer::reinitializeGenieReweight ( )
private

Definition at line 56 of file GenieReweightProducer.cxx.

56 {
57 genie_rw_ = std::make_unique<genie::rew::GReWeight>();
58
59 // set message thresholds
60 genie::utils::app_init::MesgThresholds(message_threshold_file_);
61
62 genie::RunOpt::Instance()->SetTuneName(tune_);
63 if (!genie::RunOpt::Instance()->Tune()) {
64 EXCEPTION_RAISE("ConfigurationException", "No TuneId in RunOption.");
65 }
66 genie::RunOpt::Instance()->BuildTune();
67
68 genie_rw_->AdoptWghtCalc("hadro_fzone", new genie::rew::GReWeightFZone);
69 genie_rw_->AdoptWghtCalc("hadro_intranuke", new genie::rew::GReWeightINuke);
70
71 auto& syst = genie_rw_->Systematics();
72 for (auto var : variation_map_)
73 syst.Init(variationTypeToGenieDial(var.first));
74}

◆ variationTypeToGenieDial()

static genie::rew::EGSyst simcore::GenieReweightProducer::variationTypeToGenieDial ( const ldmx::EventWeights::VariationType & vtype)
inlinestaticprivate

Definition at line 78 of file GenieReweightProducer.h.

79 {
80 switch (vtype) {
81 case ldmx::EventWeights::VariationType::kGENIE_INukeTwkDial_MFP_pi:
82 return genie::rew::EGSyst::kINukeTwkDial_MFP_pi;
83 case ldmx::EventWeights::VariationType::kGENIE_INukeTwkDial_MFP_N:
84 return genie::rew::EGSyst::kINukeTwkDial_MFP_N;
85 case ldmx::EventWeights::VariationType::kGENIE_INukeTwkDial_FrCEx_pi:
86 return genie::rew::EGSyst::kINukeTwkDial_FrCEx_pi;
87 case ldmx::EventWeights::VariationType::kGENIE_INukeTwkDial_FrInel_pi:
88 return genie::rew::EGSyst::kINukeTwkDial_FrInel_pi;
89 case ldmx::EventWeights::VariationType::kGENIE_INukeTwkDial_FrAbs_pi:
90 return genie::rew::EGSyst::kINukeTwkDial_FrAbs_pi;
91 case ldmx::EventWeights::VariationType::kGENIE_INukeTwkDial_FrPiProd_pi:
92 return genie::rew::EGSyst::kINukeTwkDial_FrPiProd_pi;
93 case ldmx::EventWeights::VariationType::kGENIE_INukeTwkDial_FrCEx_N:
94 return genie::rew::EGSyst::kINukeTwkDial_FrCEx_N;
95 case ldmx::EventWeights::VariationType::kGENIE_INukeTwkDial_FrInel_N:
96 return genie::rew::EGSyst::kINukeTwkDial_FrInel_N;
97 case ldmx::EventWeights::VariationType::kGENIE_INukeTwkDial_FrAbs_N:
98 return genie::rew::EGSyst::kINukeTwkDial_FrAbs_N;
99 case ldmx::EventWeights::VariationType::kGENIE_INukeTwkDial_FrPiProd_N:
100 return genie::rew::EGSyst::kINukeTwkDial_FrPiProd_N;
101 case ldmx::EventWeights::VariationType ::kGENIE_HadrNuclTwkDial_FormZone:
102 return genie::rew::EGSyst::kHadrNuclTwkDial_FormZone;
103 default:
104 return genie::rew::EGSyst::kNullSystematic;
105 }
106 return genie::rew::EGSyst::kNullSystematic;
107 }

Member Data Documentation

◆ event_weights_coll_name_

std::string simcore::GenieReweightProducer::event_weights_coll_name_
private

Definition at line 52 of file GenieReweightProducer.h.

◆ genie_rw_

std::unique_ptr<genie::rew::GReWeight> simcore::GenieReweightProducer::genie_rw_
private

Definition at line 73 of file GenieReweightProducer.h.

◆ hep_mc3_converter_

std::unique_ptr<genie::HepMC3Converter> simcore::GenieReweightProducer::hep_mc3_converter_
private

Definition at line 71 of file GenieReweightProducer.h.

◆ hepmc3_coll_name_

std::string simcore::GenieReweightProducer::hepmc3_coll_name_
private

Definition at line 46 of file GenieReweightProducer.h.

◆ hepmc3_pass_name_

std::string simcore::GenieReweightProducer::hepmc3_pass_name_
private

Definition at line 49 of file GenieReweightProducer.h.

◆ message_threshold_file_

std::string simcore::GenieReweightProducer::message_threshold_file_
private

Definition at line 61 of file GenieReweightProducer.h.

◆ n_weights_

size_t simcore::GenieReweightProducer::n_weights_
private

Definition at line 58 of file GenieReweightProducer.h.

◆ seed_

int simcore::GenieReweightProducer::seed_
private

Definition at line 55 of file GenieReweightProducer.h.

◆ tune_

std::string simcore::GenieReweightProducer::tune_
private

Definition at line 64 of file GenieReweightProducer.h.

◆ variation_map_

std::map<ldmx::EventWeights::VariationType, std::vector<double> > simcore::GenieReweightProducer::variation_map_
private

Definition at line 68 of file GenieReweightProducer.h.


The documentation for this class was generated from the following files: