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 
10 
11 #include "Rivet/AnalysisHandler.hh"
12 #include "Rivet/Analysis.hh"
13 
14 #include <string>
15 #include <vector>
16 #include <iostream>
17 #include <cstdlib>
18 #include <cstring>
19 
20 using namespace Rivet;
21 using namespace edm;
22 
24 _analysisHandler(),
25 _isFirstEvent(true),
26 _outFileName(pset.getParameter<std::string>("OutputFile")),
27 //decide whether to finlaize tthe plots or not.
28 //deciding not to finalize them can be useful for further harvesting of many jobs
29 _doFinalize(pset.getParameter<bool>("DoFinalize")),
30 _produceDQM(pset.getParameter<bool>("ProduceDQMOutput"))
31 {
32  //retrive the analysis name from paarmeter set
33  std::vector<std::string> analysisNames = pset.getParameter<std::vector<std::string> >("AnalysisNames");
34 
35  _hepmcCollection = pset.getParameter<edm::InputTag>("HepMCCollection");
36 
37  _useExternalWeight = pset.getParameter<bool>("UseExternalWeight");
38  if (_useExternalWeight) {
39  if (!pset.exists("GenEventInfoCollection")){
40  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 " ;
41  }
42  _genEventInfoCollection = pset.getParameter<edm::InputTag>("GenEventInfoCollection");
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  double xsection = -1.;
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  std::string rivetref, rivetinfo;
77  rivetref = "RIVET_REF_PATH=" + string(cmsswbase) + "/src/GeneratorInterface/RivetInterface/data:" + string(cmsswrelease) + "/src/GeneratorInterface/RivetInterface/data";
78  rivetinfo = "RIVET_INFO_PATH=" + string(cmsswbase) + "/src/GeneratorInterface/RivetInterface/data:" + string(cmsswrelease) + "/src/GeneratorInterface/RivetInterface/data";
79  putenv(strdup(rivetref.c_str()));
80  putenv(strdup(rivetinfo.c_str()));
81 }
82 
83 void RivetAnalyzer::beginRun(const edm::Run& iRun,const edm::EventSetup& iSetup){
84  return;
85 }
86 
88 
89  //get the hepmc product from the event
91  iEvent.getByLabel(_hepmcCollection, evt);
92 
93  // get HepMC GenEvent
94  const HepMC::GenEvent *myGenEvent = evt->GetEvent();
95  //if you want to use an external weight we have to clene the GenEvent and change the weight
96  if ( _useExternalWeight ){
97  HepMC::GenEvent * tmpGenEvtPtr = new HepMC::GenEvent( *(evt->GetEvent()) );
98  if (tmpGenEvtPtr->weights().size() == 0) {
99  throw cms::Exception("RivetAnalyzer") << "Original weight container has 0 size ";
100  }
101  if (tmpGenEvtPtr->weights().size() > 1) {
102  edm::LogWarning("RivetAnalyzer") << "Original event weight size is " << tmpGenEvtPtr->weights().size() << ". Will change only the first one ";
103  }
104  edm::Handle<GenEventInfoProduct> genEventInfoProduct;
105  iEvent.getByLabel(_genEventInfoCollection, genEventInfoProduct);
106  tmpGenEvtPtr->weights()[0] = genEventInfoProduct->weight();
107  myGenEvent = tmpGenEvtPtr;
108  }
109 
110 
111  //aaply the beams initialization on the first event
112  if (_isFirstEvent){
113  _analysisHandler.init(*myGenEvent);
114  _isFirstEvent = false;
115  }
116 
117  //run the analysis
118  _analysisHandler.analyze(*myGenEvent);
119 
120  //if we have cloned the GenEvent, we delete it
121  if ( _useExternalWeight )
122  delete myGenEvent;
123 }
124 
125 
126 void RivetAnalyzer::endRun(const edm::Run& iRun,const edm::EventSetup& iSetup){
127  if (_doFinalize)
128  _analysisHandler.finalize();
129  else {
130  //if we don't finalize we just want to do the transformation from histograms to DPS
132  //normalizeTree();
133 
134  }
135  _analysisHandler.writeData(_outFileName);
136 
137  return;
138 }
139 
140 
141 
142  //from Rivet 2.X: Analysis.hh (cls 18Feb2014)
144  //const vector<AnalysisObjectPtr>& analysisObjects() const {
145  //return _analysisobjects;
146  //}
147 
148 
149 
151 }
152 
153 
155  using namespace YODA;
156  std::vector<string> analyses = _analysisHandler.analysisNames();
157 
158  //tree.ls(".", true);
159  const string tmpdir = "/RivetNormalizeTmp";
160  //tree.mkdir(tmpdir);
161  foreach (const string& analysis, analyses) {
162  if (_produceDQM){
163  dbe->setCurrentFolder(("Rivet/"+analysis).c_str());
164  //global variables that are always present
165  //sumOfWeights
166  TH1F nevent("nEvt", "n analyzed Events", 1, 0., 1.);
167  nevent.SetBinContent(1,_analysisHandler.sumOfWeights());
168  _mes.push_back(dbe->book1D("nEvt",&nevent));
169  }
170  //cross section
171  //TH1F xsection("xSection", "Cross Section", 1, 0., 1.);
172  //xsection.SetBinContent(1,_analysisHandler.crossSection());
173  //_mes.push_back(dbe->book1D("xSection",&xsection));
174  //now loop over the histograms
175 
176  /*
177  const vector<string> paths = tree.listObjectNames("/"+analysis, true); // args set recursive listing
178  std::cout << "Number of objects in YODA tree for analysis " << analysis << " = " << paths.size() << std::endl;
179  foreach (const string& path, paths) {
180  IManagedObject* hobj = tree.find(path);
181  if (hobj) {
182  // Weird seg fault on SLC4 when trying to dyn cast an IProfile ptr to a IHistogram
183  // Fix by attempting to cast to IProfile first, only try IHistogram if it fails.
184  IHistogram1D* histo = 0;
185  IProfile1D* prof = dynamic_cast<IProfile1D*>(hobj);
186  if (!prof) histo = dynamic_cast<IHistogram1D*>(hobj);
187 
188  std::cout << "Converting histo " << path << " to DPS" << std::endl;
189  tree.mv(path, tmpdir);
190  const size_t lastslash = path.find_last_of("/");
191  const string basename = path.substr(lastslash+1, path.length() - (lastslash+1));
192  const string tmppath = tmpdir + "/" + basename;
193 
194  // If it's a normal histo:
195  if (histo) {
196  IHistogram1D* tmphisto = dynamic_cast<IHistogram1D*>(tree.find(tmppath));
197  if (tmphisto) {
198  _analysisHandler.datapointsetFactory().create(path, *tmphisto);
199  }
200  //now convert to root and then ME
201  //need aida2flat (from Rivet 1.X) & flat2root here
202  TH1F* h = aida2root<IHistogram1D, TH1F>(histo, basename);
203  if (_produceDQM)
204  _mes.push_back(dbe->book1D(h->GetName(), h));
205  delete h;
206  tree.rm(tmppath);
207  }
208  // If it's a profile histo:
209  else if (prof) {
210  IProfile1D* tmpprof = dynamic_cast<IProfile1D*>(tree.find(tmppath));
211  if (tmpprof) {
212  _analysisHandler.datapointsetFactory().create(path, *tmpprof);
213  }
214  //now convert to root and then ME
215  //need aida2flat (from Rivet 1.X) & flat2root here
216  TProfile* p = aida2root<IProfile1D, TProfile>(prof, basename);
217  if (_produceDQM)
218  _mes.push_back(dbe->bookProfile(p->GetName(), p));
219  delete p;
220  tree.rm(tmppath);
221  }
222  }
223  }
224  */
225  }
226  //tree.rmdir(tmpdir);
227 
228 }
229 
230 
231 
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:51
int iEvent
Definition: GenABIO.cc:230
RivetAnalyzer(const edm::ParameterSet &)
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:402
DQMStore * dbe
Definition: RivetAnalyzer.h:57
std::string _outFileName
Definition: RivetAnalyzer.h:53
bool _useExternalWeight
Definition: RivetAnalyzer.h:49
edm::InputTag _genEventInfoCollection
Definition: RivetAnalyzer.h:50
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:58