4 #include "fhiclcpp/ParameterSet.h"
12 CLHEP::HepRandomEngine& engine) {
16 const fhicl::ParameterSet& pset = p.get<fhicl::ParameterSet>(
GetName());
21 auto const& pars = pset.get<std::vector<std::string> >(
"parameter_list");
22 std::vector< float > parsigmas(pars.size(), 1.0);
23 if (pars.size() != parsigmas.size()) {
24 throw cet::exception(__PRETTY_FUNCTION__) <<
GetName() <<
": "
25 <<
"parameter_list and parameter_sigma length mismatch."
29 if(!pset.get_if_present(
"parameter_sigma", parsigmas)){
30 std::cout<<
"SBNEventWeight Flux: `parameter_sigma` was not set; now it is set as 1"<<std::endl;
33 std::string
fMode = pset.get<std::string>(
"mode");
34 int number_of_multisims = pset.get<
int>(
"number_of_multisims", 1);
37 for (
size_t i=0; i<pars.size(); i++) {
44 CalcType = pset.get< std::string >(
"calc_type");
47 fScalePos = pset.get<
double>(
"scale_factor_pos");
48 if(!pset.get_if_present(
"scale_factor_neg",
fScaleNeg)){
49 std::cout<<
"SBNEventWeight Flux: auto-assignment: scale_factor_neg = 1."<<std::endl;
52 cet::search_path sp(
"FW_SEARCH_PATH");
64 std::string dataInput1 = pset.get< std::string >(
"CentralValue_hist_file");
65 std::string cvfile_path = sp.find_file(dataInput1);
66 TFile fcv(Form(
"%s",cvfile_path.c_str()));
68 std::string dataInput2pos = pset.get< std::string >(
"PositiveSystematicVariation_hist_file");
69 std::string rwfilepos = sp.find_file(dataInput2pos);
70 TFile frwpos(Form(
"%s", rwfilepos.c_str()));
72 std::string dataInput2neg = pset.get< std::string >(
"NegativeSystematicVariation_hist_file");
73 std::string rwfileneg = sp.find_file(dataInput2neg);
74 TFile frwneg(Form(
"%s", rwfileneg.c_str()));
76 if(dataInput2pos == dataInput2neg)
PosOnly =
true;
78 int cptype[4] = {1,2,3,4};
79 int cntype[4] = {1,2,3,4};
81 for (
int iptyp=0;iptyp<4;iptyp++) {
82 for (
int intyp=0;intyp<4;intyp++) {
83 for (
int ibin=0;ibin<200;ibin++) {
84 fCV[iptyp][intyp][ibin]=(
dynamic_cast<TH1F*
> (fcv.Get(Form(
"h5%d%d",cptype[iptyp],cntype[intyp]))))->GetBinContent(ibin+1);
85 fRWpos[iptyp][intyp][ibin]=(
dynamic_cast<TH1F*
> (frwpos.Get(Form(
"h5%d%d",cptype[iptyp],cntype[intyp]))))->GetBinContent(ibin+1);
86 fRWneg[iptyp][intyp][ibin]=(
dynamic_cast<TH1F*
> (frwneg.Get(Form(
"h5%d%d",cptype[iptyp],cntype[intyp]))))->GetBinContent(ibin+1);
97 }
else if(
CalcType.compare(0, 13,
"PrimaryHadron") == 0){
99 fprimaryHad = pset.get< std::vector<int>>(
"PrimaryHadronGeantCode");
101 if(
CalcType ==
"PrimaryHadronNormalization" ){
107 std::string dataInput = pset.get< std::string >(
"ExternalData");
108 std::string ExternalDataInput = sp.find_file(dataInput);
109 TFile*
file =
new TFile(Form(
"%s",ExternalDataInput.c_str()));
111 std::vector< std::string > pname;
112 if(
CalcType ==
"PrimaryHadronFeynmanScaling" ){
114 pname.push_back(
"FS/KPlus/FSKPlusFitVal");
115 pname.push_back(
"FS/KPlus/FSKPlusFitCov");
117 TArrayD* FSKPlusFitValArray = (TArrayD*) file->Get(pname[0].c_str());
119 FitCov = (TMatrixD*) file->Get(pname[1].c_str());
122 }
else if(
CalcType ==
"PrimaryHadronSanfordWang" ){
123 pname.push_back(
"SW/K0s/SWK0sFitVal");
124 pname.push_back(
"SW/K0s/SWK0sFitCov");
125 TArrayD* SWK0FitValArray = (TArrayD*) file->Get(pname[0].c_str());
128 FitCov = (TMatrixD*) file->Get(pname[1].c_str());
131 }
else if(
CalcType ==
"PrimaryHadronSWCentralSplineVariation" ){
133 std::string fitInput = pset.get< std::string >(
"ExternalFit");
134 std::string HadronName;
135 std::string HadronAbriviation;
137 HadronName =
"PiPlus";
138 HadronAbriviation =
"PP";
140 HadronName =
"PiMinus";
141 HadronAbriviation =
"PM";
143 throw art::Exception(art::errors::StdException)
144 <<
"sanford-wang is only configured for charged pions ";
146 pname.push_back( Form(
"HARPData/%s/%sCrossSection",HadronName.c_str(),HadronAbriviation.c_str()) );
147 pname.push_back( Form(
"HARPData/%s/%scovarianceMatrix",HadronName.c_str(),HadronAbriviation.c_str()) );
148 pname.push_back( Form(
"HARPData/%s/%smomentumBoundsArray",HadronName.c_str(),HadronAbriviation.c_str()) );
149 pname.push_back( Form(
"HARPData/%s/%sthetaBoundsArray",HadronName.c_str(),HadronAbriviation.c_str()) );
151 HARPXSec = (TMatrixD*) file->Get(pname[0].c_str());
152 TMatrixD* HARPCov = (TMatrixD*) file->Get(pname[1].c_str());
154 TDecompChol dc = TDecompChol(*(HARPCov));
156 throw art::Exception(art::errors::StdException)
157 <<
"Cannot decompose covariance matrix to begin smearing.";
163 FitCov =
new TMatrixD(dc.GetU());
166 TArrayD* HARPmomentumBoundsArray = (TArrayD*) file->Get(pname[2].c_str());
169 TArrayD* HARPthetaBoundsArray = (TArrayD*) file->Get(pname[3].c_str());
178 std::string ExternalFitInput = sp.find_file(fitInput);
179 TFile* Fitfile =
new TFile(Form(
"%s",ExternalFitInput.c_str()));
182 fitname = Form(
"SW/%s/SW%sFitVal",HadronName.c_str(),HadronName.c_str());
184 TArrayD* SWParamArray = (TArrayD*) Fitfile->Get(fitname.c_str());
188 }
else validC =
false;
193 for(
int index = 1; index < 2*(
FitCov->GetNcols()); index ++){
198 throw cet::exception(__PRETTY_FUNCTION__) <<
GetName() <<
": "
199 <<
" calculator "+
CalcType +
"is invalid"
206 }
else validC =
false;
212 bool count_weights =
false;
215 art::Handle< std::vector<simb::MCFlux> > mcFluxHandle;
217 std::vector<simb::MCFlux>
const& fluxlist = *mcFluxHandle;
223 std::vector<float> weights;
224 if(fluxlist.size() == 0){
225 std::cout<<
"SBNEventWeight Flux: EMPTY WEIGHTS"<<std::endl;
232 weights.resize(NUni);
235 int ptype = std::numeric_limits<int>::max();
236 int ntype = std::numeric_limits<int>::max();
240 if ( fluxlist[inu].fptype==13 || fluxlist[inu].fptype==-13 ) ptype = 0;
241 else if ( fluxlist[inu].fptype==211 || fluxlist[inu].fptype==-211 ) ptype = 1;
242 else if ( fluxlist[inu].fptype==130 ) ptype = 2;
243 else if ( fluxlist[inu].fptype==321 || fluxlist[inu].fptype==-321 ) ptype = 3;
245 throw cet::exception(__FUNCTION__) <<
GetName()<<
"::Unknown ptype "<<fluxlist[0].fptype<< std::endl;
250 if ( fluxlist[inu].fntype== 12 ) ntype=0;
251 else if ( fluxlist[inu].fntype==-12 ) ntype=1;
252 else if ( fluxlist[inu].fntype== 14 ) ntype=2;
253 else if ( fluxlist[inu].fntype==-14 ) ntype=3;
255 throw cet::exception(__FUNCTION__) <<
GetName()<<
"::Unknown ntype "<<fluxlist[inu].fntype<< std::endl;
260 double enu= fluxlist[inu].fnenergyn;
266 for (
size_t i=0;i<weights.size();i++) {
273 }
else if((weights[i]-0)<1e-30){
275 }
else if(fabs(weights[i]-30) < 1e-30){
277 }
else if(fabs(weights[i]-1)<1e-30){
291 weights.resize( NUni);
292 std::fill(weights.begin(), weights.end(), 1);
298 for (
unsigned int i = 0; int(weights.size()) < NUni; i++) {
299 std::pair<bool, double> test_weight;
302 std::vector< float > subVrandom;
303 if(
CalcType ==
"PrimaryHadronNormalization"){
307 subVrandom = {Vrandom.begin()+i*
FitCov->GetNcols(), Vrandom.begin()+(i+1)*
FitCov->GetNcols()};
308 if(
CalcType ==
"PrimaryHadronFeynmanScaling"){
311 }
else if(
CalcType ==
"PrimaryHadronSanfordWang"){
314 }
else if(
CalcType ==
"PrimaryHadronSWCentralSplineVariation"){
317 }
else throw cet::exception(__PRETTY_FUNCTION__) <<
GetName() <<
": this shouldnt happen.."<<std::endl;
320 if(test_weight.first){
321 weights.push_back(test_weight.second);
322 double tmp_weight = test_weight.second;
327 }
else if((tmp_weight-0)<1e-30){
329 }
else if(fabs(tmp_weight-30) < 1e-30){
331 }
else if(fabs(tmp_weight-1)<1e-30){
345 std::cout<<
"SBNEventWeight Flux: Weights counter: (normal, <0, ==0, 1, 30)= ";
355 std::vector<double> v(array->GetSize());
356 std::copy(array->GetArray(), array->GetArray() + array->GetSize(),
void AddParameter(std::string name, float width, float mean=0, size_t covIndex=0)
EventWeightParameterSet fParameterSet
std::vector< double > FitVal
std::vector< double > HARPmomentumBounds
std::string fGeneratorModuleLabel
std::pair< bool, double > PHSWCSVWeightCalc(simb::MCFlux flux, std::vector< float > rand)
double UnisimWeightCalc(double enu, int ptype, int ntype, double randomN, bool noNeg)
void Configure(std::string name, ReweightType rwtype, size_t nuni=1)
std::pair< bool, double > PHSWWeightCalc(simb::MCFlux flux, std::vector< float > rand)
ReweightType fRWType
Type of throws (the same for all parameters in a set)
void Sample(CLHEP::HepRandomEngine &engine)
std::map< EventWeightParameter, std::vector< float > > fParameterMap
Mapping of definitions to the set of values.
void fill(const art::PtrVector< recob::Hit > &hits, int only_plane)
size_t fNuniverses
Number of universes (i.e. random throws)
std::vector< double > SWParam
std::vector< double > ConvertToVector(TArrayD const *array)
std::vector< double > HARPthetaBounds
void Configure(fhicl::ParameterSet const &p, CLHEP::HepRandomEngine &engine) override
#define REGISTER_WEIGHTCALC(wghcalc)
std::pair< bool, double > PHNWeightCalc(simb::MCFlux flux, float rand)
std::pair< bool, double > PHFSWeightCalc(simb::MCFlux flux, std::vector< float > rand)
std::vector< int > fprimaryHad
BEGIN_PROLOG could also be cout
std::vector< float > GetWeight(art::Event &e, size_t inu) override
std::string GetFullName()