test
CMS 3D CMS Logo

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