26 #include "art/Framework/Principal/Event.h"
27 #include "cetlib_except/exception.h"
28 #include "messagefacility/MessageLogger/MessageLogger.h"
33 #include "nugen/EventGeneratorBase/GENIE/GENIE2ART.h"
35 #include "nusimdata/SimulationBase/MCFlux.h"
36 #include "nusimdata/SimulationBase/MCTruth.h"
37 #include "nusimdata/SimulationBase/GTruth.h"
39 #include "fhiclcpp/ParameterSet.h"
42 #include "GENIE/Framework/Conventions/KineVar.h"
43 #include "GENIE/Framework/EventGen/EventRecord.h"
44 #include "GENIE/Framework/Interaction/Interaction.h"
45 #include "GENIE/Framework/Interaction/Kinematics.h"
46 #include "GENIE/Framework/Messenger/Messenger.h"
47 #include "GENIE/Framework/Utils/AppInit.h"
49 #include "GENIE/RwFramework/GSystSet.h"
50 #include "GENIE/RwFramework/GSyst.h"
51 #include "GENIE/RwFramework/GReWeight.h"
52 #include "GENIE/RwCalculators/GReWeightNuXSecNCEL.h"
53 #include "GENIE/RwCalculators/GReWeightNuXSecCCQE.h"
54 #include "GENIE/RwCalculators/GReWeightNuXSecCCRES.h"
55 #include "GENIE/RwCalculators/GReWeightNuXSecCOH.h"
56 #include "GENIE/RwCalculators/GReWeightNonResonanceBkg.h"
57 #include "GENIE/RwCalculators/GReWeightFGM.h"
58 #include "GENIE/RwCalculators/GReWeightDISNuclMod.h"
59 #include "GENIE/RwCalculators/GReWeightResonanceDecay.h"
60 #include "GENIE/RwCalculators/GReWeightFZone.h"
61 #include "GENIE/RwCalculators/GReWeightINuke.h"
62 #include "GENIE/RwCalculators/GReWeightAGKY.h"
63 #include "GENIE/RwCalculators/GReWeightNuXSecCCQEaxial.h"
64 #include "GENIE/RwCalculators/GReWeightNuXSecCCQEvec.h"
65 #include "GENIE/RwCalculators/GReWeightNuXSecNCRES.h"
66 #include "GENIE/RwCalculators/GReWeightNuXSecDIS.h"
67 #include "GENIE/RwCalculators/GReWeightINukeParams.h"
68 #include "GENIE/RwCalculators/GReWeightNuXSecNC.h"
69 #include "GENIE/RwCalculators/GReWeightXSecEmpiricalMEC.h"
74 #include "GENIE/RwCalculators/GReWeightXSecMEC.h"
77 #include "GENIE/RwCalculators/GReWeightDeltaradAngle.h"
78 #include "GENIE/RwCalculators/GReWeightNuXSecCOHuB.h"
79 #include "GENIE/RwCalculators/GReWeightRESBugFix.h"
88 std::set< genie::rew::GSyst_t > UNIMPLEMENTED_GENIE_KNOBS = {
89 kXSecTwkDial_RnubarnuCC,
90 kXSecTwkDial_NormCCQEenu,
91 kXSecTwkDial_NormDISCC,
92 kXSecTwkDial_DISNuclMod
108 std::map< std::string, std::map<int, std::set<genie::rew::GSyst_t> > > INCOMPATIBLE_GENIE_KNOBS = {
111 { genie::rew::GReWeightNuXSecCCQE::kModeNormAndMaShape,
114 kXSecTwkDial_NormCCQE,
115 kXSecTwkDial_MaCCQEshape,
116 kXSecTwkDial_E0CCQEshape
119 { genie::rew::GReWeightNuXSecCCQE::kModeMa,
126 { genie::rew::GReWeightNuXSecCCQE::kModeZExp,
129 kXSecTwkDial_ZNormCCQE,
130 kXSecTwkDial_ZExpA1CCQE,
131 kXSecTwkDial_ZExpA2CCQE,
132 kXSecTwkDial_ZExpA3CCQE,
133 kXSecTwkDial_ZExpA4CCQE
140 { genie::rew::GReWeightNuXSecCCRES::kModeNormAndMaMvShape,
143 kXSecTwkDial_NormCCRES,
144 kXSecTwkDial_MaCCRESshape,
145 kXSecTwkDial_MvCCRESshape
148 { genie::rew::GReWeightNuXSecCCRES::kModeMaMv,
151 kXSecTwkDial_MaCCRES,
159 { genie::rew::GReWeightNuXSecNCRES::kModeNormAndMaMvShape,
162 kXSecTwkDial_NormNCRES,
163 kXSecTwkDial_MaNCRESshape,
164 kXSecTwkDial_MvNCRESshape
167 { genie::rew::GReWeightNuXSecNCRES::kModeMaMv,
170 kXSecTwkDial_MaNCRES,
178 { genie::rew::GReWeightNuXSecDIS::kModeABCV12u,
186 { genie::rew::GReWeightNuXSecDIS::kModeABCV12uShape,
188 kXSecTwkDial_AhtBYshape,
189 kXSecTwkDial_BhtBYshape,
190 kXSecTwkDial_CV1uBYshape,
191 kXSecTwkDial_CV2uBYshape
201 bool valid_knob_name(
const std::string& knob_name, genie::rew::GSyst_t& knob ) {
203 if ( knob != kNullSystematic && knob != kNTwkDials ) {
204 if ( UNIMPLEMENTED_GENIE_KNOBS.count(knob) ) {
205 MF_LOG_WARNING(
"GENIEWeightCalc") <<
"Ignoring unimplemented GENIE"
206 <<
" knob " << knob_name;
211 throw cet::exception(__PRETTY_FUNCTION__) <<
"Encountered unrecognized"
212 "GENIE knob \"" << knob_name <<
'\"';
222 std::set< std::string > CALC_NAMES_THAT_IGNORE_TUNED_CV = {
"RootinoFix" };
233 void Configure(fhicl::ParameterSet
const& pset,
234 CLHEP::HepRandomEngine& engine)
override;
236 std::vector<float>
GetWeight(art::Event&
e,
size_t inu)
override;
245 const std::vector<genie::rew::GSyst_t>& knob_vec);
248 const std::map<std::string, int>& modes_to_use);
257 CLHEP::HepRandomEngine& engine) {
259 genie::Messenger* messenger = genie::Messenger::Instance();
266 genie::utils::app_init::MesgThresholds(
"Messenger_whisper.xml" );
273 MF_LOG_INFO(
"GENIEWeightCalc") <<
"Configuring GENIE weight"
274 <<
" calculator " << this->
GetName();
280 messenger->SetPriorityLevel(
"TransverseEnhancementFFModel",
292 std::string genie_tune_name = p.get<std::string>(
"TuneName",
"${GENIE_XSEC_TUNE}");
297 std::string evgen_list_name = p.get<std::string>(
"EventGeneratorList",
"");
300 evgb::SetEventGeneratorListAndTune( evgen_list_name, genie_tune_name );
305 const fhicl::ParameterSet& param_CVs = p.get<fhicl::ParameterSet>(
306 "genie_central_values", fhicl::ParameterSet() );
307 std::vector< std::string > CV_knob_names = param_CVs.get_all_keys();
309 std::map< genie::rew::GSyst_t, double > gsyst_to_cv_map;
310 genie::rew::GSyst_t temp_knob;
312 if ( !CV_knob_names.empty() && !
fQuietMode ) MF_LOG_INFO(
"GENIEWeightCalc")
313 <<
"Configuring non-default GENIE knob central values from input"
314 <<
" FHiCL parameter set";
316 for (
const auto& knob_name : CV_knob_names ) {
317 double CV_knob_value = param_CVs.get<
double>( knob_name );
318 if ( valid_knob_name(knob_name, temp_knob) ) {
319 if ( gsyst_to_cv_map.count( temp_knob ) ) {
320 throw cet::exception(__PRETTY_FUNCTION__) <<
"Duplicate central values"
321 <<
" were configured for the " << knob_name <<
" GENIE knob.";
323 gsyst_to_cv_map[ temp_knob ] = CV_knob_value;
325 MF_LOG_INFO(
"GENIEWeightCalc") <<
"Central value for the " << knob_name
326 <<
" knob was set to " << CV_knob_value;
332 const fhicl::ParameterSet& pset = p.get<fhicl::ParameterSet>(
GetName());
334 auto const& pars = pset.get<std::vector<std::string> >(
"parameter_list");
336 std::vector<float> parsigmas = pset.get<std::vector<float> >(
"parameter_sigma", std::vector<float>() );
341 std::vector< genie::rew::GSyst_t > knobs_to_use;
342 for (
const auto& knob_name : pars ) {
343 if ( valid_knob_name(knob_name, temp_knob) ) knobs_to_use.push_back( temp_knob );
347 std::vector< genie::rew::GSyst_t > all_knobs_vec = knobs_to_use;
348 for (
const auto& pair : gsyst_to_cv_map ) {
349 genie::rew::GSyst_t cv_knob = pair.first;
350 auto begin = all_knobs_vec.cbegin();
351 auto end = all_knobs_vec.cend();
354 std::map< std::string, int >
358 std::string
mode = pset.get<std::string>(
"mode");
360 int num_universes = 1;
362 std::string array_name_for_exception;
364 if (pars.size() != parsigmas.size()) {
365 array_name_for_exception =
"parameter_sigma";
369 if(mode.find(
"pmNsigma") != std::string::npos){
374 for (
size_t i=0; i<pars.size(); i++){
378 }
else if(mode.find(
"multisigma") != std::string::npos){
381 array_name_for_exception =
"";
383 array_name_for_exception =
"multisigma";
384 throw cet::exception(__PRETTY_FUNCTION__) <<
GetName()
385 <<
"::Bad fcl configuration. multisigma only supports one parameter_list";
388 for (
size_t i=0; i<pars.size(); i++){
389 num_universes = pars.size() * parsigmas.size();
390 std::vector<float> withds;
391 for (
size_t j=0; j<parsigmas.size(); j++){
392 withds.push_back(parsigmas[j]);
397 }
else if(mode.find(
"multisim") != std::string::npos){
399 num_universes = pset.get<
int>(
"number_of_multisims", 1 );
401 MF_LOG_INFO(
"GENIEWeightCalc") <<
"GENIE weight calculator "
402 << this->
GetName() <<
" will generate " << num_universes
403 <<
" multisim universes";
405 for (
size_t i=0; i<pars.size(); i++) {
409 }
else if(mode.find(
"fixed") != std::string::npos){
413 for (
size_t i=0; i<pars.size(); i++) {
419 throw cet::exception(__PRETTY_FUNCTION__) <<
GetName()
420 <<
":: Mode is not recognized: "<<
mode;
424 if( array_name_for_exception.size() > 1){
426 throw cet::exception(__PRETTY_FUNCTION__) <<
GetName()
427 <<
"::Bad fcl configuration. parameter_list and "
428 << array_name_for_exception
429 <<
" need to have same number of parameters.";
438 std::cout <<
GetFullName() <<
": Initialize WeightCalculators with "<<num_universes<<
" univeres."<<std::endl;
444 for (
size_t univ = 0; univ < reweightVector.size(); ++univ ) {
446 auto& rwght = reweightVector.at( univ );
447 genie::rew::GSystSet& syst = rwght.Systematics();
455 std::string
name = it.first.fName;
459 auto iter = gsyst_to_cv_map.find( knob );
460 if ( iter != gsyst_to_cv_map.end() ) {
461 cv_shift = iter->second;
462 if ( !
fQuietMode ) MF_LOG_INFO(
"GENIEWeightCalc")
463 <<
"CV offset added to the "
464 << genie::rew::GSyst::AsString( knob )
465 <<
" knob. New sigma for universe #" << univ <<
" is "
469 double twk_dial_value = it.second[univ]+cv_shift;
470 syst.Set( knob, twk_dial_value );
472 std::cout <<
GetFullName() <<
": univ "<<univ<<
" "<<genie::rew::GSyst::AsString( knob )<<
" Knob value: "<<twk_dial_value<<std::endl;
475 MF_LOG_INFO(
"GENIEWeightCalc") <<
"In universe #" << univ <<
", knob name" << name
476 <<
" (" << genie::rew::GSyst::AsString( knob ) <<
") was set to"
477 <<
" the value " << twk_dial_value <<
" with shift "<<cv_shift;
492 art::Handle< std::vector<simb::MCTruth> > mcTruthHandle;
493 art::Handle< std::vector<simb::GTruth> > gTruthHandle;
499 std::vector< art::Ptr<simb::MCTruth> > mclist;
500 art::fill_ptr_vector( mclist, mcTruthHandle );
502 std::vector< art::Ptr<simb::GTruth > > glist;
503 art::fill_ptr_vector( glist, gTruthHandle );
508 std::vector< float > weights( num_knobs );
514 std::unique_ptr< genie::EventRecord >
515 genie_event( evgb::RetrieveGHEP(*mclist[inu], *glist[inu]) );
525 genie::Interaction* interaction = genie_event->Summary();
526 genie::Kinematics* kine_ptr = interaction->KinePtr();
529 double ml = interaction->FSPrimLepton()->Mass();
531 const TLorentzVector& p4l = kine_ptr->FSLeptonP4();
533 double Tl = p4l.E() - ml;
535 double ctl = p4l.CosTheta();
537 kine_ptr->SetKV( kKVTl, Tl );
538 kine_ptr->SetKV( kKVctl, ctl );
543 for (
size_t k = 0u;
k < num_knobs; ++
k ) {
546 std::vector< genie::rew::GSyst_t > ak =
reweightVector.at(
k ).Systematics().AllIncluded();
550 if(glist[inu]->fIsStrange && std::find(ak.begin(), ak.end(), kXSecTwkDial_MaCCQE) != ak.end()){
565 const std::vector<genie::rew::GSyst_t>& knob_vec){
566 std::map< std::string, int > modes_to_use;
568 for (
const auto& knob : knob_vec ) {
569 for (
const auto& pair1 : INCOMPATIBLE_GENIE_KNOBS ) {
570 std::string calc_name = pair1.first;
571 const auto& mode_map = pair1.second;
572 for (
const auto& pair2 : mode_map ) {
573 int mode = pair2.first;
574 std::set<genie::rew::GSyst_t> knob_set = pair2.second;
576 if ( knob_set.count(knob) ) {
577 auto search = modes_to_use.find( calc_name );
578 if ( search != modes_to_use.end() ) {
579 if ( search->second != mode ) {
580 auto knob_str = genie::rew::GSyst::AsString( knob );
581 throw cet::exception(__PRETTY_FUNCTION__) << this->
GetName()
582 <<
": the GENIE knob " << knob_str <<
" is incompatible"
583 <<
" with others that are already configured";
586 else modes_to_use[ calc_name ] =
mode;
597 const std::map<std::string, int>& modes_to_use){
599 rw.AdoptWghtCalc(
"xsec_ncel",
new GReWeightNuXSecNCEL );
600 rw.AdoptWghtCalc(
"xsec_ccqe",
new GReWeightNuXSecCCQE );
601 rw.AdoptWghtCalc(
"xsec_ccqe_axial",
new GReWeightNuXSecCCQEaxial );
602 rw.AdoptWghtCalc(
"xsec_ccqe_vec",
new GReWeightNuXSecCCQEvec );
603 rw.AdoptWghtCalc(
"xsec_ccres",
new GReWeightNuXSecCCRES );
604 rw.AdoptWghtCalc(
"xsec_ncres",
new GReWeightNuXSecNCRES );
605 rw.AdoptWghtCalc(
"xsec_nonresbkg",
new GReWeightNonResonanceBkg );
606 rw.AdoptWghtCalc(
"xsec_coh",
new GReWeightNuXSecCOH );
607 rw.AdoptWghtCalc(
"xsec_dis",
new GReWeightNuXSecDIS );
608 rw.AdoptWghtCalc(
"nuclear_qe",
new GReWeightFGM );
609 rw.AdoptWghtCalc(
"hadro_res_decay",
new GReWeightResonanceDecay );
610 rw.AdoptWghtCalc(
"hadro_fzone",
new GReWeightFZone );
611 rw.AdoptWghtCalc(
"hadro_intranuke",
new GReWeightINuke );
612 rw.AdoptWghtCalc(
"hadro_agky",
new GReWeightAGKY );
613 rw.AdoptWghtCalc(
"xsec_nc",
new GReWeightNuXSecNC );
614 rw.AdoptWghtCalc(
"res_dk",
new GReWeightResonanceDecay );
615 rw.AdoptWghtCalc(
"xsec_empmec",
new GReWeightXSecEmpiricalMEC);
620 #ifdef GENIE_UB_PATCH
623 rw.AdoptWghtCalc(
"xsec_mec",
new GReWeightXSecMEC );
626 rw.AdoptWghtCalc(
"deltarad_angle",
new GReWeightDeltaradAngle );
627 rw.AdoptWghtCalc(
"xsec_coh_ub",
new GReWeightNuXSecCOHuB );
628 rw.AdoptWghtCalc(
"res_bug_fix",
new GReWeightRESBugFix );
633 for (
const auto& pair : modes_to_use ) {
634 std::string calc_name = pair.first;
635 int mode = pair.second;
637 genie::rew::GReWeightI* calc = rw.WghtCalc( calc_name );
638 if ( !calc )
throw cet::exception(__PRETTY_FUNCTION__)
639 <<
"Failed to retrieve the GENIE weight calculator labeled \""
640 << calc_name <<
'\"';
647 auto* calc_ccqe =
dynamic_cast< genie::rew::GReWeightNuXSecCCQE*
>( calc );
648 auto* calc_ccres =
dynamic_cast< genie::rew::GReWeightNuXSecCCRES*
>( calc );
649 auto* calc_ncres =
dynamic_cast< genie::rew::GReWeightNuXSecNCRES*
>( calc );
650 auto* calc_dis =
dynamic_cast< genie::rew::GReWeightNuXSecDIS*
>( calc );
651 if ( calc_ccqe ) calc_ccqe->SetMode( mode );
652 else if ( calc_ccres ) calc_ccres->SetMode( mode );
653 else if ( calc_ncres ) calc_ncres->SetMode( mode );
654 else if ( calc_dis ) calc_dis->SetMode( mode );
655 else throw cet::exception(__PRETTY_FUNCTION__)
656 <<
"Request to set the mode of an unrecognized GENIE weight calculator \""
657 << calc_name <<
'\"';
void AddParameter(std::string name, float width, float mean=0, size_t covIndex=0)
#define DECLARE_WEIGHTCALC(wghcalc)
EventWeightParameterSet fParameterSet
void Configure(fhicl::ParameterSet const &pset, CLHEP::HepRandomEngine &engine) override
void Configure(std::string name, ReweightType rwtype, size_t nuni=1)
std::vector< float > GetWeight(art::Event &e, size_t inu) override
std::string fGenieModuleLabel
MF_LOG_WARNING("SimWire")<< "SimWire is an example module that works for the "<< "MicroBooNE detector. Each experiment should implement "<< "its own version of this module to simulate electronics "<< "response."
std::string FromString(const std::string &value)
void Sample(CLHEP::HepRandomEngine &engine)
auto end(FixedBins< T, C > const &) noexcept
std::map< EventWeightParameter, std::vector< float > > fParameterMap
Mapping of definitions to the set of values.
auto begin(FixedBins< T, C > const &) noexcept
#define REGISTER_WEIGHTCALC(wghcalc)
std::vector< genie::rew::GReWeight > reweightVector
void SetupWeightCalculators(genie::rew::GReWeight &rw, const std::map< std::string, int > &modes_to_use)
std::size_t count(Cont const &cont)
std::map< std::string, int > CheckForIncompatibleSystematics(const std::vector< genie::rew::GSyst_t > &knob_vec)
BEGIN_PROLOG don t mess with this pandoraTrackGausCryoW true
BEGIN_PROLOG could also be cout
std::string GetFullName()