CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
RivetHarvesting.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"
16 
17 #include <string>
18 #include <vector>
19 #include <iostream>
20 #include <cstdlib>
21 #include <cstring>
22 
23 using namespace Rivet;
24 using namespace edm;
25 using namespace std;
26 
28 _analysisHandler(),
29 _fileNames(pset.getParameter<std::vector<std::string> >("FilesToHarvest")),
30 _sumOfWeights(pset.getParameter<std::vector<double> >("VSumOfWeights")),
31 _crossSections(pset.getParameter<std::vector<double> >("VCrossSections")),
32 _outFileName(pset.getParameter<std::string>("OutputFile")),
33 _isFirstEvent(true),
34 _hepmcCollection(pset.getParameter<edm::InputTag>("HepMCCollection")),
35 _analysisNames(pset.getParameter<std::vector<std::string> >("AnalysisNames"))
36 {
37 
38  if (_sumOfWeights.size() != _fileNames.size() ||
39  _sumOfWeights.size() != _crossSections.size() ||
40  _fileNames.size() != _crossSections.size()){
41  throw cms::Exception("RivetHarvesting") << "Mismatch in vector sizes: FilesToHarvest: " << _sumOfWeights.size() << ", VSumOfWeights: " << _sumOfWeights.size() << ", VCrossSections: " << _crossSections.size();
42  }
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, AnaHandleLess > & analyses = _analysisHandler.analyses();
50 
51  std::set< AnaHandle, AnaHandleLess >::const_iterator ibeg = analyses.begin();
52  std::set< AnaHandle, AnaHandleLess >::const_iterator iend = analyses.end();
53  std::set< AnaHandle, AnaHandleLess >::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 
61  double totalSumOfWeights = _sumOfWeights[0];
62  _lumis.push_back(_sumOfWeights[0]/_crossSections[0]);
63  for (unsigned int i = 1; i < _sumOfWeights.size(); ++i){
64  _lumis.push_back(_sumOfWeights[i]/_crossSections[i]);
65  totalSumOfWeights += _sumOfWeights[i]*_lumis[0]/_lumis[i];
66  }
67  _analysisHandler.setSumOfWeights(totalSumOfWeights);
68 
69 
70 }
71 
73 }
74 
76  //set the environment, very ugly but rivet is monolithic when it comes to paths
77  char * cmsswbase = getenv("CMSSW_BASE");
78  char * cmsswrelease = getenv("CMSSW_RELEASE_BASE");
79  std::string rivetref, rivetinfo;
80  rivetref = "RIVET_REF_PATH=" + string(cmsswbase) + "/src/GeneratorInterface/RivetInterface/data:" + string(cmsswrelease) + "/src/GeneratorInterface/RivetInterface/data";
81  rivetinfo = "RIVET_INFO_PATH=" + string(cmsswbase) + "/src/GeneratorInterface/RivetInterface/data:" + string(cmsswrelease) + "/src/GeneratorInterface/RivetInterface/data";
82  putenv(strdup(rivetref.c_str()));
83  putenv(strdup(rivetinfo.c_str()));
84 }
85 
86 void RivetHarvesting::beginRun(const edm::Run& iRun,const edm::EventSetup& iSetup){
87  return;
88 }
89 
91  if (!_isFirstEvent)
92  return;
93 
94  //initialize the analysis handles, all histograms are booked
95  //we need at least one event to get the handler initialized
97  iEvent.getByLabel(_hepmcCollection, evt);
98 
99  // get HepMC GenEvent
100  const HepMC::GenEvent *myGenEvent = evt->GetEvent();
101  _analysisHandler.init(*myGenEvent);
102  //gain access to the histogram factory and change the histograms
103  AIDA::ITree & tree = _analysisHandler.tree();
104  tree.ls(".", true);
105 
106  /*
107  map<string, vector<DPSXYPoint> > existingHistos;
108  vector<string>::const_iterator iFile;
109  vector<string>::const_iterator iFileBeg = _fileNames.begin();
110  vector<string>::const_iterator iFileEnd = _fileNames.end();
111  for (iFile = iFileBeg; iFile != iFileEnd; ++iFile) {
112  map<string, vector<DPSXYPoint> > thisFileHistos = getDPSXYValsErrs(*iFile);
113  map<string, vector<DPSXYPoint> >::const_iterator iMap;
114  map<string, vector<DPSXYPoint> >::const_iterator iMapBeg = thisFileHistos.begin();
115  map<string, vector<DPSXYPoint> >::const_iterator iMapEnd = thisFileHistos.end();
116  for (iMap = iMapBeg; iMap != iMapEnd; ++iMap){
117  std::cout << iMap->first << " found in the original file " << *iFile << std::endl;
118  existingHistos[iMap->first] = iMap->second;
119  }
120  //existingHistos.insert(existingHistos.end(), thisFileHistos.begin(), thisFileHistos.end());
121  }
122  */
123 
124  for (std::vector<std::string>::const_iterator iAna = _analysisNames.begin(); iAna != _analysisNames.end(); ++iAna){
125  std::vector<std::string> listOfNames = tree.listObjectNames("./"+(*iAna), true);
126  std::vector<std::string>::const_iterator iNameBeg = listOfNames.begin();
127  std::vector<std::string>::const_iterator iNameEnd = listOfNames.end();
128  for (std::vector<std::string>::const_iterator iName = iNameBeg; iName != iNameEnd; ++iName ){
129  AIDA::IManagedObject * iObj = tree.find(*iName);
130  if (!iObj){
131  std::cout << *iName << " not found; SKIPPING!" << std::endl;
132  continue;
133  }
134 
135  std::cout << *iName << " FOUND!" << std::endl;
136  vector<string>::const_iterator iFile;
137  vector<string>::const_iterator iFileBeg = _fileNames.begin();
138  vector<string>::const_iterator iFileEnd = _fileNames.end();
139  AIDA::IHistogram1D* histo = dynamic_cast<AIDA::IHistogram1D*>(iObj);
140  AIDA::IProfile1D* prof = dynamic_cast<AIDA::IProfile1D*>(iObj);
141  string tmpdir = "/tmpdir";
142  tree.mkdir(tmpdir);
143  unsigned int ifc = 0;
144  for (iFile = iFileBeg; iFile != iFileEnd; ++iFile) {
145  std::cout << "opening file " << *iFile << std::endl;
146  string name = *iName;
147  string tostrip = *iAna+'/';
148  name.replace(name.find(tostrip),tostrip.length(),"");
149  name.replace(name.find("/"),1,"");
150  cout << name << endl;
151  vector<DPSXYPoint> original = getDPSXYValsErrs(*iFile, *iAna, name);
152  if (histo){
153  const string tmppath = tmpdir + "/" + name;
154  cout << tmppath << endl;
155  IHistogram1D* tmphisto = _analysisHandler.histogramFactory().createCopy(tmppath, *histo);
156  tmphisto->reset();
157  for (unsigned int i = 0; i < original.size(); ++i){
158  tmphisto->fill(original[i].xval, original[i].yval);
159  }
160  tmphisto->scale(_lumis[ifc]);
161  histo->add(*tmphisto);
162  //iObj = new AIDA::IHistogram1D(*(_analysisHandler.histogramFactory().add(*iName, *histo, *tmphisto)));
163  tree.rm(tmppath);
164  //delete tmphisto;
165  } else if (prof) {
166  std::cout << *iName << "is a profile, doing nothing " << std::endl;
167  } else {
168  std::cout << *iName << " is neither a IHistogram1D neither a IProfile1D. Doing nothing with it." << std::endl;
169  }
170  ++ifc;
171  }
172  cout << iObj << endl;
173  }
174  }
175 
176  tree.ls(".", true);
177 
178  _isFirstEvent = false;
179 }
180 
181 
182 void RivetHarvesting::endRun(const edm::Run& iRun,const edm::EventSetup& iSetup){
183  return;
184 }
185 
187  _analysisHandler.finalize();
188  _analysisHandler.writeData(_outFileName);
189 }
190 
191 vector<DPSXYPoint> RivetHarvesting::getDPSXYValsErrs(std::string filename, std::string path, std::string name) {
192  // Open AIDA XML file
193  TiXmlDocument doc(filename);
194  doc.LoadFile();
195  if (doc.Error()) {
196  string err = "Error in " + string(doc.Value());
197  err += ": " + string(doc.ErrorDesc());
198  cerr << err << endl;
199  throw cms::Exception("RivetHarvesting") << "Cannot open " << filename;
200  }
201 
202  // Return value, to be populated
203  vector<DPSXYPoint> rtn;
204 
205  try {
206  // Walk down tree to get to the <paper> element
207  const TiXmlNode* aidaN = doc.FirstChild("aida");
208  if (!aidaN) throw cms::Exception("RivetHarvesting") << "Couldn't get <aida> root element";
209  for (const TiXmlNode* dpsN = aidaN->FirstChild("dataPointSet"); dpsN; dpsN = dpsN->NextSibling()) {
210  const TiXmlElement* dpsE = dpsN->ToElement();
211  const string plotname = dpsE->Attribute("name");
212  const string plotpath = dpsE->Attribute("path");
213  if (plotpath != path && plotname != name)
214  continue;
216  //if (plotpath.find("/REF") != 0) {
217  // cerr << "Skipping non-reference histogram " << plotname << endl;
218  // continue;
219  //}
220 
222  vector<DPSXYPoint> points;
223  for (const TiXmlNode* dpN = dpsN->FirstChild("dataPoint"); dpN; dpN = dpN->NextSibling()) {
224  const TiXmlNode* xMeasN = dpN->FirstChild("measurement");
225  const TiXmlNode* yMeasN = xMeasN->NextSibling();
226  if (xMeasN && yMeasN) {
227  const TiXmlElement* xMeasE = xMeasN->ToElement();
228  const TiXmlElement* yMeasE = yMeasN->ToElement();
229  const string xcentreStr = xMeasE->Attribute("value");
230  const string xerrplusStr = xMeasE->Attribute("errorPlus");
231  const string xerrminusStr = xMeasE->Attribute("errorMinus");
232  const string ycentreStr = yMeasE->Attribute("value");
233  const string yerrplusStr = yMeasE->Attribute("errorPlus");
234  const string yerrminusStr = yMeasE->Attribute("errorMinus");
235  //if (!centreStr) throw Error("Couldn't get a valid bin centre");
236  //if (!errplusStr) throw Error("Couldn't get a valid bin err+");
237  //if (!errminusStr) throw Error("Couldn't get a valid bin err-");
238  istringstream xssC(xcentreStr);
239  istringstream xssP(xerrplusStr);
240  istringstream xssM(xerrminusStr);
241  istringstream yssC(ycentreStr);
242  istringstream yssP(yerrplusStr);
243  istringstream yssM(yerrminusStr);
244  double xcentre, xerrplus, xerrminus, ycentre, yerrplus, yerrminus;
245  xssC >> xcentre; xssP >> xerrplus; xssM >> xerrminus;
246  yssC >> ycentre; yssP >> yerrplus; yssM >> yerrminus;
247  //cout << " " << centre << " + " << errplus << " - " << errminus << endl;
248  DPSXYPoint pt(xcentre, xerrminus, xerrplus, ycentre, yerrminus, yerrplus);
249  points.push_back(pt);
250  } else {
251  cerr << "Couldn't get <measurement> tag" << endl;
253  }
254  }
255 
256  return points;
257  }
258 
259  }
260  // Write out the error
262  catch (std::exception& e) {
263  cerr << e.what() << endl;
264  throw;
265  }
266 
267  throw cms::Exception("RivetHarvesting") << "could not find " << path << "/" << name << " in file " << filename;
268  return rtn;
269 }
270 
T getParameter(std::string const &) const
std::vector< double > _lumis
int i
Definition: DBlmapReader.cc:9
virtual const TiXmlElement * ToElement() const
Cast to a more defined type. Will return null not of the requested type.
Definition: tinyxml.h:1127
bool Error() const
Definition: tinyxml.h:1459
virtual void beginRun(const edm::Run &, const edm::EventSetup &)
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
list original
Definition: definitions.py:60
bool LoadFile(TiXmlEncoding encoding=TIXML_DEFAULT_ENCODING)
Definition: tinyxml.cc:934
virtual void endRun(const edm::Run &, const edm::EventSetup &)
virtual ~RivetHarvesting()
edm::InputTag _hepmcCollection
list path
Definition: scaleCards.py:51
std::vector< double > _crossSections
int iEvent
Definition: GenABIO.cc:243
RivetHarvesting(const edm::ParameterSet &)
std::vector< std::string > _fileNames
virtual void beginJob()
const TiXmlNode * FirstChild() const
The first child of this node. Will be null if there are no children.
Definition: tinyxml.h:526
const char * ErrorDesc() const
Contains a textual (english) description of the error if one occurs.
Definition: tinyxml.h:1462
tuple doc
Definition: asciidump.py:381
std::vector< double > _sumOfWeights
const TiXmlNode * NextSibling(const std::string &_value) const
STL std::string form.
Definition: tinyxml.h:630
Rivet::AnalysisHandler _analysisHandler
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:356
std::string _outFileName
virtual const TiXmlElement * ToElement() const
Cast to a more defined type. Will return null if not of the requested type.
Definition: tinyxml.h:702
std::vector< Rivet::DPSXYPoint > getDPSXYValsErrs(std::string filename, std::string path, std::string name)
virtual void analyze(const edm::Event &, const edm::EventSetup &)
tuple filename
Definition: lut2db_cfg.py:20
const char * Value() const
Definition: tinyxml.h:491
tuple cout
Definition: gather_cfg.py:121
std::vector< std::string > _analysisNames
const char * Attribute(const char *name) const
Definition: tinyxml.cc:564
Definition: Run.h:33
virtual void endJob()