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