GENIEGenerator
Loading...
Searching...
No Matches
gRwghtZExpDirect.cxx File Reference
#include <string>
#include <sstream>
#include <TSystem.h>
#include <TFile.h>
#include <TTree.h>
#include <TArrayF.h>
#include "Algorithm/AlgConfigPool.h"
#include "Algorithm/AlgFactory.h"
#include "Base/XSecAlgorithmI.h"
#include "Conventions/Controls.h"
#include "EVGCore/EventRecord.h"
#include "Ntuple/NtpMCFormat.h"
#include "Ntuple/NtpMCTreeHeader.h"
#include "Ntuple/NtpMCEventRecord.h"
#include "Messenger/Messenger.h"
#include "ReWeight/GReWeightI.h"
#include "ReWeight/GSystSet.h"
#include "ReWeight/GReWeight.h"
#include "ReWeight/GReWeightNuXSecCCQE.h"
#include "ReWeight/GReWeightNuXSecCCQEvec.h"
#include "ReWeight/GReWeightNuXSecCCRES.h"
#include "ReWeight/GReWeightNuXSecNCRES.h"
#include "ReWeight/GReWeightNuXSecDIS.h"
#include "ReWeight/GReWeightNuXSecCOH.h"
#include "ReWeight/GReWeightNonResonanceBkg.h"
#include "ReWeight/GReWeightFGM.h"
#include "ReWeight/GReWeightDISNuclMod.h"
#include "ReWeight/GReWeightResonanceDecay.h"
#include "ReWeight/GReWeightFZone.h"
#include "ReWeight/GReWeightINuke.h"
#include "ReWeight/GReWeightAGKY.h"
#include "ReWeight/GSystUncertainty.h"
#include "Utils/CmdLnArgParser.h"
#include "Utils/StringUtils.h"
Include dependency graph for gRwghtZExpDirect.cxx:

Go to the source code of this file.

Macros

#define MAX_COEF   4
 A simple program to reweight the GENIE z-expansion axial form factor from one set of z-expansion parameters directly to another.

Functions

void PrintSyntax ()
void GetEventRange (Long64_t nev_in_file, Long64_t &nfirst, Long64_t &nlast)
void GetCommandLineArgs (int argc, char **argv)
GSyst_t GetZExpSystematic (int ip)
int main (int argc, char **argv)

Variables

string gOptInpFilename
string gOptOutFilename
Long64_t gOptNEvt1
Long64_t gOptNEvt2
bool gOptDoNorm = false
double gOptNormValue = 1.
double gOptParameterValue [MAX_COEF] = {0.}

Macro Definition Documentation

◆ MAX_COEF

#define MAX_COEF   4

A simple program to reweight the GENIE z-expansion axial form factor from one set of z-expansion parameters directly to another.

\program gRwghtZExpDirect

\syntax grwghtzexpaxff -f filename -v val1,val2,val3,val4 [-n nev] [-o fileOutName] [-m norm]

    where 
    [] is an optional argument
    -f specifies a GENIE event file (GHEP format)
    -o specifies a GENIE output filename
    -n specifies the number of events to process (default: all)
    -v z-expansion values to reweight to
    -m reweight normalization of form factor (default: 1)
Author
Aaron Meyer <asmeyer2012 \at uchicago.edu> University of Chicago, Fermi National Accelerator Laboratory

based on gtestRewght by

Costas Andreopoulos <c.andreopoulos \at cern.ch> University of Liverpool

Created:\n Mar 14, 2016
License:\n Copyright (c) 2003-2025, The GENIE Collaboration
For the full text of the license visit http://copyright.genie-mc.org

Definition at line 74 of file gRwghtZExpDirect.cxx.

Function Documentation

◆ GetCommandLineArgs()

void GetCommandLineArgs ( int argc,
char ** argv )

Definition at line 282 of file gRwghtZExpDirect.cxx.

283{
284 LOG("rwghtzexpaxff", pINFO) << "*** Parsing command line arguments";
285
286 CmdLnArgParser parser(argc,argv);
287
288 // get GENIE event sample
289 if( parser.OptionExists('f') ) {
290 LOG("rwghtzexpaxff", pINFO) << "Reading event sample filename";
291 gOptInpFilename = parser.ArgAsString('f');
292 } else {
293 LOG("rwghtzexpaxff", pFATAL)
294 << "Unspecified input filename - Exiting";
295 PrintSyntax();
296 exit(1);
297 }
298
299 // output weight file
300 if(parser.OptionExists('o')) {
301 LOG("rwghtzexpaxff", pINFO) << "Reading requested output filename";
302 gOptOutFilename = parser.ArgAsString('o');
303 } else {
304 LOG("rwghtzexpaxff", pINFO) << "Setting default output filename";
305 gOptOutFilename = "test_rw_zexp_axff.root";
306 }
307
308 if ( parser.OptionExists('n') ) {
309 //
310 LOG("grwghtzexpaxff", pINFO) << "Reading number of events to analyze";
311 string nev = parser.ArgAsString('n');
312 if (nev.find(",") != string::npos) {
313 vector<long> vecn = parser.ArgAsLongTokens('n',",");
314 if(vecn.size()!=2) {
315 LOG("grwghtzexpaxff", pFATAL) << "Invalid syntax";
316 gAbortingInErr = true;
317 PrintSyntax();
318 exit(1);
319 }
320 // User specified a comma-separated set of values n1,n2.
321 // Use [n1,n2] as the event range to process.
322 gOptNEvt1 = vecn[0];
323 gOptNEvt2 = vecn[1];
324 } else {
325 // User specified a single number n.
326 // Use [0,n] as the event range to process.
327 gOptNEvt1 = -1;
328 gOptNEvt2 = parser.ArgAsLong('n');
329 }
330 } else {
331 LOG("grwghtzexpaxff", pINFO)
332 << "Unspecified number of events to analyze - Use all";
333 gOptNEvt1 = -1;
334 gOptNEvt2 = -1;
335 }
336 LOG("grwghtzexpaxff", pDEBUG)
337 << "Input event range: " << gOptNEvt1 << ", " << gOptNEvt2;
338
339 // values to reweight to:
340 if( parser.OptionExists('v') ) {
341 LOG("rwghtzexpaxff", pINFO) << "Reading requested parameter values";
342 string coef = parser.ArgAsString('v');
343
344 vector<string> zvals = utils::str::Split(coef, ",");
345 // MAX_COEF should be set to the number of z-expansion tweaks which exist
346 assert(zvals.size() == (unsigned int) MAX_COEF);
347 for (int ik = 0;ik<MAX_COEF;ik++)
348 {
349 gOptParameterValue[ik] = atof(zvals[ik].c_str());
350 }
351 for (int ik = 0;ik<MAX_COEF;ik++)
352 {
353 LOG("rwghtzexpaxff",pINFO)<<"Parameter value "<<ik+1<<": "<<
355 }
356 }
357
358 // norm to reweight to:
359 if( parser.OptionExists('m') ) {
360 LOG("rwghtzexpaxff", pINFO) << "Reading requested normalization";
361 string coef = parser.ArgAsString('m');
362 gOptDoNorm = true;
363 gOptNormValue = atof(coef.c_str());
364 LOG("rwghtzexpaxff",pINFO)<<"Requested normalization: "<< gOptNormValue;
365 }
366
367}
#define pINFO
Definition Messenger.h:62
#define pFATAL
Definition Messenger.h:56
#define pDEBUG
Definition Messenger.h:63
#define LOG(stream, priority)
A macro that returns the requested log4cpp::Category appending a string (using the FILE,...
Definition Messenger.h:96
Command line argument parser.
string gOptInpFilename
Definition gEvDump.cxx:76
string gOptOutFilename
Definition gEvScan.cxx:93
Long64_t gOptNEvt2
bool gOptDoNorm
Long64_t gOptNEvt1
void PrintSyntax()
double gOptNormValue
double gOptParameterValue[MAX_COEF]
#define MAX_COEF
Program used for testing / debugging the axial form factor.
vector< string > Split(string input, string delim)
bool gAbortingInErr
Definition Messenger.cxx:34

References genie::CmdLnArgParser::ArgAsLong(), genie::CmdLnArgParser::ArgAsLongTokens(), genie::CmdLnArgParser::ArgAsString(), genie::gAbortingInErr, gOptDoNorm, gOptInpFilename, gOptNEvt1, gOptNEvt2, gOptNormValue, gOptOutFilename, gOptParameterValue, LOG, MAX_COEF, genie::CmdLnArgParser::OptionExists(), pDEBUG, pFATAL, pINFO, PrintSyntax(), and genie::utils::str::Split().

Referenced by main().

◆ GetEventRange()

void GetEventRange ( Long64_t nev_in_file,
Long64_t & nfirst,
Long64_t & nlast )

Definition at line 369 of file gRwghtZExpDirect.cxx.

370{
371 nfirst = 0;
372 nlast = 0;
373
374 if(gOptNEvt1>=0 && gOptNEvt2>=0) {
375 // Input was `-n N1,N2'.
376 // Process events [N1,N2].
377 // Note: Incuding N1 and N2.
378 nfirst = gOptNEvt1;
379 nlast = TMath::Min(nev_in_file-1, gOptNEvt2);
380 }
381 else
383 // Input was `-n N'.
384 // Process first N events [0,N).
385 // Note: Event N is not included.
386 nfirst = 0;
387 nlast = TMath::Min(nev_in_file-1, gOptNEvt2-1);
388 }
389 else
390 if(gOptNEvt1<0 && gOptNEvt2<0) {
391 // No input. Process all events.
392 nfirst = 0;
393 nlast = nev_in_file-1;
394 }
395
396 assert(nfirst <= nlast && nfirst >= 0 && nlast <= nev_in_file-1);
397}

References gOptNEvt1, and gOptNEvt2.

Referenced by main().

◆ GetZExpSystematic()

GSyst_t GetZExpSystematic ( int ip)

Definition at line 399 of file gRwghtZExpDirect.cxx.

400{
401 switch(ip){
402 case 1: return kXSecTwkDial_ZExpA1CCQE; break;
403 case 2: return kXSecTwkDial_ZExpA2CCQE; break;
404 case 3: return kXSecTwkDial_ZExpA3CCQE; break;
405 case 4: return kXSecTwkDial_ZExpA4CCQE; break;
406 default:
407 LOG("rwghtzexpaxff", pFATAL)
408 << "Cannot find systematic corresponding to parameter " << ip;
409 exit(0);
410 break;
411 }
412}

References LOG, and pFATAL.

Referenced by main().

◆ main()

int main ( int argc,
char ** argv )

Definition at line 95 of file gRwghtZExpDirect.cxx.

96{
97 GetCommandLineArgs (argc, argv);
98
99 // open the ROOT file and get the TTree & its header
100 TTree * tree = 0;
101 NtpMCTreeHeader * thdr = 0;
102 TFile file(gOptInpFilename.c_str(),"READ");
103 tree = dynamic_cast <TTree *> ( file.Get("gtree") );
104 thdr = dynamic_cast <NtpMCTreeHeader *> ( file.Get("header") );
105 LOG("rwghtzexpaxff", pNOTICE) << "Input tree header: " << *thdr;
106 if(!tree){
107 LOG("grwghtzexpaxff", pFATAL)
108 << "Can't find a GHEP tree in input file: "<< file.GetName();
109 gAbortingInErr = true;
110 PrintSyntax();
111 exit(1);
112 }
113 NtpMCEventRecord * mcrec = 0;
114 tree->SetBranchAddress("gmcrec", &mcrec);
115
116 Long64_t nev_in_file = tree->GetEntries();
117 Long64_t nfirst = 0;
118 Long64_t nlast = 0;
119 GetEventRange(nev_in_file, nfirst, nlast);
120 int nev = int(nlast - nfirst + 1);
121
122 LOG("rwghtzexpaxff", pNOTICE) << "Will process " << nev << " events";
123
124 //
125 // Create a GReWeight object and add to it a set of
126 // weight calculators
127 //
128 // If seg-faulting here, need to change
129 // AxialFormFactorModel in UserPhysicsOptions.xml and LwlynSmithFFCC.xml
130 //
131
132 GReWeight rw;
133 rw.AdoptWghtCalc( "xsec_ccqe", new GReWeightNuXSecCCQE );
134
135 //
136 // Create a list of systematic params (more to be found at GSyst.h)
137 // set non-default values and re-configure.
138 // Weight calculators included above must be able to handle the tweaked params.
139 // Each tweaking dial t modifies a physics parameter p as:
140 // p_{tweaked} = p_{default} ( 1 + t * dp/p )
141 // So setting a tweaking dial to +/-1 modifies a physics quantity
142 // by +/- 1sigma.
143 // Default fractional errors are defined in GSystUncertainty
144 // and can be overriden.
145 //
146
147 GSystSet & syst = rw.Systematics();
148
149 // Create a concrete weight calculator to fine-tune
150 GReWeightNuXSecCCQE * rwccqe =
151 dynamic_cast<GReWeightNuXSecCCQE *> (rw.WghtCalc("xsec_ccqe"));
152 rwccqe->SetMode(GReWeightNuXSecCCQE::kModeZExp);
153 // In case uncertainties need to be altered
154 GSystUncertainty * unc = GSystUncertainty::Instance();
155
156 // Set up algorithm for loading central values
158 AlgId id("genie::LwlynSmithQELCCPXSec","ZExp");
159
160 Algorithm * alg = algf->AdoptAlgorithm(id);
161 XSecAlgorithmI* fXSecModel = dynamic_cast<XSecAlgorithmI*>(alg);
162 fXSecModel->AdoptSubstructure();
163
164 Registry * fXSecModelConfig = new Registry(fXSecModel->GetConfig());
166 const Registry * gc = confp->GlobalParameterList();
167
168 // further optional fine-tuning
169 //rwccqe -> RewNue (false);
170 //rwccqe -> RewNuebar (false);
171 //rwccqe -> RewNumubar(false);
172
173 // Declare the weights and twkvals arrays
174 const int n_events = (const int) nev;
175 const int n_params = (const int) MAX_COEF;
176 // if segfaulting here, may need to increase MAX_COEF
177 float weights [n_events];
178
179 // Initialize
180 for (int iev = 0; iev < nev; iev++) { weights[iev] = 1.; }
181
182 // set first values for weighting
183 if (gOptDoNorm)
184 {
185 //LOG("rwghtzexpaxff", pNOTICE) << "Setting z-expansion tweak for norm : "
186 // << (gOptNormValue-1.)
187 // /unc->OneSigmaErr(kXSecTwkDial_ZNormCCQE,TMath::Sign(1,(int)(gOptNormValue-1.)));
188 syst.Set(kXSecTwkDial_ZNormCCQE, (gOptNormValue-1.)
189 /unc->OneSigmaErr(kXSecTwkDial_ZNormCCQE,TMath::Sign(1,(int)(gOptNormValue-1.))));
190 }
191 GSyst_t gsyst;
192 double cval = 0.; // central value for parameter
193 ostringstream zval_name; // string to use to extract central value
194 for (int ipr = 0; ipr < n_params; ipr++)
195 {
196 gsyst = GetZExpSystematic(ipr+1); // get tweak dial
197 zval_name.str("");
198 zval_name << "QEL-Z_A" << ipr+1;
199
200 cval = fXSecModelConfig->GetDoubleDef(zval_name.str(),gc->GetDouble(zval_name.str()));
201 unc->SetUncertainty(gsyst,1.,1.); // easier to deal with
202
203 //LOG("rwghtzexpaxff", pNOTICE) << "Setting z-expansion tweak for param "
204 // <<ipr<<" : " << (gOptParameterValue[ipr]-cval)/cval;
205 syst.Set(gsyst, (gOptParameterValue[ipr]-cval)/cval);
206 }
207
208 rw.Reconfigure();
209 // Event loop
210 for(int iev = nfirst; iev <= nlast; iev++) {
211 tree->GetEntry(iev);
212
213 EventRecord & event = *(mcrec->event);
214 LOG("rwghtzexpaxff", pNOTICE) << "Event number = " << iev;
215 LOG("rwghtzexpaxff", pNOTICE) << event;
216
217 double wght = rw.CalcWeight(event);
218
219 LOG("rwghtzexpaxff", pNOTICE) << "Overall weight = " << wght;
220
221 // add to arrays
222 weights[iev - nfirst] = wght;
223
224 mcrec->Clear();
225 } // events
226
227 // Close event file
228 file.Close();
229
230 //
231 // Save weights
232 //
233
234 // Make an output tree for saving the weights.
235 TFile * wght_file = new TFile(gOptOutFilename.c_str(), "RECREATE");
236 TTree * wght_tree = new TTree("ZExpCCQE","GENIE weights tree");
237 // objects to pass elements into tree
238 int branch_eventnum = 0;
239 float branch_weight = 1.;
240 float branch_norm_val = gOptNormValue;
241 float branch_zparam_val[MAX_COEF] = {0.};
242
243 wght_tree->Branch("eventnum", &branch_eventnum);
244 wght_tree->Branch("weights", &branch_weight);
245 wght_tree->Branch("norm", &branch_norm_val);
246
247 // create and add branches for each z-expansion coefficient
248 ostringstream zparam_brnch_name;
249 for (int ipr = 0; ipr < n_params; ipr++) {
250 zparam_brnch_name.str("");
251 zparam_brnch_name << "param_" << ipr+1;
252 LOG("rwghtzexpaxff", pINFO) << "Branch name = " << zparam_brnch_name.str();
253 branch_zparam_val[ipr] = gOptParameterValue[ipr];
254 LOG("rwghtzexpaxff", pINFO) << "Setting parameter value = " << gOptParameterValue[ipr];
255 wght_tree->Branch(zparam_brnch_name.str().c_str(), &branch_zparam_val[ipr]);
256 }
257
258 ostringstream str_wght;
259 for(int iev = nfirst; iev <= nlast; iev++) {
260 branch_eventnum = iev;
261
262 // printout
263 LOG("grwghtzexpaxff", pNOTICE)
264 << "Filling tree with wght = " << weights[iev - nfirst];
265 branch_weight = weights[iev - nfirst];
266 wght_tree->Fill();
267 }
268
269 wght_file->cd();
270 wght_tree->Write();
271 wght_tree = 0;
272 wght_file->Close();
273
274 // free memory
275 delete wght_tree;
276 delete wght_file;
277
278 LOG("rwghtzexpaxff", pNOTICE) << "Done!";
279 return 0;
280}
#define pNOTICE
Definition Messenger.h:61
A singleton class holding all configuration registries built while parsing all loaded XML configurati...
static AlgConfigPool * Instance()
Registry * GlobalParameterList(void) const
The GENIE Algorithm Factory.
Definition AlgFactory.h:39
static AlgFactory * Instance()
Algorithm * AdoptAlgorithm(const AlgId &algid) const
Algorithm ID (algorithm name + configuration set name)
Definition AlgId.h:34
Algorithm abstract base class.
Definition Algorithm.h:54
void AdoptSubstructure(void)
virtual const Registry & GetConfig(void) const
Generated Event Record. It is a GHepRecord object that can accept / be visited by EventRecordVisitorI...
Definition EventRecord.h:37
MINOS-style ntuple record. Each such ntuple record holds a generated EventRecord object....
EventRecord * event
event
void Clear(Option_t *opt="")
MINOS-style Ntuple Class to hold an output MC Tree Header.
A registry. Provides the container for algorithm configuration parameters.
Definition Registry.h:65
RgDbl GetDouble(RgKey key) const
Definition Registry.cxx:474
RgDbl GetDoubleDef(RgKey key, RgDbl def_opt, bool set_def=true)
Definition Registry.cxx:535
Cross Section Calculation Interface.
void GetEventRange(Long64_t nev_in_file, Long64_t &nfirst, Long64_t &nlast)
void GetCommandLineArgs(int argc, char **argv)
GSyst_t GetZExpSystematic(int ip)

References genie::AlgFactory::AdoptAlgorithm(), genie::Algorithm::AdoptSubstructure(), genie::NtpMCEventRecord::Clear(), genie::NtpMCEventRecord::event, genie::gAbortingInErr, GetCommandLineArgs(), genie::Algorithm::GetConfig(), genie::Registry::GetDouble(), genie::Registry::GetDoubleDef(), GetEventRange(), GetZExpSystematic(), genie::AlgConfigPool::GlobalParameterList(), gOptDoNorm, gOptInpFilename, gOptNormValue, gOptOutFilename, gOptParameterValue, genie::AlgConfigPool::Instance(), genie::AlgFactory::Instance(), LOG, MAX_COEF, pFATAL, pINFO, pNOTICE, and PrintSyntax().

◆ PrintSyntax()

void PrintSyntax ( void )

Definition at line 414 of file gRwghtZExpDirect.cxx.

415{
416 LOG("grwghtzexpaxff", pFATAL)
417 << "\n\n"
418 << "grwghtzexpaxff \n"
419 << " -f input_event_file \n"
420 << " -v val1,val2,val3,val4 \n"
421 << " [-n nev] \n"
422 << " [-o output_weights_file]";
423}

References LOG, and pFATAL.

Referenced by GetCommandLineArgs(), and main().

Variable Documentation

◆ gOptDoNorm

bool gOptDoNorm = false

Definition at line 90 of file gRwghtZExpDirect.cxx.

◆ gOptInpFilename

string gOptInpFilename

Definition at line 86 of file gRwghtZExpDirect.cxx.

◆ gOptNEvt1

Long64_t gOptNEvt1

Definition at line 88 of file gRwghtZExpDirect.cxx.

◆ gOptNEvt2

Long64_t gOptNEvt2

Definition at line 89 of file gRwghtZExpDirect.cxx.

◆ gOptNormValue

double gOptNormValue = 1.

Definition at line 91 of file gRwghtZExpDirect.cxx.

Referenced by GetCommandLineArgs(), and main().

◆ gOptOutFilename

string gOptOutFilename

Definition at line 87 of file gRwghtZExpDirect.cxx.

◆ gOptParameterValue

double gOptParameterValue[MAX_COEF] = {0.}

Definition at line 92 of file gRwghtZExpDirect.cxx.

92{0.};

Referenced by GetCommandLineArgs(), and main().