Macros | Functions | Variables
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"

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. More...
 

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

#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.

gRwghtZExpDirect

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 uchicago.edu> University of Chicago, Fermi National Accelerator Laboratory

based on gtestRewght by

Costas Andreopoulos <constantinos.andreopoulos cern.ch> STFC, Rutherford Appleton Laboratory

Mar 14, 2016

Copyright (c) 2003-2020, 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

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<<": "<<
354  gOptParameterValue[ik];
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 }
void PrintSyntax()
#define MAX_COEF
A simple program to reweight the GENIE z-expansion axial form factor from one set of z-expansion para...
#define pFATAL
Definition: Messenger.h:56
double gOptParameterValue[MAX_COEF]
#define LOG(stream, priority)
A macro that returns the requested log4cpp::Category appending a string (using the FILE...
Definition: Messenger.h:96
Long64_t gOptNEvt2
#define pINFO
Definition: Messenger.h:62
bool gOptDoNorm
string gOptOutFilename
vector< string > Split(string input, string delim)
Definition: StringUtils.cxx:36
double gOptNormValue
string gOptInpFilename
Command line argument parser.
Long64_t gOptNEvt1
bool gAbortingInErr
Definition: Messenger.cxx:34
#define pDEBUG
Definition: Messenger.h:63
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
382  if(gOptNEvt1<0 && gOptNEvt2>=0) {
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 }
Long64_t gOptNEvt2
Long64_t gOptNEvt1
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 }
#define pFATAL
Definition: Messenger.h:56
#define LOG(stream, priority)
A macro that returns the requested log4cpp::Category appending a string (using the FILE...
Definition: Messenger.h:96
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
157  AlgFactory * algf = AlgFactory::Instance();
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());
165  AlgConfigPool * confp = AlgConfigPool::Instance();
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 }
Cross Section Calculation Interface.
RgDbl GetDoubleDef(RgKey key, RgDbl def_opt, bool set_def=true)
Definition: Registry.cxx:535
void PrintSyntax()
#define MAX_COEF
A simple program to reweight the GENIE z-expansion axial form factor from one set of z-expansion para...
MINOS-style ntuple record. Each such ntuple record holds a generated EventRecord object. Ntuples of this type are intended for feeding GENIE events into other applications (for example the GEANT4 based MC generation framework of an experiment) if no direct interface exists.
A singleton class holding all configuration registries built while parsing all loaded XML configurati...
Definition: AlgConfigPool.h:40
#define pFATAL
Definition: Messenger.h:56
Algorithm abstract base class.
Definition: Algorithm.h:53
RgDbl GetDouble(RgKey key) const
Definition: Registry.cxx:474
void GetEventRange(Long64_t nev_in_file, Long64_t &nfirst, Long64_t &nlast)
virtual const Registry & GetConfig(void) const
Definition: Algorithm.cxx:246
double gOptParameterValue[MAX_COEF]
#define LOG(stream, priority)
A macro that returns the requested log4cpp::Category appending a string (using the FILE...
Definition: Messenger.h:96
void GetCommandLineArgs(int argc, char **argv)
GSyst_t GetZExpSystematic(int ip)
MINOS-style Ntuple Class to hold an output MC Tree Header.
#define pINFO
Definition: Messenger.h:62
void AdoptSubstructure(void)
Definition: Algorithm.cxx:400
Algorithm * AdoptAlgorithm(const AlgId &algid) const
Definition: AlgFactory.cxx:116
bool gOptDoNorm
Generated Event Record. It is a GHepRecord object that can accept / be visited by EventRecordVisitorI...
Definition: EventRecord.h:37
Algorithm ID (algorithm name + configuration set name)
Definition: AlgId.h:34
string gOptOutFilename
double gOptNormValue
A registry. Provides the container for algorithm configuration parameters.
Definition: Registry.h:65
string gOptInpFilename
#define pNOTICE
Definition: Messenger.h:61
The GENIE Algorithm Factory.
Definition: AlgFactory.h:39
void Clear(Option_t *opt="")
bool gAbortingInErr
Definition: Messenger.cxx:34
EventRecord * event
event
Event finding and building.
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 }
#define pFATAL
Definition: Messenger.h:56
#define LOG(stream, priority)
A macro that returns the requested log4cpp::Category appending a string (using the FILE...
Definition: Messenger.h:96

Variable Documentation

bool gOptDoNorm = false

Definition at line 90 of file gRwghtZExpDirect.cxx.

string gOptInpFilename

Definition at line 86 of file gRwghtZExpDirect.cxx.

Long64_t gOptNEvt1

Definition at line 88 of file gRwghtZExpDirect.cxx.

Long64_t gOptNEvt2

Definition at line 89 of file gRwghtZExpDirect.cxx.

double gOptNormValue = 1.

Definition at line 91 of file gRwghtZExpDirect.cxx.

string gOptOutFilename

Definition at line 87 of file gRwghtZExpDirect.cxx.

double gOptParameterValue[MAX_COEF] = {0.}

Definition at line 92 of file gRwghtZExpDirect.cxx.