CMS 3D CMS Logo

List of all members | Public Member Functions | Private Member Functions | Private Attributes
RivetAnalyzer Class Reference

#include <RivetAnalyzer.h>

Inheritance diagram for RivetAnalyzer:
edm::one::EDAnalyzer< edm::one::WatchRuns, edm::one::SharedResources > edm::one::EDAnalyzerBase edm::EDConsumerBase

Public Member Functions

void analyze (const edm::Event &, const edm::EventSetup &) override
 
void beginJob () override
 
void beginRun (const edm::Run &, const edm::EventSetup &) override
 
void endJob () override
 List of registered analysis data objects. More...
 
void endRun (const edm::Run &, const edm::EventSetup &) override
 
 RivetAnalyzer (const edm::ParameterSet &)
 
 ~RivetAnalyzer () override
 
- Public Member Functions inherited from edm::one::EDAnalyzer< edm::one::WatchRuns, edm::one::SharedResources >
 EDAnalyzer ()=default
 
SerialTaskQueueglobalLuminosityBlocksQueue () final
 
SerialTaskQueueglobalRunsQueue () final
 
bool wantsGlobalLuminosityBlocks () const final
 
bool wantsGlobalRuns () const final
 
- Public Member Functions inherited from edm::one::EDAnalyzerBase
void callWhenNewProductsRegistered (std::function< void(BranchDescription const &)> const &func)
 
 EDAnalyzerBase ()
 
ModuleDescription const & moduleDescription () const
 
bool wantsStreamLuminosityBlocks () const
 
bool wantsStreamRuns () const
 
 ~EDAnalyzerBase () override
 
- Public Member Functions inherited from edm::EDConsumerBase
std::vector< ConsumesInfoconsumesInfo () const
 
void convertCurrentProcessAlias (std::string const &processName)
 Convert "@currentProcess" in InputTag process names to the actual current process name. More...
 
 EDConsumerBase ()
 
 EDConsumerBase (EDConsumerBase const &)=delete
 
 EDConsumerBase (EDConsumerBase &&)=default
 
ESProxyIndex const * esGetTokenIndices (edm::Transition iTrans) const
 
ProductResolverIndexAndSkipBit indexFrom (EDGetToken, BranchType, TypeID const &) const
 
void itemsMayGet (BranchType, std::vector< ProductResolverIndexAndSkipBit > &) const
 
void itemsToGet (BranchType, std::vector< ProductResolverIndexAndSkipBit > &) const
 
std::vector< ProductResolverIndexAndSkipBit > const & itemsToGetFrom (BranchType iType) const
 
void labelsForToken (EDGetToken iToken, Labels &oLabels) const
 
void modulesWhoseProductsAreConsumed (std::vector< ModuleDescription const * > &modules, ProductRegistry const &preg, std::map< std::string, ModuleDescription const * > const &labelsToDesc, std::string const &processName) const
 
EDConsumerBase const & operator= (EDConsumerBase const &)=delete
 
EDConsumerBaseoperator= (EDConsumerBase &&)=default
 
bool registeredToConsume (ProductResolverIndex, bool, BranchType) const
 
bool registeredToConsumeMany (TypeID const &, BranchType) const
 
ProductResolverIndexAndSkipBit uncheckedIndexFrom (EDGetToken) const
 
void updateLookup (BranchType iBranchType, ProductResolverIndexHelper const &, bool iPrefetchMayGet)
 
void updateLookup (eventsetup::ESRecordsToProxyIndices const &)
 
virtual ~EDConsumerBase () noexcept(false)
 

Private Member Functions

void normalizeTree ()
 

Private Attributes

Rivet::AnalysisHandler _analysisHandler
 
bool _doFinalize
 
edm::EDGetTokenT< GenEventInfoProduct_genEventInfoCollection
 
int _GENweightNumber
 
edm::EDGetTokenT< edm::HepMCProduct_hepmcCollection
 
bool _isFirstEvent
 
edm::EDGetTokenT< LHEEventProduct_LHECollection
 
int _LHEweightNumber
 
std::vector< MonitorElement * > _mes
 
std::string _outFileName
 
bool _produceDQM
 
bool _useExternalWeight
 
bool _useGENweights
 
bool _useLHEweights
 
double _xsection
 
DQMStoredbe
 

Additional Inherited Members

- Public Types inherited from edm::one::EDAnalyzerBase
typedef EDAnalyzerBase ModuleType
 
- Public Types inherited from edm::EDConsumerBase
typedef ProductLabels Labels
 
- Static Public Member Functions inherited from edm::one::EDAnalyzerBase
static const std::string & baseType ()
 
static void fillDescriptions (ConfigurationDescriptions &descriptions)
 
static void prevalidate (ConfigurationDescriptions &descriptions)
 
- Protected Member Functions inherited from edm::EDConsumerBase
template<typename ProductType , BranchType B = InEvent>
EDGetTokenT< ProductType > consumes (edm::InputTag const &tag)
 
EDGetToken consumes (const TypeToGet &id, edm::InputTag const &tag)
 
template<BranchType B>
EDGetToken consumes (TypeToGet const &id, edm::InputTag const &tag)
 
ConsumesCollector consumesCollector ()
 Use a ConsumesCollector to gather consumes information from helper functions. More...
 
template<typename ProductType , BranchType B = InEvent>
void consumesMany ()
 
void consumesMany (const TypeToGet &id)
 
template<BranchType B>
void consumesMany (const TypeToGet &id)
 
template<typename ESProduct , typename ESRecord , Transition Tr = Transition::Event>
auto esConsumes ()
 
template<typename ESProduct , typename ESRecord , Transition Tr = Transition::Event>
auto esConsumes (ESInputTag const &tag)
 
template<typename ProductType , BranchType B = InEvent>
EDGetTokenT< ProductType > mayConsume (edm::InputTag const &tag)
 
EDGetToken mayConsume (const TypeToGet &id, edm::InputTag const &tag)
 
template<BranchType B>
EDGetToken mayConsume (const TypeToGet &id, edm::InputTag const &tag)
 

Detailed Description

Definition at line 21 of file RivetAnalyzer.h.

Constructor & Destructor Documentation

RivetAnalyzer::RivetAnalyzer ( const edm::ParameterSet pset)

Definition at line 15 of file RivetAnalyzer.cc.

References _analysisHandler, _genEventInfoCollection, _GENweightNumber, _hepmcCollection, _LHECollection, _LHEweightNumber, _produceDQM, _useExternalWeight, _useGENweights, _useLHEweights, _xsection, dbe, Exception, edm::ParameterSet::exists(), edm::ParameterSet::getParameter(), and Utilities::operator.

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  usesResource("Rivet");
25 
26  //retrive the analysis name from paarmeter set
27  std::vector<std::string> analysisNames = pset.getParameter<std::vector<std::string> >("AnalysisNames");
28 
29  _hepmcCollection = consumes<HepMCProduct>(pset.getParameter<edm::InputTag>("HepMCCollection"));
30 
31  _useExternalWeight = pset.getParameter<bool>("UseExternalWeight");
32  if (_useExternalWeight) {
33  if (!pset.exists("GenEventInfoCollection")) {
34  throw cms::Exception("RivetAnalyzer") << "when using an external event weight you have to specify the "
35  "GenEventInfoProduct collection from which the weight has to be taken ";
36  }
37 
38  _genEventInfoCollection = consumes<GenEventInfoProduct>(pset.getParameter<edm::InputTag>("GenEventInfoCollection"));
39  _useGENweights = pset.getParameter<bool>("useGENweights");
40  _GENweightNumber = pset.getParameter<int>("GENweightNumber");
41  _LHECollection = consumes<LHEEventProduct>(pset.getParameter<edm::InputTag>("LHECollection"));
42  _useLHEweights = pset.getParameter<bool>("useLHEweights");
43  _LHEweightNumber = pset.getParameter<int>("LHEweightNumber");
44  }
45 
46  //get the analyses
47  _analysisHandler.addAnalyses(analysisNames);
48 
49  //go through the analyses and check those that need the cross section
50  const std::set<AnaHandle, CmpAnaHandle>& analyses = _analysisHandler.analyses();
51 
52  std::set<AnaHandle, CmpAnaHandle>::const_iterator ibeg = analyses.begin();
53  std::set<AnaHandle, CmpAnaHandle>::const_iterator iend = analyses.end();
54  std::set<AnaHandle, CmpAnaHandle>::const_iterator iana;
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 = nullptr;
64  dbe->setVerbose(50);
65  }
66 }
T getParameter(std::string const &) const
bool exists(std::string const &parameterName) const
checks if a parameter exists
Rivet::AnalysisHandler _analysisHandler
Definition: RivetAnalyzer.h:48
edm::EDGetTokenT< edm::HepMCProduct > _hepmcCollection
Definition: RivetAnalyzer.h:40
double _xsection
Definition: RivetAnalyzer.h:53
DQMStore * dbe
Definition: RivetAnalyzer.h:55
std::string _outFileName
Definition: RivetAnalyzer.h:50
edm::EDGetTokenT< LHEEventProduct > _LHECollection
Definition: RivetAnalyzer.h:46
bool _useExternalWeight
Definition: RivetAnalyzer.h:41
edm::EDGetTokenT< GenEventInfoProduct > _genEventInfoCollection
Definition: RivetAnalyzer.h:47
RivetAnalyzer::~RivetAnalyzer ( )
override

Definition at line 68 of file RivetAnalyzer.cc.

68 {}

Member Function Documentation

void RivetAnalyzer::analyze ( const edm::Event iEvent,
const edm::EventSetup iSetup 
)
override

Definition at line 94 of file RivetAnalyzer.cc.

References _analysisHandler, _genEventInfoCollection, _GENweightNumber, _hepmcCollection, _isFirstEvent, _LHECollection, _LHEweightNumber, _useExternalWeight, _useGENweights, _useLHEweights, _xsection, Exception, edm::Event::getByToken(), edm::HepMCProduct::GetEvent(), LHEEventProduct::originalXWGTUP(), GenEventInfoProduct::weight(), GenEventInfoProduct::weights(), and LHEEventProduct::weights().

94  {
95  //get the hepmc product from the event
97  iEvent.getByToken(_hepmcCollection, evt);
98 
99  // get HepMC GenEvent
100  const HepMC::GenEvent* myGenEvent = evt->GetEvent();
101  std::unique_ptr<HepMC::GenEvent> tmpGenEvtPtr;
102  //if you want to use an external weight or set the cross section we have to clone the GenEvent and change the weight
103  if (_useExternalWeight || _xsection > 0) {
104  tmpGenEvtPtr = std::make_unique<HepMC::GenEvent>(*(evt->GetEvent()));
105 
106  if (_xsection > 0) {
107  HepMC::GenCrossSection xsec;
108  xsec.set_cross_section(_xsection);
109  tmpGenEvtPtr->set_cross_section(xsec);
110  }
111 
112  if (_useExternalWeight) {
113  if (tmpGenEvtPtr->weights().empty()) {
114  throw cms::Exception("RivetAnalyzer") << "Original weight container has 0 size ";
115  }
116  if (tmpGenEvtPtr->weights().size() > 1) {
117  edm::LogWarning("RivetAnalyzer") << "Original event weight size is " << tmpGenEvtPtr->weights().size()
118  << ". Will change only the first one ";
119  }
120 
121  edm::Handle<GenEventInfoProduct> genEventInfoProduct;
122  iEvent.getByToken(_genEventInfoCollection, genEventInfoProduct);
123  double weightForRivet = genEventInfoProduct->weight();
124 
125  if (_useGENweights) {
126  weightForRivet = genEventInfoProduct->weights().at(_GENweightNumber);
127  }
128  if (_useLHEweights) {
129  edm::Handle<LHEEventProduct> lheEventHandle;
130  iEvent.getByToken(_LHECollection, lheEventHandle);
131  weightForRivet *= lheEventHandle->weights().at(_LHEweightNumber).wgt / lheEventHandle->originalXWGTUP();
132  }
133 
134  tmpGenEvtPtr->weights()[0] = weightForRivet;
135  }
136  myGenEvent = tmpGenEvtPtr.get();
137  }
138 
139  //aaply the beams initialization on the first event
140  if (_isFirstEvent) {
141  _analysisHandler.init(*myGenEvent);
142  _isFirstEvent = false;
143  }
144 
145  //run the analysis
146  _analysisHandler.analyze(*myGenEvent);
147 }
double originalXWGTUP() const
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
double weight() const
const std::vector< WGT > & weights() const
Rivet::AnalysisHandler _analysisHandler
Definition: RivetAnalyzer.h:48
edm::EDGetTokenT< edm::HepMCProduct > _hepmcCollection
Definition: RivetAnalyzer.h:40
const HepMC::GenEvent * GetEvent() const
Definition: HepMCProduct.h:38
double _xsection
Definition: RivetAnalyzer.h:53
edm::EDGetTokenT< LHEEventProduct > _LHECollection
Definition: RivetAnalyzer.h:46
std::vector< double > & weights()
bool _useExternalWeight
Definition: RivetAnalyzer.h:41
edm::EDGetTokenT< GenEventInfoProduct > _genEventInfoCollection
Definition: RivetAnalyzer.h:47
void RivetAnalyzer::beginJob ( void  )
overridevirtual

Reimplemented from edm::one::EDAnalyzerBase.

Definition at line 70 of file RivetAnalyzer.cc.

References AlCaHLTBitMon_QueryRunRegistry::string.

70  {
71  //set the environment, very ugly but rivet is monolithic when it comes to paths
72  char* cmsswbase = getenv("CMSSW_BASE");
73  char* cmsswrelease = getenv("CMSSW_RELEASE_BASE");
74  if (!getenv("RIVET_REF_PATH")) {
75  const std::string rivetref = "RIVET_REF_PATH=" + string(cmsswbase) +
76  "/src/GeneratorInterface/RivetInterface/data:" + string(cmsswrelease) +
77  "/src/GeneratorInterface/RivetInterface/data";
78  char* rivetrefCstr = strdup(rivetref.c_str());
79  putenv(rivetrefCstr);
80  free(rivetrefCstr);
81  }
82  if (!getenv("RIVET_INFO_PATH")) {
83  const std::string rivetinfo = "RIVET_INFO_PATH=" + string(cmsswbase) +
84  "/src/GeneratorInterface/RivetInterface/data:" + string(cmsswrelease) +
85  "/src/GeneratorInterface/RivetInterface/data";
86  char* rivetinfoCstr = strdup(rivetinfo.c_str());
87  putenv(rivetinfoCstr);
88  free(rivetinfoCstr);
89  }
90 }
void RivetAnalyzer::beginRun ( const edm::Run iRun,
const edm::EventSetup iSetup 
)
override

Definition at line 92 of file RivetAnalyzer.cc.

92 { return; }
void RivetAnalyzer::endJob ( void  )
overridevirtual

List of registered analysis data objects.

Reimplemented from edm::one::EDAnalyzerBase.

Definition at line 168 of file RivetAnalyzer.cc.

168 {}
void RivetAnalyzer::endRun ( const edm::Run iRun,
const edm::EventSetup iSetup 
)
override

Definition at line 149 of file RivetAnalyzer.cc.

References _analysisHandler, _doFinalize, and _outFileName.

149  {
150  if (_doFinalize)
151  _analysisHandler.finalize();
152  else {
153  //if we don't finalize we just want to do the transformation from histograms to DPS
155  //normalizeTree();
156  }
157  _analysisHandler.writeData(_outFileName);
158 
159  return;
160 }
Rivet::AnalysisHandler _analysisHandler
Definition: RivetAnalyzer.h:48
std::string _outFileName
Definition: RivetAnalyzer.h:50
void RivetAnalyzer::normalizeTree ( )
private

Definition at line 170 of file RivetAnalyzer.cc.

References _analysisHandler, _mes, _produceDQM, dbe, DEFINE_FWK_MODULE, and nevent.

170  {
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);
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 }
int nevent
Definition: AMPTWrapper.h:74
Rivet::AnalysisHandler _analysisHandler
Definition: RivetAnalyzer.h:48
DQMStore * dbe
Definition: RivetAnalyzer.h:55
std::vector< MonitorElement * > _mes
Definition: RivetAnalyzer.h:56

Member Data Documentation

Rivet::AnalysisHandler RivetAnalyzer::_analysisHandler
private

Definition at line 48 of file RivetAnalyzer.h.

Referenced by analyze(), endRun(), normalizeTree(), and RivetAnalyzer().

bool RivetAnalyzer::_doFinalize
private

Definition at line 51 of file RivetAnalyzer.h.

Referenced by endRun().

edm::EDGetTokenT<GenEventInfoProduct> RivetAnalyzer::_genEventInfoCollection
private

Definition at line 47 of file RivetAnalyzer.h.

Referenced by analyze(), and RivetAnalyzer().

int RivetAnalyzer::_GENweightNumber
private

Definition at line 45 of file RivetAnalyzer.h.

Referenced by analyze(), and RivetAnalyzer().

edm::EDGetTokenT<edm::HepMCProduct> RivetAnalyzer::_hepmcCollection
private

Definition at line 40 of file RivetAnalyzer.h.

Referenced by analyze(), and RivetAnalyzer().

bool RivetAnalyzer::_isFirstEvent
private

Definition at line 49 of file RivetAnalyzer.h.

Referenced by analyze().

edm::EDGetTokenT<LHEEventProduct> RivetAnalyzer::_LHECollection
private

Definition at line 46 of file RivetAnalyzer.h.

Referenced by analyze(), and RivetAnalyzer().

int RivetAnalyzer::_LHEweightNumber
private

Definition at line 43 of file RivetAnalyzer.h.

Referenced by analyze(), and RivetAnalyzer().

std::vector<MonitorElement *> RivetAnalyzer::_mes
private

Definition at line 56 of file RivetAnalyzer.h.

Referenced by normalizeTree().

std::string RivetAnalyzer::_outFileName
private

Definition at line 50 of file RivetAnalyzer.h.

Referenced by endRun().

bool RivetAnalyzer::_produceDQM
private

Definition at line 52 of file RivetAnalyzer.h.

Referenced by normalizeTree(), and RivetAnalyzer().

bool RivetAnalyzer::_useExternalWeight
private

Definition at line 41 of file RivetAnalyzer.h.

Referenced by analyze(), and RivetAnalyzer().

bool RivetAnalyzer::_useGENweights
private

Definition at line 44 of file RivetAnalyzer.h.

Referenced by analyze(), and RivetAnalyzer().

bool RivetAnalyzer::_useLHEweights
private

Definition at line 42 of file RivetAnalyzer.h.

Referenced by analyze(), and RivetAnalyzer().

double RivetAnalyzer::_xsection
private

Definition at line 53 of file RivetAnalyzer.h.

Referenced by analyze(), and RivetAnalyzer().

DQMStore* RivetAnalyzer::dbe
private

Definition at line 55 of file RivetAnalyzer.h.

Referenced by normalizeTree(), and RivetAnalyzer().