gRwghtNCorrelatedParams.cxx
Go to the documentation of this file.
1 //____________________________________________________________________________
2 /*!
3 
4 \program grwghtnp
5 
6 \brief Generates weights given an input GHEP event file, a set of (possibly
7  correlated) systematic parameters (supported by the ReWeight package),
8  and a covariance matrix for the input set of parameters.
9  The covariance matrix should be in the form a ROOT file containing
10  only a TMatrixD object which is square and symmetric.
11  It outputs a ROOT file containing a tree with an entry for every
12  input event. Each such tree entry contains a TArrayD of all computed
13  weights and TArrayD for each requested systematic of all of the
14  corresponding randomly generated tweak dial values.
15 
16 \syntax grwghtnp \
17  -f input_event_file
18  -c input_covariance_file
19  -s systematic1[,systematic2[,...]]
20  -v central_value1[,central_value2[,...]]
21  -t n_twk_dial_values
22  [-n n1[,n2]]
23  [-r run_key]
24  [-o output_weights_file]
25 
26  where
27  [] is an optional argument.
28 
29  -f
30  Specifies a GHEP input file.
31  -c
32  Specifies a binary ROOT file which contains the covariance matrix
33  as a TMatrixD object.
34  -s
35  Specifies the systematic parameters to tweak.
36  See $GENIE/src/ReWeight/GSyst.h for a list of parameters and
37  their corresponding label, which is what should be input here.
38  -v
39  Central values specified in $GENIE/config/UserPhysicsOptions.xml
40  for the reweighted parameters. Assigns uncertainties based on
41  covariance matrix diagonal. Should be automated, but cannot for now.
42  -t
43  Number of random drawings of tweak values between -1 and 1.
44  Values for tweaks respect the covariance of systematics
45  -n
46  Specifies an event range.
47  Examples:
48  - Type `-n 50,2350' to process all 2301 events from 50 up to 2350.
49  Note: Both 50 and 2350 are included.
50  - Type `-n 1000' to process the first 1000 events;
51  from event number 0 up to event number 999.
52  This is an optional argument.
53  By default GENIE will process all events.
54  -r
55  Specifies an integer run key.
56  Changes temporary file names so that multiple instances can run
57  without overwriting each other's temporary tree files
58 
59 \author Aaron Meyer <asmeyer2012 \at uchicago.edu>
60  University of Chicago, Fermi National Accelerator Laboratory
61 
62 \created July 20, 2015
63 
64 \cpright Copyright (c) 2003-2017, GENIE Neutrino MC Generator Collaboration
65  For the full text of the license visit http://copyright.genie-mc.org
66  or see $GENIE/LICENSE
67 */
68 //____________________________________________________________________________
69 
70 
71 #include <TArrayD.h>
72 #include <TFile.h>
73 #include <TKey.h>
74 #include <TList.h>
75 #include <TMath.h>
76 #include <TMatrixD.h>
77 #include <TTree.h>
78 #include <TRandom.h>
79 
80 #include "Conventions/Constants.h"
81 #include "Conventions/Controls.h"
82 #include "EVGCore/EventRecord.h"
83 #include "Ntuple/NtpMCFormat.h"
84 #include "Ntuple/NtpMCTreeHeader.h"
86 #include "Numerical/RandomGen.h"
87 #include "Messenger/Messenger.h"
88 #include "ReWeight/GReWeight.h"
89 #include "ReWeight/GReWeightI.h"
90 #include "ReWeight/GReWeightAGKY.h"
92 #include "ReWeight/GReWeightFGM.h"
103 #include "ReWeight/GSystSet.h"
105 #include "Utils/CmdLnArgParser.h"
106 #include "Utils/MathUtils.h"
107 #include "Utils/StringUtils.h"
108 
109 using namespace genie;
110 using namespace genie::constants;
111 using namespace genie::rew;
112 using namespace genie::utils::math;
113 using std::stringstream;
114 
115 void PrintSyntax();
116 void GetEventRange (Long64_t nev_in_file, Long64_t & nfirst, Long64_t & nlast);
117 void GetCommandLineArgs (int argc, char ** argv);
118 void GetCorrelationMatrix(string fname, TMatrixD *& cmat);
119 void AdoptWeightCalcs (vector<GSyst_t> lsyst, GReWeight & rw);
120 bool FindIncompatibleSystematics(vector<GSyst_t> lsyst);
121 
122 vector<GSyst_t> gOptVSyst;
123 vector<double> gOptVCentVal;
127 Long64_t gOptNEvt1;
128 Long64_t gOptNEvt2;
129 int gOptRunKey= 0;
130 int gOptNSyst = 0;
131 int gOptNTwk = 0;
132 TRandom *tRnd = new TRandom(); // to access normal distribution
133 
134 //___________________________________________________________________
135 int main(int argc, char ** argv)
136 {
137  GetCommandLineArgs (argc, argv);
138 
139  // open the ROOT file and get the TTree & its header
140  TTree * tree = 0;
141  NtpMCTreeHeader * thdr = 0;
142  TFile file(gOptInpFilename.c_str(),"READ");
143  tree = dynamic_cast <TTree *> ( file.Get("gtree") );
144  thdr = dynamic_cast <NtpMCTreeHeader *> ( file.Get("header") );
145  if(!tree){
146  LOG("grwghtnp", pFATAL)
147  << "Can't find a GHEP tree in input file: "<< file.GetName();
148  gAbortingInErr = true;
149  PrintSyntax();
150  exit(1);
151  }
152  LOG("grwghtnp", pNOTICE) << "Input tree header: " << *thdr;
153  if(!FindIncompatibleSystematics(gOptVSyst))
154  {
155  LOG("grwghtnp", pFATAL) << "Error: conflicting systematics";
156  gAbortingInErr = true;
157  exit(1);
158  }
159 
160  //
161  // Preparation for finding correlated vectors
162  //
163  // Solutions are subject to constraint of an error ellipse with equation:
164  // 1 = x^T.Cor^-1.x
165  // x is a vector of tweaks to apply to the systematics
166  // Cholesky Decomposition solves for U in equation:
167  // Cor^-1 = U^T.U
168  // LU decomposition used to solve for x:
169  // L.U.x = b
170  // where L=I and b is a vector of random numbers with b^T.b = 1
171  //
172 
173  TMatrixD *cmat = NULL;
174  // Gets Cor, which is needed in decompositions
175  // Assumed errors from covariance are stored in one sigma errors for parameters
177  TMatrixD lTri = CholeskyDecomposition(*cmat);
178 
179  //LOG("grwghtnp", pNOTICE) << "Correlation matrix:";
180  //cmat->Print();
181  //LOG("grwghtnp", pNOTICE) << "Lower triangular matrix:";
182  //lTri.Print();
183 
184  NtpMCEventRecord * mcrec = 0;
185  tree->SetBranchAddress("gmcrec", &mcrec);
186 
187  Long64_t nev_in_file = tree->GetEntries();
188  Long64_t nfirst = 0;
189  Long64_t nlast = 0;
190  GetEventRange(nev_in_file, nfirst, nlast);
191  int nev = int(nlast - nfirst + 1);
192 
193  LOG("grwghtnp", pNOTICE) << "Will process " << nev << " events";
194 
195  //
196  // Create a GReWeight object and add to it a set of
197  // weight calculators
198  //
199  // If seg-faulting here, probably need to change
200  // models in UserPhysicsOptions.xml and other config files
201  //
202 
203  GReWeight rw;
204  AdoptWeightCalcs(gOptVSyst, rw);
205 
206  //
207  // Create a list of systematic params (more to be found at GSyst.h)
208  // set non-default values and re-configure.
209  // Weight calculators included above must be able to handle the tweaked params.
210  // Each tweaking dial t modifies a physics parameter p as:
211  // p_{tweaked} = p_{default} ( 1 + t * dp/p )
212  // So setting a tweaking dial to +/-1 modifies a physics quantity
213  // by +/- 1sigma.
214  // Default fractional errors are defined in GSystUncertainty
215  // and can be overriden.
216  //
217 
218  GSystSet & syst = rw.Systematics();
219 
220  // Declare the weights, twkvals
221  const int n_params = (const int) gOptNSyst;
222  const int n_tweaks = (const int) gOptNTwk;
223  TVectorD twkvals(n_params);
224 
225  // Initialize
226  for (int ipr = 0; ipr < n_params; ipr++) { twkvals(ipr) = 0.; }
227 
228  // objects to pass elements into tree
229  int branch_eventnum = 0;
230  double branch_weight = 0.;
231 
232  // objects used in processing
234  stringstream twk_dial_brnch_name;
235  stringstream tmpName;
236  int ip;
237 
238  //
239  // REWEIGHTING LOOP
240  // -- do all of reweighting, save to temporary files
241  //
242  TFile * wght_file = NULL;
243  TTree * wght_tree = NULL;
244  for (int itk = 0; itk < gOptNTwk; itk++) {
245  // Make temporary output trees for saving the weights.
246  // This step is necessary because ROOT trees cannot be edited once filled
247  // Later consolidate the trees into a single tree with the requested filename
248  tmpName.str("");
249  tmpName << "_temporary_rwght." <<itk <<"." <<gOptRunKey <<".root";
250  LOG("grwghtnp", pINFO) <<"temporary file: " <<tmpName.str();
251  wght_file = new TFile(tmpName.str().c_str(),"RECREATE");
252  wght_tree = new TTree("covrwt","GENIE weights tree");
253 
254  // Create tree branches
255  wght_tree->Branch("eventnum", &branch_eventnum);
256  wght_tree->Branch("weights", &branch_weight);
257 
258  // Construct multiple branches to streamline loading later
259  // Load tweaks into reweighting
261  // scale the size of the vector to avg length 1
262  //twkvals *= 1./(TMath::Sqrt((double)gOptNSyst));
263  ip = 0;
264  for (it = gOptVSyst.begin();it != gOptVSyst.end(); it++, ip++) {
265  twk_dial_brnch_name.str("");
266  twk_dial_brnch_name << "twk_" << GSyst::AsString(*it);
267  // each array element individually
268  wght_tree->Branch(twk_dial_brnch_name.str().c_str(), &twkvals(ip));
269  //LOG("grwghtnp", pINFO) << "Setting systematic : "
270  // <<GSyst::AsString(*it) <<", " <<twkvals(ip);
271  syst.Set(*it,twkvals(ip));
272  }
273  rw.Reconfigure();
274 
275  stringstream str_wght;
276  str_wght.str("");
277  str_wght << ", parameter tweaks : ";
278  for (int ipr=0; ipr < n_params; ipr++) {
279  if (ipr > 0) str_wght << ", ";
280  str_wght << ipr << " -> " << twkvals(ipr);
281  }
282 
283  for(int iev = nfirst; iev <= nlast; iev++) {
284  branch_eventnum = iev;
285  tree->GetEntry(iev);
286 
287  EventRecord & event = *(mcrec->event);
288  LOG("rwghtzexpaxff", pNOTICE) << "Event_num => " << iev;
289  //LOG("rwghtzexpaxff", pNOTICE) << event;
290 
291  branch_weight = rw.CalcWeight(event);
292  mcrec->Clear();
293  wght_tree->Fill();
294 
295  } // event loop
296 
297  // close out temporary file
298  wght_file->cd();
299  wght_tree->Write();
300  wght_file->Close();
301  //delete wght_tree; // segfault when deleted
302  wght_tree = 0;
303  delete wght_file;
304  } // tweak loop
305 
306  // Close event file
307  file.Close();
308 
309  // open temporary trees for consolidation
310  LOG("rwghtzexpaxff", pNOTICE)
311  << "Consolidating temporary files into ROOT file " << gOptOutFilename;
312  wght_file = new TFile(gOptOutFilename.c_str(),"RECREATE"); // new file
313  wght_tree = new TTree("covrwt","GENIE covariant reweighting tree");
314  wght_tree->Branch("n_tweaks", &gOptNTwk);
315  wght_tree->Branch("eventnum", &branch_eventnum);
316  TFile * file_list[n_tweaks];
317  TTree * wght_list[n_tweaks];
318  for (int itk=0; itk < n_tweaks; itk++) {
319  tmpName.str("");
320  tmpName << "_temporary_rwght." <<itk <<"." <<gOptRunKey <<".root";
321  file_list[itk] = new TFile(tmpName.str().c_str(),"READ");
322  wght_list[itk] = (TTree*)file_list[itk]->Get("covrwt");
323  }
324 
325  // objects to load data into and fill new tree with
326  TArrayD branch_weights_array(gOptNTwk);
327  double * branch_weights_ptr = branch_weights_array.GetArray();
328  TArrayD * branch_twkdials_array[n_params];
329  double * branch_twkdials_ptr [n_params];
330 
331  // set up streamlined weight loading
332  wght_tree->Branch("weights", &branch_weights_array);
333  for (int itk = 0; itk < gOptNTwk; itk++) {
334  wght_list[itk]->SetBranchAddress("weights",&branch_weights_ptr[itk]);
335  }
336 
337  ip = 0;
338  for (it = gOptVSyst.begin();it != gOptVSyst.end(); it++, ip++) {
339  twk_dial_brnch_name.str("");
340  twk_dial_brnch_name << "twk_" << GSyst::AsString(*it);
341 
342  // access TArrayD memory directly
343  branch_twkdials_array[ip] = new TArrayD(gOptNTwk);
344  branch_twkdials_ptr[ip] = branch_twkdials_array[ip]->GetArray();
345 
346  // create branch
347  wght_tree->Branch(twk_dial_brnch_name.str().c_str(), branch_twkdials_array[ip]);
348  LOG("grwghtnp", pINFO) << "Creating tweak branch : " << twk_dial_brnch_name.str();
349 
350  // set up loading directly into TArrayD
351  for (int i=0; i < n_tweaks; i++) {
352  wght_list[i]->SetBranchAddress(twk_dial_brnch_name.str().c_str(),&branch_twkdials_ptr[ip][i]);
353  //LOG("grwghtnp", pINFO) << "Loading tweak value : "<<branch_twkdials_array[ip]->fArray[i];
354  }
355  }
356 
357  //
358  // CONSOLIDATION LOOP
359  // -- combine all data from reweighting into single file
360  //
361  wght_file->cd();
362  for(int iev = nfirst; iev <= nlast; iev++) {
363  branch_eventnum = iev;
364  for (int itk = 0; itk < n_tweaks; itk++) {
365  wght_list[itk]->GetEntry(iev);
366  } // tweak loop
367  wght_tree->Fill();
368  } // event loop
369  wght_file->cd();
370  wght_tree->Write();
371 
372  //
373  // CLEANUP LOOP
374  //
375 
376  // delete temporary files
377  for (int itk = 0; itk < gOptNTwk; itk++) {
378  tmpName.str("");
379  tmpName << "_temporary_rwght." <<itk <<"." <<gOptRunKey <<".root";
380  if( remove(tmpName.str().c_str()) != 0 )
381  { LOG("grwghtnp", pWARN) << "Could not delete temporary file : " << tmpName.str(); }
382  //else
383  //{ LOG("grwghtnp", pINFO) << "Deleted temporary file : " << tmpName.str(); }
384  }
385 
386  // free memory
387  wght_file->Close();
388  //delete wght_tree;
389  wght_tree = 0;
390  delete wght_file;
391  for (int ipr = 0; ipr < n_params; ipr++) {
392  delete branch_twkdials_array[ipr];
393  }
394 
395  LOG("grwghtnp", pNOTICE) << "Done!";
396  return 0;
397 }
398 //___________________________________________________________________
399 void GetCommandLineArgs(int argc, char ** argv)
400 {
401  LOG("grwghtnp", pINFO) << "*** Parsing command line arguments";
402 
403  CmdLnArgParser parser(argc,argv);
404 
405  // get GENIE event sample
406  if( parser.OptionExists('f') ) {
407  LOG("grwghtnp", pINFO) << "Reading event sample filename";
408  gOptInpFilename = parser.ArgAsString('f');
409  } else {
410  LOG("grwghtnp", pFATAL)
411  << "Unspecified input filename - Exiting";
412  PrintSyntax();
413  exit(1);
414  }
415 
416  // get ROOT covariance matrix binary file
417  if( parser.OptionExists('c') ) {
418  LOG("grwghtnp", pINFO) << "Reading covariance matrix filename";
419  gOptInpCovariance = parser.ArgAsString('c');
420  } else {
421  LOG("grwghtnp", pFATAL)
422  << "Unspecified covariance filename - Exiting";
423  PrintSyntax();
424  exit(1);
425  }
426 
427  // output weight file
428  if(parser.OptionExists('o')) {
429  LOG("grwghtnp", pINFO) << "Reading requested output filename";
430  gOptOutFilename = parser.ArgAsString('o');
431  } else {
432  LOG("grwghtnp", pINFO) << "Setting default output filename";
433  gOptOutFilename = "rwt_cov.root";
434  }
435 
436  if ( parser.OptionExists('n') ) {
437  //
438  LOG("grwghtnp", pINFO) << "Reading number of events to analyze";
439  string nev = parser.ArgAsString('n');
440  if (nev.find(",") != string::npos) {
441  vector<long> vecn = parser.ArgAsLongTokens('n',",");
442  if(vecn.size()!=2) {
443  LOG("grwghtnp", pFATAL) << "Invalid syntax";
444  gAbortingInErr = true;
445  PrintSyntax();
446  exit(1);
447  }
448  // User specified a comma-separated set of values n1,n2.
449  // Use [n1,n2] as the event range to process.
450  gOptNEvt1 = vecn[0];
451  gOptNEvt2 = vecn[1];
452  } else {
453  // User specified a single number n.
454  // Use [0,n] as the event range to process.
455  gOptNEvt1 = -1;
456  gOptNEvt2 = parser.ArgAsLong('n');
457  }
458  } else {
459  LOG("grwghtnp", pINFO)
460  << "Unspecified number of events to analyze - Use all";
461  gOptNEvt1 = -1;
462  gOptNEvt2 = -1;
463  }
464  LOG("grwghtnp", pDEBUG)
465  << "Input event range: " << gOptNEvt1 << ", " << gOptNEvt2;
466 
467  // systematics:
468  if( parser.OptionExists('s') ) {
469  LOG("grwghtnp", pINFO) << "Reading systematics";
470  string insyst = parser.ArgAsString('s');
471  vector<string> lsyst = utils::str::Split(insyst, ",");
473  int ik = 0;
474  for(it=lsyst.begin();it != lsyst.end();it++,ik++)
475  {
476  gOptVSyst.push_back(GSyst::FromString(*it));
477  LOG("grwghtnp",pINFO)<<"Read systematic "<<ik+1<<" : "<< lsyst[ik];
478  }
479  // split into strings of systematics
480  gOptNSyst = gOptVSyst.size();
481  LOG("grwghtnp", pINFO) << "Number of systematics : " << gOptNSyst;
482  } else {
483  LOG("rwghtcov", pFATAL)
484  << "Unspecified systematics - Exiting";
485  PrintSyntax();
486  exit(1);
487  }
488 
489  // systematic central values:
490  if( parser.OptionExists('v') ) {
491  LOG("grwghtnp", pINFO) << "Reading parameter central values";
492  gOptVCentVal = parser.ArgAsDoubleTokens('v',",");
493  // check size
494  if (gOptNSyst != (int)gOptVCentVal.size()) {
495  LOG("rwghtcov", pFATAL)
496  << "Number of systematic central values does not match number of systematics- Exiting";
497  PrintSyntax();
498  exit(1);
499  }
500  } else {
501  LOG("rwghtcov", pFATAL)
502  << "Unspecified systematic central values - Exiting";
503  PrintSyntax();
504  exit(1);
505  }
506 
507  // number of tweaks:
508  if( parser.OptionExists('t') ) {
509  LOG("grwghtnp", pINFO) << "Reading number of tweaks";
510  gOptNTwk = parser.ArgAsInt('t');
511 
512  if( gOptNTwk < 1 )
513  {
514  LOG("grwghtnp", pFATAL) << "Must have at least 1 tweak - Exiting";
515  PrintSyntax();
516  exit(1);
517  }
518  LOG("grwghtnp",pINFO)<<"Number of tweaks : "<< gOptNTwk;
519  } else {
520  LOG("grwghtnp", pFATAL)
521  << "Unspecified tweaks for parameters - Exiting";
522  PrintSyntax();
523  exit(1);
524  }
525 
526  // run key:
527  if( parser.OptionExists('r') ) {
528  LOG("grwghtnp", pINFO) << "Reading run key";
529  gOptRunKey = parser.ArgAsInt('r');
530 
531  LOG("grwghtnp", pINFO)
532  << "Run key set to " <<gOptRunKey;
533  }
534 
535 }
536 //_________________________________________________________________________________
537 void GetEventRange(Long64_t nev_in_file, Long64_t & nfirst, Long64_t & nlast)
538 {
539  nfirst = 0;
540  nlast = 0;
541 
542  if(gOptNEvt1>=0 && gOptNEvt2>=0) {
543  // Input was `-n N1,N2'.
544  // Process events [N1,N2].
545  // Note: Incuding N1 and N2.
546  nfirst = gOptNEvt1;
547  nlast = TMath::Min(nev_in_file-1, gOptNEvt2);
548  }
549  else
550  if(gOptNEvt1<0 && gOptNEvt2>=0) {
551  // Input was `-n N'.
552  // Process first N events [0,N).
553  // Note: Event N is not included.
554  nfirst = 0;
555  nlast = TMath::Min(nev_in_file-1, gOptNEvt2-1);
556  }
557  else
558  if(gOptNEvt1<0 && gOptNEvt2<0) {
559  // No input. Process all events.
560  nfirst = 0;
561  nlast = nev_in_file-1;
562  }
563 
564  assert(nfirst <= nlast && nfirst >= 0 && nlast <= nev_in_file-1);
565 }
566 //_________________________________________________________________________________
567 void GetCorrelationMatrix(string fname, TMatrixD *& cmat)
568 {
569  //
570  // Loads a ROOT file containing only a TMatrixD
571  // Reads a covariance/correlation matrix and returns correlation matrix
572  //
573 
574  // Load covariance matrix diagonal into uncertainties
575  GSystUncertainty * unc = GSystUncertainty::Instance();
578 
579  // open file and prepare for loading
580  TFile * fin = new TFile(fname.c_str(),"READ");
581  TKey * fkey;
582  TMatrixD * inmat = new TMatrixD();
583  if(cmat) { delete cmat; }
584 
585  // read covariance into cmat and return
586  TIter next(fin->GetListOfKeys());
587  fkey = (TKey *)next();
588  fkey->Read(inmat);
589 
590  // checks
591  fin->Close();
592  if (!inmat) {
593  LOG("grwghtnp", pFATAL) << "Error reading covariance matrix - Exiting";
594  gAbortingInErr = true;
595  exit(1);
596  }
597  if (inmat->GetNrows() != inmat->GetNcols()) {
598  LOG("grwghtnp", pFATAL) << "Covariance matrix not square - Exiting";
599  gAbortingInErr = true;
600  exit(1);
601  }
602  if (inmat->GetNrows() != gOptNSyst ){
603  LOG("grwghtnp", pFATAL) << "Number of systematics does not match covariance matrix size- Exiting";
604  gAbortingInErr = true;
605  exit(1);
606  }
607 
608  // Convert covariance to correlation
609  TMatrixD tmpmat = TMatrixD(*inmat);
610 
611  // Diag = Diag(Cov)
612  // set off-diagonals to zero and take 1/square root of diagonals
613  // is there an easier way?
614  int i=0;
615  itd = gOptVCentVal.begin();
616  for (it = gOptVSyst.begin();it != gOptVSyst.end(); it++, itd++, i++) {
617  for(int j=0;j<tmpmat.GetNcols();j++){
618  if(i!=j) { tmpmat(i,j) = 0.; }
619  else {
620  // convert diagonal entries to uncertainty
621  //LOG("grwghtnp", pINFO) <<"Setting uncertainty "<<i<<" to : "<<
622  // TMath::Sqrt(tmpmat(i,i))/(*itd);
623  unc->SetUncertainty(*it,TMath::Sqrt(tmpmat(i,i))/(*itd),
624  TMath::Sqrt(tmpmat(i,i))/(*itd));
625  tmpmat(i,i) = 1./TMath::Sqrt(tmpmat(i,i));
626  }
627  }
628  }
629  //LOG("grwghtnp", pWARN) <<"diagonal scaling matrix:";
630  //tmpmat.Print();
631 
632  // Cor = Diag^(-1/2).Cov.Diag^(-1/2)
633  TMatrixD sigmat = TMatrixD(tmpmat);
634  tmpmat = TMatrixD(*inmat,TMatrixD::kMult,sigmat);
635  cmat = new TMatrixD(sigmat,TMatrixD::kMult,tmpmat);
636 
637  delete inmat;
638  return;
639 }
640 //_________________________________________________________________________________
641 bool FindIncompatibleSystematics(vector<GSyst_t> lsyst)
642 {
643  //
644  // returns false if there are incompatible systematics
645  //
648  // CC QE
649  GSyst_t ccqe_ma_shp[1]
650  = { kXSecTwkDial_MaCCQE };
651  GSyst_t ccqe_ma_shp_norm[2]
653  GSyst_t ccqe_z_shp[5]
657  // CC Res
658  GSyst_t ccres_shp[2]
660  GSyst_t ccres_shp_norm[3]
663  // DIS
664  GSyst_t dis_shp_norm[4]
667  GSyst_t dis_shp[4]
670  // NC Res
671  GSyst_t ncres_shp[2]
673  GSyst_t ncres_shp_norm[3]
676 
677  //
678  // Sort systematics by mode (it0)
679  // Get the list of systematics for that systematic
680  // Look through the rest of systematics the list for others with similar modes (it1)
681  // If it1 conflicts with the group for it0, reject whole list
682  //
683  for(it0=lsyst.begin();it0 != lsyst.end();it0++)
684  {
685  switch(*it0){
686  // CC QE
687  case kXSecTwkDial_MaCCQE:
688  for(it1=it0;it1 != lsyst.end();it1++){
689  for(int i=0;i<2;i++){ if(ccqe_ma_shp_norm[i] == *it1) return false; }
690  for(int i=0;i<5;i++){ if(ccqe_z_shp[i] == *it1) return false; }
691  }
693  case kXSecTwkDial_MaCCQEshape:
694  for(it1=it0;it1 != lsyst.end();it1++){
695  for(int i=0;i<1;i++){ if(ccqe_ma_shp[i] == *it1) return false; }
696  for(int i=0;i<5;i++){ if(ccqe_z_shp[i] == *it1) return false; }
697  }
698  break;
703  case kXSecTwkDial_ZExpA4CCQE:
704  for(it1=it0;it1 != lsyst.end();it1++){
705  for(int i=0;i<1;i++){ if(ccqe_ma_shp[i] == *it1) return false; }
706  for(int i=0;i<2;i++){ if(ccqe_ma_shp_norm[i] == *it1) return false; }
707  }
708  break;
709  // CC RES
711  case kXSecTwkDial_MvCCRES:
712  for(it1=it0;it1 != lsyst.end();it1++){
713  for(int i=0;i<3;i++){ if(ccres_shp_norm[i] == *it1) return false; }
714  }
715  break;
718  case kXSecTwkDial_MvCCRESshape:
719  for(it1=it0;it1 != lsyst.end();it1++){
720  for(int i=0;i<2;i++){ if(ccres_shp[i] == *it1) return false; }
721  }
722  break;
723  // DIS
727  case kXSecTwkDial_CV2uBYshape:
728  for(it1=it0;it1 != lsyst.end();it1++){
729  for(int i=0;i<4;i++){ if(dis_shp_norm[i] == *it1) return false; }
730  }
731  break;
732  case kXSecTwkDial_AhtBY:
733  case kXSecTwkDial_BhtBY:
734  case kXSecTwkDial_CV1uBY:
735  case kXSecTwkDial_CV2uBY:
736  for(it1=it0;it1 != lsyst.end();it1++){
737  for(int i=0;i<4;i++){ if(dis_shp[i] == *it1) return false; }
738  }
739  break;
740  // NC RES
742  case kXSecTwkDial_MvNCRES:
743  for(it1=it0;it1 != lsyst.end();it1++){
744  for(int i=0;i<3;i++){ if(ncres_shp_norm[i] == *it1) return false; }
745  }
746  break;
749  case kXSecTwkDial_MvNCRESshape:
750  for(it1=it0;it1 != lsyst.end();it1++){
751  for(int i=0;i<2;i++){ if(ncres_shp[i] == *it1) return false; }
752  }
753  break;
754  default: // no conflicts
755  break;
756  }
757  }
758  return true;
759 
760 }
761 //_________________________________________________________________________________
762 void AdoptWeightCalcs (vector<GSyst_t> lsyst, GReWeight & rw)
763 {
764  //
765  // Sets of systematics can be incompatible because they request different
766  // reweighting modes. If one of these systematics is requested, adopt a
767  // reweighting calculator and set it to the appropriate mode.
768  //
770  for(it=lsyst.begin();it != lsyst.end();it++)
771  {
772  switch(*it){
773  // CC QE
774  case kXSecTwkDial_MaCCQE:
777  if ( ! rw.WghtCalc("xsec_ccqe") ){
778  LOG("grwghtnp", pNOTICE) << "Adopting xsec_ccqe weight calc";
779  rw.AdoptWghtCalc( "xsec_ccqe", new GReWeightNuXSecCCQE );
780  GReWeightNuXSecCCQE * rwccqe =
781  dynamic_cast<GReWeightNuXSecCCQE *> (rw.WghtCalc("xsec_ccqe"));
782  if (*it == kXSecTwkDial_MaCCQE) {
783  rwccqe->SetMode(GReWeightNuXSecCCQE::kModeMa); }
784  else { rwccqe->SetMode(GReWeightNuXSecCCQE::kModeNormAndMaShape); }
785  }
786  break;
792  if ( ! rw.WghtCalc("xsec_ccqe") ){
793  LOG("grwghtnp", pNOTICE) << "Adopting xsec_ccqe weight calc";
794  rw.AdoptWghtCalc( "xsec_ccqe", new GReWeightNuXSecCCQE );
795  GReWeightNuXSecCCQE * rwccqe =
796  dynamic_cast<GReWeightNuXSecCCQE *> (rw.WghtCalc("xsec_ccqe"));
797  rwccqe->SetMode(GReWeightNuXSecCCQE::kModeZExp);
798  }
799  break;
800  // CC Res
803  if ( ! rw.WghtCalc("xsec_ccres") ){
804  LOG("grwghtnp", pNOTICE) << "Adopting xsec_ccres weight calc";
805  rw.AdoptWghtCalc( "xsec_ccres", new GReWeightNuXSecCCRES );
806  GReWeightNuXSecCCRES * rwccres =
807  dynamic_cast<GReWeightNuXSecCCRES *> (rw.WghtCalc("xsec_ccres"));
808  rwccres->SetMode(GReWeightNuXSecCCRES::kModeMaMv);
809  }
810  break;
814  if ( ! rw.WghtCalc("xsec_ccres") ){
815  LOG("grwghtnp", pNOTICE) << "Adopting xsec_ccres weight calc";
816  rw.AdoptWghtCalc( "xsec_ccres", new GReWeightNuXSecCCQE );
817  GReWeightNuXSecCCQE * rwccres =
818  dynamic_cast<GReWeightNuXSecCCQE *> (rw.WghtCalc("xsec_ccres"));
819  rwccres->SetMode(GReWeightNuXSecCCRES::kModeNormAndMaMvShape);
820  }
821  break;
822  // DIS
827  if ( ! rw.WghtCalc("xsec_dis") ){
828  LOG("grwghtnp", pNOTICE) << "Adopting xsec_dis weight calc";
829  rw.AdoptWghtCalc( "xsec_dis", new GReWeightNuXSecDIS );
830  GReWeightNuXSecDIS * rwdis =
831  dynamic_cast<GReWeightNuXSecDIS *> (rw.WghtCalc("xsec_dis"));
832  rwdis->SetMode(GReWeightNuXSecDIS::kModeABCV12uShape);
833  }
834  break;
835  case kXSecTwkDial_AhtBY:
836  case kXSecTwkDial_BhtBY:
837  case kXSecTwkDial_CV1uBY:
838  case kXSecTwkDial_CV2uBY:
839  if ( ! rw.WghtCalc("xsec_dis") ){
840  LOG("grwghtnp", pNOTICE) << "Adopting xsec_dis weight calc";
841  rw.AdoptWghtCalc( "xsec_dis", new GReWeightNuXSecDIS );
842  GReWeightNuXSecDIS * rwdis =
843  dynamic_cast<GReWeightNuXSecDIS *> (rw.WghtCalc("xsec_dis"));
844  rwdis->SetMode(GReWeightNuXSecDIS::kModeABCV12u);
845  }
846  break;
847  // NC Res
850  if ( ! rw.WghtCalc("xsec_ncres") ){
851  LOG("grwghtnp", pNOTICE) << "Adopting xsec_ncres weight calc";
852  rw.AdoptWghtCalc( "xsec_ncres", new GReWeightNuXSecNCRES );
853  GReWeightNuXSecNCRES * rwncres =
854  dynamic_cast<GReWeightNuXSecNCRES *> (rw.WghtCalc("xsec_ncres"));
855  rwncres->SetMode(GReWeightNuXSecNCRES::kModeMaMv);
856  }
857  break;
861  if ( ! rw.WghtCalc("xsec_ncres") ){
862  LOG("grwghtnp", pNOTICE) << "Adopting xsec_ncres weight calc";
863  rw.AdoptWghtCalc( "xsec_ncres", new GReWeightNuXSecNCRES );
864  GReWeightNuXSecNCRES * rwncres =
865  dynamic_cast<GReWeightNuXSecNCRES *> (rw.WghtCalc("xsec_ncres"));
866  rwncres->SetMode(GReWeightNuXSecNCRES::kModeNormAndMaMvShape);
867  }
868  break;
869  default: // no fine-tuning needed
870  break;
871  }
872  }
873 }
874 //_________________________________________________________________________________
875 void PrintSyntax(void)
876 {
877  LOG("grwghtnp", pFATAL)
878  << "\n\n"
879  << "grwghtnp \n"
880  << " -f input_event_file \n"
881  << " -c input_covariance_file\n"
882  << " -t num_twk \n"
883  << " -s syst1[,syst2[,...]] \n"
884  << " -v cval1[,cval2[,...]] \n"
885  << " [-n n1[,n2]] \n"
886  << " [-r run_key] \n"
887  << " [-o output_weights_file]";
888 }
889 //_________________________________________________________________________________
Reweighting GENIE DIS neutrino-nucleus cross sections.
tweak Mv CCRES, affects d2sigma(CCRES)/dWdQ2 both in shape and normalization
Definition: GSyst.h:59
Long64_t gOptNEvt2
tweak Z-expansion coefficient 1, affects dsigma(CCQE)/dQ2 both in shape and normalization ...
Definition: GSyst.h:157
Resonance_t FromString(const char *res)
string -> resonance id
long ArgAsLong(char opt)
Basic constants.
tweak Mv CCRES, affects d2sigma(CCRES)/dWdQ2 in shape only (normalized to constant integral) ...
Definition: GSyst.h:57
string ArgAsString(char opt)
#include "Numerical/GSFunc.h"
Definition: AlgCmp.h:26
vector< GSyst_t > gOptVSyst
string gOptOutFilename
intermediate_table::iterator iterator
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.
string gOptInpFilename
Reweighting CCQE GENIE neutrino cross sections.
#define pFATAL
Definition: Messenger.h:47
Long64_t gOptNEvt1
vector< double > gOptVCentVal
tweak CCQE normalization (energy independent)
Definition: GSyst.h:49
vector< long > ArgAsLongTokens(char opt, string delimeter)
tweak the Bodek-Yang model parameter B_{ht} - shape only effect to d2sigma(DIS)/dxdy ...
Definition: GSyst.h:91
Reweight GENIE NC resonance neutrino-production cross section. Basically a clone of the corresponding...
std::string AsString(TrkPoint_t trkpt)
void SetUncertainty(GSyst_t syst, double plus_err, double minus_err)
void Reconfigure(void)
reconfigure weight calculators with new params
Definition: GReWeight.cxx:74
Simple mathematical utilities not found in ROOT&#39;s TMath.
TMatrixD CholeskyDecomposition(const TMatrixD &cov)
Definition: MathUtils.cxx:27
GReWeightI * WghtCalc(string name)
access a weight calculator by name
Definition: GReWeight.cxx:61
void Set(GSyst_t syst, double current_value)
Definition: GSystSet.cxx:89
tweak Z-expansion coefficient 4, affects dsigma(CCQE)/dQ2 both in shape and normalization ...
Definition: GSyst.h:160
void GetEventRange(Long64_t nev_in_file, Long64_t &nfirst, Long64_t &nlast)
#define LOG(stream, priority)
A macro that returns the requested log4cpp::Category appending a string (using the FILE...
Definition: Messenger.h:87
tweak Z-expansion coefficient 3, affects dsigma(CCQE)/dQ2 both in shape and normalization ...
Definition: GSyst.h:159
vector< double > ArgAsDoubleTokens(char opt, string delimeter)
tweak the Bodek-Yang model parameter CV2u - incl. both shape and normalization effect ...
Definition: GSyst.h:89
tweak Z-expansion coefficient 2, affects dsigma(CCQE)/dQ2 both in shape and normalization ...
Definition: GSyst.h:158
MINOS-style Ntuple Class to hold an output MC Tree Header.
tweak Z-expansion CCQE normalization (energy independent)
Definition: GSyst.h:156
An enumeration of systematic parameters.
#define pINFO
Definition: Messenger.h:53
#define pWARN
Definition: Messenger.h:51
void AdoptWghtCalc(string name, GReWeightI *wcalc)
add concrete weight calculator, transfers ownership
Definition: GReWeight.cxx:54
string gOptInpCovariance
void PrintSyntax()
Generated Event Record. It is a GHepRecord object that can accept / be visited by EventRecordVisitorI...
Definition: EventRecord.h:38
tweak CCRES normalization
Definition: GSyst.h:55
tweak Mv NCRES, affects d2sigma(NCRES)/dWdQ2 both in shape and normalization
Definition: GSyst.h:64
tweak the Bodek-Yang model parameter A_{ht} - incl. both shape and normalization effect ...
Definition: GSyst.h:86
bool FindIncompatibleSystematics(vector< GSyst_t > lsyst)
tweak the Bodek-Yang model parameter A_{ht} - shape only effect to d2sigma(DIS)/dxdy ...
Definition: GSyst.h:90
vector< string > Split(string input, string delim)
Definition: StringUtils.cxx:42
tweak Ma CCRES, affects d2sigma(CCRES)/dWdQ2 both in shape and normalization
Definition: GSyst.h:58
tweak NCRES normalization
Definition: GSyst.h:60
TRandom * tRnd
tweak Ma CCQE, affects dsigma(CCQE)/dQ2 both in shape and normalization
Definition: GSyst.h:52
tweak the Bodek-Yang model parameter CV1u - shape only effect to d2sigma(DIS)/dxdy ...
Definition: GSyst.h:92
tweak Ma CCRES, affects d2sigma(CCRES)/dWdQ2 in shape only (normalized to constant integral) ...
Definition: GSyst.h:56
void GetCorrelationMatrix(string fname, TMatrixD *&cmat)
double CalcWeight(const genie::EventRecord &event)
calculate weight for input event
Definition: GReWeight.cxx:99
TVectorD CholeskyGenerateCorrelatedParamVariations(const TMatrixD &Lch)
Definition: MathUtils.cxx:172
tweak the Bodek-Yang model parameter CV1u - incl. both shape and normalization effect ...
Definition: GSyst.h:88
Reweight GENIE CC resonance neutrino-production.
Command line argument parser.
#define pNOTICE
Definition: Messenger.h:52
tweak Ma NCRES, affects d2sigma(NCRES)/dWdQ2 in shape only (normalized to constant integral) ...
Definition: GSyst.h:61
GSystSet & Systematics(void)
set of enabled systematic params & values
Definition: GReWeight.cxx:69
void Clear(Option_t *opt="")
tweak Mv NCRES, affects d2sigma(NCRES)/dWdQ2 in shape only (normalized to constant integral) ...
Definition: GSyst.h:62
bool gAbortingInErr
Definition: Messenger.cxx:56
int main(int argc, char **argv)
tweak the Bodek-Yang model parameter CV2u - shape only effect to d2sigma(DIS)/dxdy ...
Definition: GSyst.h:93
Set of systematics to be considered by the reweighting package.
Definition: GSystSet.h:37
tweak Ma CCQE, affects dsigma(CCQE)/dQ2 in shape only (normalized to constant integral) ...
Definition: GSyst.h:51
bool OptionExists(char opt)
was option set?
Interface to the GENIE event reweighting engines.
Definition: GReWeight.h:40
EventRecord * event
event
Event finding and building.
tweak the Bodek-Yang model parameter B_{ht} - incl. both shape and normalization effect ...
Definition: GSyst.h:87
void AdoptWeightCalcs(vector< GSyst_t > lsyst, GReWeight &rw)
#define pDEBUG
Definition: Messenger.h:54
tweak Ma NCRES, affects d2sigma(NCRES)/dWdQ2 both in shape and normalization
Definition: GSyst.h:63
void GetCommandLineArgs(int argc, char **argv)