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 #include "Rivet/RivetAIDA.hh"
14 #include "LWH/AIManagedObject.h"
15 
16 #include <string>
17 #include <vector>
18 #include <iostream>
19 #include <cstdlib>
20 #include <cstring>
21 
22 using namespace Rivet;
23 using namespace edm;
24 
26 _analysisHandler(),
27 _isFirstEvent(true),
28 _outFileName(pset.getParameter<std::string>("OutputFile")),
29 //decide whether to finlaize tthe plots or not.
30 //deciding not to finalize them can be useful for further harvesting of many jobs
31 _doFinalize(pset.getParameter<bool>("DoFinalize")),
32 _produceDQM(pset.getParameter<bool>("ProduceDQMOutput"))
33 {
34  //retrive the analysis name from paarmeter set
35  std::vector<std::string> analysisNames = pset.getParameter<std::vector<std::string> >("AnalysisNames");
36 
37  _hepmcCollection = pset.getParameter<edm::InputTag>("HepMCCollection");
38 
39  _useExternalWeight = pset.getParameter<bool>("UseExternalWeight");
40  if (_useExternalWeight) {
41  if (!pset.exists("GenEventInfoCollection")){
42  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 " ;
43  }
44  _genEventInfoCollection = pset.getParameter<edm::InputTag>("GenEventInfoCollection");
45  }
46 
47  //get the analyses
48  _analysisHandler.addAnalyses(analysisNames);
49 
50  //go through the analyses and check those that need the cross section
51  const std::set< AnaHandle, AnaHandleLess > & analyses = _analysisHandler.analyses();
52 
53  std::set< AnaHandle, AnaHandleLess >::const_iterator ibeg = analyses.begin();
54  std::set< AnaHandle, AnaHandleLess >::const_iterator iend = analyses.end();
55  std::set< AnaHandle, AnaHandleLess >::const_iterator iana;
56  double xsection = -1.;
57  xsection = pset.getParameter<double>("CrossSection");
58  for (iana = ibeg; iana != iend; ++iana){
59  if ((*iana)->needsCrossSection())
60  (*iana)->setCrossSection(xsection);
61  }
62  if (_produceDQM){
63  // book stuff needed for DQM
64  dbe = 0;
66  dbe->setVerbose(50);
67  }
68 
69 }
70 
72 }
73 
75  //set the environment, very ugly but rivet is monolithic when it comes to paths
76  char * cmsswbase = getenv("CMSSW_BASE");
77  char * cmsswrelease = getenv("CMSSW_RELEASE_BASE");
78  std::string rivetref, rivetinfo;
79  rivetref = "RIVET_REF_PATH=" + string(cmsswbase) + "/src/GeneratorInterface/RivetInterface/data:" + string(cmsswrelease) + "/src/GeneratorInterface/RivetInterface/data";
80  rivetinfo = "RIVET_INFO_PATH=" + string(cmsswbase) + "/src/GeneratorInterface/RivetInterface/data:" + string(cmsswrelease) + "/src/GeneratorInterface/RivetInterface/data";
81  putenv(strdup(rivetref.c_str()));
82  putenv(strdup(rivetinfo.c_str()));
83 }
84 
85 void RivetAnalyzer::beginRun(const edm::Run& iRun,const edm::EventSetup& iSetup){
86  return;
87 }
88 
90 
91  //get the hepmc product from the event
93  iEvent.getByLabel(_hepmcCollection, evt);
94 
95  // get HepMC GenEvent
96  const HepMC::GenEvent *myGenEvent = evt->GetEvent();
97  //if you want to use an external weight we have to clene the GenEvent and change the weight
98  if ( _useExternalWeight ){
99  HepMC::GenEvent * tmpGenEvtPtr = new HepMC::GenEvent( *(evt->GetEvent()) );
100  if (tmpGenEvtPtr->weights().size() == 0) {
101  throw cms::Exception("RivetAnalyzer") << "Original weight container has 0 size ";
102  }
103  if (tmpGenEvtPtr->weights().size() > 1) {
104  edm::LogWarning("RivetAnalyzer") << "Original event weight size is " << tmpGenEvtPtr->weights().size() << ". Will change only the first one ";
105  }
106  edm::Handle<GenEventInfoProduct> genEventInfoProduct;
107  iEvent.getByLabel(_genEventInfoCollection, genEventInfoProduct);
108  tmpGenEvtPtr->weights()[0] = genEventInfoProduct->weight();
109  myGenEvent = tmpGenEvtPtr;
110  }
111 
112 
113  //aaply the beams initialization on the first event
114  if (_isFirstEvent){
115  _analysisHandler.init(*myGenEvent);
116  _isFirstEvent = false;
117  }
118 
119  //run the analysis
120  _analysisHandler.analyze(*myGenEvent);
121 
122  //if we have cloned the GenEvent, we delete it
123  if ( _useExternalWeight )
124  delete myGenEvent;
125 }
126 
127 
128 void RivetAnalyzer::endRun(const edm::Run& iRun,const edm::EventSetup& iSetup){
129  if (_doFinalize)
130  _analysisHandler.finalize();
131  else {
132  //if we don't finalize we just want to do the transformation from histograms to DPS
134  }
135  _analysisHandler.writeData(_outFileName);
136 
137  return;
138 }
139 
141 }
142 
143 void RivetAnalyzer::normalizeTree(AIDA::ITree& tree) {
144  using namespace AIDA;
145  std::vector<string> analyses = _analysisHandler.analysisNames();
146  tree.ls(".", true);
147  const string tmpdir = "/RivetNormalizeTmp";
148  tree.mkdir(tmpdir);
149  foreach (const string& analysis, analyses) {
150  if (_produceDQM){
151  dbe->setCurrentFolder(("Rivet/"+analysis).c_str());
152  //global variables that are always present
153  //sumOfWeights
154  TH1F nevent("nEvt", "n analyzed Events", 1, 0., 1.);
155  nevent.SetBinContent(1,_analysisHandler.sumOfWeights());
156  _mes.push_back(dbe->book1D("nEvt",&nevent));
157  }
158  //cross section
159  //TH1F xsection("xSection", "Cross Section", 1, 0., 1.);
160  //xsection.SetBinContent(1,_analysisHandler.crossSection());
161  //_mes.push_back(dbe->book1D("xSection",&xsection));
162  //now loop over the histograms
163  const vector<string> paths = tree.listObjectNames("/"+analysis, true); // args set recursive listing
164  std::cout << "Number of objects in AIDA tree for analysis " << analysis << " = " << paths.size() << std::endl;
165  foreach (const string& path, paths) {
166  IManagedObject* hobj = tree.find(path);
167  if (hobj) {
168  // Weird seg fault on SLC4 when trying to dyn cast an IProfile ptr to a IHistogram
169  // Fix by attempting to cast to IProfile first, only try IHistogram if it fails.
170  IHistogram1D* histo = 0;
171  IProfile1D* prof = dynamic_cast<IProfile1D*>(hobj);
172  if (!prof) histo = dynamic_cast<IHistogram1D*>(hobj);
173 
174  std::cout << "Converting histo " << path << " to DPS" << std::endl;
175  tree.mv(path, tmpdir);
176  const size_t lastslash = path.find_last_of("/");
177  const string basename = path.substr(lastslash+1, path.length() - (lastslash+1));
178  const string tmppath = tmpdir + "/" + basename;
179 
180  // If it's a normal histo:
181  if (histo) {
182  IHistogram1D* tmphisto = dynamic_cast<IHistogram1D*>(tree.find(tmppath));
183  if (tmphisto) {
184  _analysisHandler.datapointsetFactory().create(path, *tmphisto);
185  }
186  //now convert to root and then ME
187  TH1F* h = aida2root<IHistogram1D, TH1F>(histo, basename);
188  if (_produceDQM)
189  _mes.push_back(dbe->book1D(h->GetName(), h));
190  delete h;
191  tree.rm(tmppath);
192  }
193  // If it's a profile histo:
194  else if (prof) {
195  IProfile1D* tmpprof = dynamic_cast<IProfile1D*>(tree.find(tmppath));
196  if (tmpprof) {
197  _analysisHandler.datapointsetFactory().create(path, *tmpprof);
198  }
199  //now convert to root and then ME
200  TProfile* p = aida2root<IProfile1D, TProfile>(prof, basename);
201  if (_produceDQM)
202  _mes.push_back(dbe->bookProfile(p->GetName(), p));
203  delete p;
204  tree.rm(tmppath);
205  }
206  }
207  }
208  }
209  tree.rmdir(tmpdir);
210 }
211 
212 
T getParameter(std::string const &) const
virtual void beginRun(const edm::Run &, const edm::EventSetup &)
virtual void endRun(const edm::Run &, const edm::EventSetup &)
MonitorElement * book1D(const char *name, const char *title, int nchX, double lowX, double highX)
Book 1D histogram.
Definition: DQMStore.cc:954
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:52
int nevent
Definition: AMPTWrapper.h:74
Rivet::AnalysisHandler _analysisHandler
Definition: RivetAnalyzer.h:55
int iEvent
Definition: GenABIO.cc:230
tuple path
else: Piece not in the list, fine.
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
MonitorElement * bookProfile(const char *name, const char *title, int nchX, double lowX, double highX, int nchY, double lowY, double highY, const char *option="s")
Definition: DQMStore.cc:1268
void setVerbose(unsigned level)
Definition: DQMStore.cc:631
RivetAnalyzer(const edm::ParameterSet &)
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:390
DQMStore * dbe
Definition: RivetAnalyzer.h:61
std::string _outFileName
Definition: RivetAnalyzer.h:57
void normalizeTree(AIDA::ITree &tree)
tuple cout
Definition: gather_cfg.py:121
bool _useExternalWeight
Definition: RivetAnalyzer.h:53
edm::InputTag _genEventInfoCollection
Definition: RivetAnalyzer.h:54
virtual ~RivetAnalyzer()
virtual void beginJob()
void setCurrentFolder(const std::string &fullpath)
Definition: DQMStore.cc:667
Definition: Run.h:41
virtual void endJob()
std::vector< MonitorElement * > _mes
Definition: RivetAnalyzer.h:62