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