All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
generate_weightcalc.py
Go to the documentation of this file.
1 #script to generate skeleton function for calculating weight
2 #usage:
3 # generate_weightcalc.py FUNCNAME
4 #
5 #output:
6 # script creates FUNCNAMEWeightCalc.cxx
7 #
8 #to run your code, put it in generated cxx file
9 #and add the config to fcl file:
10 
11 import os
12 import sys, getopt
13 
14 def main(argv):
15  funcname = ''
16 
17  try:
18  opts, args = getopt.getopt(argv,"hf:",["funcname="])
19  except getopt.GetoptError:
20  print 'generate_weightcalc.py -f FUNCNAME'
21  sys.exit(2)
22  for opt, arg in opts:
23  if opt == '-h':
24  print 'generate_weightcalc.py -f FUNCNAME'
25  sys.exit()
26  elif opt == '-f':
27  funcname = arg
28  else:
29  print 'option not recognized'
30  sys.exit(2)
31 
32  if ( funcname=='' ):
33  print "You must specify function name with option -f!"
34  sys.exit(2)
35 
36  filename="".join(x for x in funcname if x.isalnum())
37  filename=filename+"WeightCalc.cxx"
38 
39  if (os.path.isfile(filename)):
40  print "File",filename,"already exists!"
41  sys.exit(2)
42 
43  print 'Genarating function', filename
44 
45  ofstr='''
46 #include \"larsim/EventWeight/Base/WeightCalcCreator.h\"
47 #include \"larsim/EventWeight/Base/WeightCalc.h\"
48 
49 #include "art/Framework/Services/Registry/ServiceHandle.h"
50 #include "art/Framework/Services/Optional/RandomNumberGenerator.h"
51 
52 #include "CLHEP/Random/RandGaussQ.h"
53 
54 namespace evwgh {
55  class %(funcname)sWeightCalc : public WeightCalc
56  {
57  public:
58  %(funcname)sWeightCalc();
59  void Configure(fhicl::ParameterSet const& p);
60  std::vector<std::vector<double> > GetWeight(art::Event & e);
61  private:
62  CLHEP::RandGaussQ *fGaussRandom;
63 
64  DECLARE_WEIGHTCALC(%(funcname)sWeightCalc)
65  };
66  %(funcname)sWeightCalc::%(funcname)sWeightCalc()
67  {
68  }
69 
70  void %(funcname)sWeightCalc::Configure(fhicl::ParameterSet const& p)
71  {
72  //get configuration for this function
73  fhicl::ParameterSet const &pset=p.get<fhicl::ParameterSet> (GetName());
74 
75  //Prepare random generator
76  art::ServiceHandle<art::RandomNumberGenerator> rng;
77  fGaussRandom = new CLHEP::RandGaussQ(rng->getEngine(GetName()));
78  }
79 
80  std::vector<std::vector<double> > %(funcname)sWeightCalc::GetWeight(art::Event & e)
81  {
82  //calculate weight(s) here
83  std::vector<std::vector<double> > weight;
84  return weight;
85  }
86  REGISTER_WEIGHTCALC(%(funcname)sWeightCalc)
87 }
88 '''%{'funcname':funcname}
89 
90  of=open(filename,'w')
91 
92  of.write(ofstr)
93  of.close()
94 
95 if __name__ == "__main__":
96  main(sys.argv[1:])
97 
98 
S join(S const &sep, Coll const &s)
Returns a concatenation of strings in s separated by sep.
open(RACETRACK) or die("Could not open file $RACETRACK for writing")