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 
6 
11 
12 #include "Rivet/AnalysisHandler.hh"
13 #include "Rivet/Analysis.hh"
14 
15 #include <string>
16 #include <vector>
17 #include <iostream>
18 #include <cstdlib>
19 #include <cstring>
20 
21 using namespace Rivet;
22 using namespace edm;
23 
25 _analysisHandler(),
26 _isFirstEvent(true),
27 _outFileName(pset.getParameter<std::string>("OutputFile")),
28 //decide whether to finlaize tthe plots or not.
29 //deciding not to finalize them can be useful for further harvesting of many jobs
30 _doFinalize(pset.getParameter<bool>("DoFinalize")),
31 _produceDQM(pset.getParameter<bool>("ProduceDQMOutput"))
32 {
33  //retrive the analysis name from paarmeter set
34  std::vector<std::string> analysisNames = pset.getParameter<std::vector<std::string> >("AnalysisNames");
35 
36  _hepmcCollection = pset.getParameter<edm::InputTag>("HepMCCollection");
37 
38  _useExternalWeight = pset.getParameter<bool>("UseExternalWeight");
39  if (_useExternalWeight) {
40  if (!pset.exists("GenEventInfoCollection")){
41  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 " ;
42  }
43  _genEventInfoCollection = pset.getParameter<edm::InputTag>("GenEventInfoCollection");
44  _LHECollection = pset.getParameter<edm::InputTag>("LHECollection");
45  _useLHEweights = pset.getParameter<bool>("useLHEweights");
46  _LHEweightNumber = pset.getParameter<int>("LHEweightNumber");
47 
48  }
49 
50  //get the analyses
51  _analysisHandler.addAnalyses(analysisNames);
52 
53  //go through the analyses and check those that need the cross section
54  const std::set< AnaHandle, CmpAnaHandle > & analyses = _analysisHandler.analyses();
55 
56  std::set< AnaHandle, CmpAnaHandle >::const_iterator ibeg = analyses.begin();
57  std::set< AnaHandle, CmpAnaHandle >::const_iterator iend = analyses.end();
58  std::set< AnaHandle, CmpAnaHandle >::const_iterator iana;
59  double xsection = -1.;
60  xsection = pset.getParameter<double>("CrossSection");
61  for (iana = ibeg; iana != iend; ++iana){
62  if ((*iana)->needsCrossSection())
63  (*iana)->setCrossSection(xsection);
64  }
65  if (_produceDQM){
66  // book stuff needed for DQM
67  dbe = 0;
69  dbe->setVerbose(50);
70  }
71 
72 }
73 
75 }
76 
78  //set the environment, very ugly but rivet is monolithic when it comes to paths
79  char * cmsswbase = getenv("CMSSW_BASE");
80  char * cmsswrelease = getenv("CMSSW_RELEASE_BASE");
81  std::string rivetref, rivetinfo;
82  rivetref = "RIVET_REF_PATH=" + string(cmsswbase) + "/src/GeneratorInterface/RivetInterface/data:" + string(cmsswrelease) + "/src/GeneratorInterface/RivetInterface/data";
83  rivetinfo = "RIVET_INFO_PATH=" + string(cmsswbase) + "/src/GeneratorInterface/RivetInterface/data:" + string(cmsswrelease) + "/src/GeneratorInterface/RivetInterface/data";
84  putenv(strdup(rivetref.c_str()));
85  putenv(strdup(rivetinfo.c_str()));
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.getByLabel(_hepmcCollection, evt);
97 
98  // get HepMC GenEvent
99  const HepMC::GenEvent *myGenEvent = evt->GetEvent();
100 
101  //if you want to use an external weight we have to clone the GenEvent and change the weight
102  if ( _useExternalWeight ){
103 
104  HepMC::GenEvent * tmpGenEvtPtr = new HepMC::GenEvent( *(evt->GetEvent()) );
105  if (tmpGenEvtPtr->weights().size() == 0) {
106  throw cms::Exception("RivetAnalyzer") << "Original weight container has 0 size ";
107  }
108  if (tmpGenEvtPtr->weights().size() > 1) {
109  edm::LogWarning("RivetAnalyzer") << "Original event weight size is " << tmpGenEvtPtr->weights().size() << ". Will change only the first one ";
110  }
111 
112  if(!_useLHEweights){
113  edm::Handle<GenEventInfoProduct> genEventInfoProduct;
114  iEvent.getByLabel(_genEventInfoCollection, genEventInfoProduct);
115  tmpGenEvtPtr->weights()[0] = genEventInfoProduct->weight();
116  }else{
117  edm::Handle<LHEEventProduct> lheEventHandle;
118  iEvent.getByLabel(_LHECollection,lheEventHandle);
119  const LHEEventProduct::WGT& wgt = lheEventHandle->weights().at(_LHEweightNumber);
120  tmpGenEvtPtr->weights()[0] = wgt.wgt;
121  }
122  myGenEvent = tmpGenEvtPtr;
123 
124  }
125 
126 
127  //aaply the beams initialization on the first event
128  if (_isFirstEvent){
129  _analysisHandler.init(*myGenEvent);
130  _isFirstEvent = false;
131  }
132 
133  //run the analysis
134  _analysisHandler.analyze(*myGenEvent);
135 
136  //if we have cloned the GenEvent, we delete it
137  if ( _useExternalWeight )
138  delete myGenEvent;
139 }
140 
141 
142 void RivetAnalyzer::endRun(const edm::Run& iRun,const edm::EventSetup& iSetup){
143  if (_doFinalize)
144  _analysisHandler.finalize();
145  else {
146  //if we don't finalize we just want to do the transformation from histograms to DPS
148  //normalizeTree();
149 
150  }
151  _analysisHandler.writeData(_outFileName);
152 
153  return;
154 }
155 
156 
157 
158 //from Rivet 2.X: Analysis.hh (cls 18Feb2014)
160 //const vector<AnalysisObjectPtr>& analysisObjects() const {
161 //return _analysisobjects;
162 //}
163 
164 
165 
167 }
168 
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).c_str());
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 }
245 
246 
247 
T getParameter(std::string const &) const
virtual void beginRun(const edm::Run &, const edm::EventSetup &)
virtual void endRun(const edm::Run &, const edm::EventSetup &)
virtual void analyze(const edm::Event &, const edm::EventSetup &)
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
bool exists(std::string const &parameterName) const
checks if a parameter exists
edm::InputTag _hepmcCollection
Definition: RivetAnalyzer.h:48
int nevent
Definition: AMPTWrapper.h:74
Rivet::AnalysisHandler _analysisHandler
Definition: RivetAnalyzer.h:54
int iEvent
Definition: GenABIO.cc:230
RivetAnalyzer(const edm::ParameterSet &)
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:413
DQMStore * dbe
Definition: RivetAnalyzer.h:60
std::string _outFileName
Definition: RivetAnalyzer.h:56
edm::InputTag _LHECollection
Definition: RivetAnalyzer.h:52
bool _useExternalWeight
Definition: RivetAnalyzer.h:49
edm::InputTag _genEventInfoCollection
Definition: RivetAnalyzer.h:53
virtual ~RivetAnalyzer()
virtual void beginJob()
void normalizeTree()
Definition: Run.h:41
virtual void endJob()
List of registered analysis data objects.
std::vector< MonitorElement * > _mes
Definition: RivetAnalyzer.h:61