CMS 3D CMS Logo

RivetAnalyzer.cc
Go to the documentation of this file.
2 
5 
8 
9 #include "Rivet/AnalysisHandler.hh"
10 #include "Rivet/Analysis.hh"
11 
12 using namespace Rivet;
13 using namespace edm;
14 
16  : _analysisHandler(),
17  _isFirstEvent(true),
18  _outFileName(pset.getParameter<std::string>("OutputFile")),
19  //decide whether to finlaize tthe plots or not.
20  //deciding not to finalize them can be useful for further harvesting of many jobs
21  _doFinalize(pset.getParameter<bool>("DoFinalize")),
22  _produceDQM(pset.getParameter<bool>("ProduceDQMOutput")),
23  _xsection(-1.) {
24  usesResource("Rivet");
25 
26  //retrive the analysis name from paarmeter set
27  std::vector<std::string> analysisNames = pset.getParameter<std::vector<std::string> >("AnalysisNames");
28 
29  _hepmcCollection = consumes<HepMCProduct>(pset.getParameter<edm::InputTag>("HepMCCollection"));
30 
31  _useExternalWeight = pset.getParameter<bool>("UseExternalWeight");
32  if (_useExternalWeight) {
33  if (!pset.exists("GenEventInfoCollection")) {
34  throw cms::Exception("RivetAnalyzer") << "when using an external event weight you have to specify the "
35  "GenEventInfoProduct collection from which the weight has to be taken ";
36  }
37 
38  _genEventInfoCollection = consumes<GenEventInfoProduct>(pset.getParameter<edm::InputTag>("GenEventInfoCollection"));
39  _useGENweights = pset.getParameter<bool>("useGENweights");
40  _GENweightNumber = pset.getParameter<int>("GENweightNumber");
41  _LHECollection = consumes<LHEEventProduct>(pset.getParameter<edm::InputTag>("LHECollection"));
42  _useLHEweights = pset.getParameter<bool>("useLHEweights");
43  _LHEweightNumber = pset.getParameter<int>("LHEweightNumber");
44  }
45 
46  //get the analyses
47  _analysisHandler.addAnalyses(analysisNames);
48 
49  //go through the analyses and check those that need the cross section
50  const std::set<AnaHandle, CmpAnaHandle>& analyses = _analysisHandler.analyses();
51 
52  std::set<AnaHandle, CmpAnaHandle>::const_iterator ibeg = analyses.begin();
53  std::set<AnaHandle, CmpAnaHandle>::const_iterator iend = analyses.end();
54  std::set<AnaHandle, CmpAnaHandle>::const_iterator iana;
55  _xsection = pset.getParameter<double>("CrossSection");
56  for (iana = ibeg; iana != iend; ++iana) {
57  if ((*iana)->needsCrossSection())
58  (*iana)->setCrossSection(_xsection);
59  }
60  if (_produceDQM) {
61  // book stuff needed for DQM
62  dbe = nullptr;
64  dbe->setVerbose(50);
65  }
66 }
67 
69 
71  //set the environment, very ugly but rivet is monolithic when it comes to paths
72  char* cmsswbase = getenv("CMSSW_BASE");
73  char* cmsswrelease = getenv("CMSSW_RELEASE_BASE");
74  if (!getenv("RIVET_REF_PATH")) {
75  const std::string rivetref = "RIVET_REF_PATH=" + string(cmsswbase) +
76  "/src/GeneratorInterface/RivetInterface/data:" + string(cmsswrelease) +
77  "/src/GeneratorInterface/RivetInterface/data";
78  char* rivetrefCstr = strdup(rivetref.c_str());
79  putenv(rivetrefCstr);
80  free(rivetrefCstr);
81  }
82  if (!getenv("RIVET_INFO_PATH")) {
83  const std::string rivetinfo = "RIVET_INFO_PATH=" + string(cmsswbase) +
84  "/src/GeneratorInterface/RivetInterface/data:" + string(cmsswrelease) +
85  "/src/GeneratorInterface/RivetInterface/data";
86  char* rivetinfoCstr = strdup(rivetinfo.c_str());
87  putenv(rivetinfoCstr);
88  free(rivetinfoCstr);
89  }
90 }
91 
92 void RivetAnalyzer::beginRun(const edm::Run& iRun, const edm::EventSetup& iSetup) { return; }
93 
95  //get the hepmc product from the event
97  iEvent.getByToken(_hepmcCollection, evt);
98 
99  // get HepMC GenEvent
100  const HepMC::GenEvent* myGenEvent = evt->GetEvent();
101  std::unique_ptr<HepMC::GenEvent> tmpGenEvtPtr;
102  //if you want to use an external weight or set the cross section we have to clone the GenEvent and change the weight
103  if (_useExternalWeight || _xsection > 0) {
104  tmpGenEvtPtr = std::make_unique<HepMC::GenEvent>(*(evt->GetEvent()));
105 
106  if (_xsection > 0) {
107  HepMC::GenCrossSection xsec;
108  xsec.set_cross_section(_xsection);
109  tmpGenEvtPtr->set_cross_section(xsec);
110  }
111 
112  if (_useExternalWeight) {
113  if (tmpGenEvtPtr->weights().empty()) {
114  throw cms::Exception("RivetAnalyzer") << "Original weight container has 0 size ";
115  }
116  if (tmpGenEvtPtr->weights().size() > 1) {
117  edm::LogWarning("RivetAnalyzer") << "Original event weight size is " << tmpGenEvtPtr->weights().size()
118  << ". Will change only the first one ";
119  }
120 
121  edm::Handle<GenEventInfoProduct> genEventInfoProduct;
122  iEvent.getByToken(_genEventInfoCollection, genEventInfoProduct);
123  double weightForRivet = genEventInfoProduct->weight();
124 
125  if (_useGENweights) {
126  weightForRivet = genEventInfoProduct->weights().at(_GENweightNumber);
127  }
128  if (_useLHEweights) {
129  edm::Handle<LHEEventProduct> lheEventHandle;
130  iEvent.getByToken(_LHECollection, lheEventHandle);
131  weightForRivet *= lheEventHandle->weights().at(_LHEweightNumber).wgt / lheEventHandle->originalXWGTUP();
132  }
133 
134  tmpGenEvtPtr->weights()[0] = weightForRivet;
135  }
136  myGenEvent = tmpGenEvtPtr.get();
137  }
138 
139  //aaply the beams initialization on the first event
140  if (_isFirstEvent) {
141  _analysisHandler.init(*myGenEvent);
142  _isFirstEvent = false;
143  }
144 
145  //run the analysis
146  _analysisHandler.analyze(*myGenEvent);
147 }
148 
149 void RivetAnalyzer::endRun(const edm::Run& iRun, const edm::EventSetup& iSetup) {
150  if (_doFinalize)
151  _analysisHandler.finalize();
152  else {
153  //if we don't finalize we just want to do the transformation from histograms to DPS
155  //normalizeTree();
156  }
157  _analysisHandler.writeData(_outFileName);
158 
159  return;
160 }
161 
162 //from Rivet 2.X: Analysis.hh (cls 18Feb2014)
164 //const vector<AnalysisObjectPtr>& analysisObjects() const {
165 //return _analysisobjects;
166 //}
167 
169 
171  using namespace YODA;
172  std::vector<string> analyses = _analysisHandler.analysisNames();
173 
174  //tree.ls(".", true);
175  const string tmpdir = "/RivetNormalizeTmp";
176  //tree.mkdir(tmpdir);
177  foreach (const string& analysis, analyses) {
178  if (_produceDQM) {
179  dbe->setCurrentFolder("Rivet/" + analysis);
180  //global variables that are always present
181  //sumOfWeights
182  TH1F nevent("nEvt", "n analyzed Events", 1, 0., 1.);
183  nevent.SetBinContent(1, _analysisHandler.sumOfWeights());
184  _mes.push_back(dbe->book1D("nEvt", &nevent));
185  }
186  //cross section
187  //TH1F xsection("xSection", "Cross Section", 1, 0., 1.);
188  //xsection.SetBinContent(1,_analysisHandler.crossSection());
189  //_mes.push_back(dbe->book1D("xSection",&xsection));
190  //now loop over the histograms
191 
192  /*
193  const vector<string> paths = tree.listObjectNames("/"+analysis, true); // args set recursive listing
194  std::cout << "Number of objects in YODA tree for analysis " << analysis << " = " << paths.size() << std::endl;
195  foreach (const string& path, paths) {
196  IManagedObject* hobj = tree.find(path);
197  if (hobj) {
198  // Weird seg fault on SLC4 when trying to dyn cast an IProfile ptr to a IHistogram
199  // Fix by attempting to cast to IProfile first, only try IHistogram if it fails.
200  IHistogram1D* histo = 0;
201  IProfile1D* prof = dynamic_cast<IProfile1D*>(hobj);
202  if (!prof) histo = dynamic_cast<IHistogram1D*>(hobj);
203 
204  std::cout << "Converting histo " << path << " to DPS" << std::endl;
205  tree.mv(path, tmpdir);
206  const size_t lastslash = path.find_last_of("/");
207  const string basename = path.substr(lastslash+1, path.length() - (lastslash+1));
208  const string tmppath = tmpdir + "/" + basename;
209 
210  // If it's a normal histo:
211  if (histo) {
212  IHistogram1D* tmphisto = dynamic_cast<IHistogram1D*>(tree.find(tmppath));
213  if (tmphisto) {
214  _analysisHandler.datapointsetFactory().create(path, *tmphisto);
215  }
216  //now convert to root and then ME
217  //need aida2flat (from Rivet 1.X) & flat2root here
218  TH1F* h = aida2root<IHistogram1D, TH1F>(histo, basename);
219  if (_produceDQM)
220  _mes.push_back(dbe->book1D(h->GetName(), h));
221  delete h;
222  tree.rm(tmppath);
223  }
224  // If it's a profile histo:
225  else if (prof) {
226  IProfile1D* tmpprof = dynamic_cast<IProfile1D*>(tree.find(tmppath));
227  if (tmpprof) {
228  _analysisHandler.datapointsetFactory().create(path, *tmpprof);
229  }
230  //now convert to root and then ME
231  //need aida2flat (from Rivet 1.X) & flat2root here
232  TProfile* p = aida2root<IProfile1D, TProfile>(prof, basename);
233  if (_produceDQM)
234  _mes.push_back(dbe->bookProfile(p->GetName(), p));
235  delete p;
236  tree.rm(tmppath);
237  }
238  }
239  }
240  */
241  }
242  //tree.rmdir(tmpdir);
243 }
244 
void analyze(const edm::Event &, const edm::EventSetup &) override
double originalXWGTUP() const
T getParameter(std::string const &) const
void endRun(const edm::Run &, const edm::EventSetup &) override
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
bool exists(std::string const &parameterName) const
checks if a parameter exists
double weight() const
int nevent
Definition: AMPTWrapper.h:74
const std::vector< WGT > & weights() const
Rivet::AnalysisHandler _analysisHandler
Definition: RivetAnalyzer.h:48
int iEvent
Definition: GenABIO.cc:224
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
void beginJob() override
void beginRun(const edm::Run &, const edm::EventSetup &) override
RivetAnalyzer(const edm::ParameterSet &)
edm::EDGetTokenT< edm::HepMCProduct > _hepmcCollection
Definition: RivetAnalyzer.h:40
const HepMC::GenEvent * GetEvent() const
Definition: HepMCProduct.h:38
double _xsection
Definition: RivetAnalyzer.h:53
DQMStore * dbe
Definition: RivetAnalyzer.h:55
std::string _outFileName
Definition: RivetAnalyzer.h:50
HLT enums.
void endJob() override
List of registered analysis data objects.
edm::EDGetTokenT< LHEEventProduct > _LHECollection
Definition: RivetAnalyzer.h:46
std::vector< double > & weights()
bool _useExternalWeight
Definition: RivetAnalyzer.h:41
edm::EDGetTokenT< GenEventInfoProduct > _genEventInfoCollection
Definition: RivetAnalyzer.h:47
void normalizeTree()
Definition: Run.h:45
std::vector< MonitorElement * > _mes
Definition: RivetAnalyzer.h:56
~RivetAnalyzer() override