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::EDAnalyzer 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::EDAnalyzer
void callWhenNewProductsRegistered (std::function< void(BranchDescription const &)> const &func)
 
 EDAnalyzer ()
 
SerialTaskQueueglobalLuminosityBlocksQueue ()
 
SerialTaskQueueglobalRunsQueue ()
 
ModuleDescription const & moduleDescription () const
 
std::string workerType () const
 
 ~EDAnalyzer () 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
 
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)
 
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::EDAnalyzer
typedef EDAnalyzer ModuleType
 
- Public Types inherited from edm::EDConsumerBase
typedef ProductLabels Labels
 
- Static Public Member Functions inherited from edm::EDAnalyzer
static const std::string & baseType ()
 
static void fillDescriptions (ConfigurationDescriptions &descriptions)
 
static void prevalidate (ConfigurationDescriptions &)
 
static bool wantsGlobalLuminosityBlocks ()
 
static bool wantsGlobalRuns ()
 
static bool wantsStreamLuminosityBlocks ()
 
static bool wantsStreamRuns ()
 
- 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 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.

15  :
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 {
25  //retrive the analysis name from paarmeter set
26  std::vector<std::string> analysisNames = pset.getParameter<std::vector<std::string> >("AnalysisNames");
27 
28  _hepmcCollection = consumes<HepMCProduct>(pset.getParameter<edm::InputTag>("HepMCCollection"));
29 
30  _useExternalWeight = pset.getParameter<bool>("UseExternalWeight");
31  if (_useExternalWeight) {
32  if (!pset.exists("GenEventInfoCollection")){
33  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 " ;
34  }
35 
36  _genEventInfoCollection = consumes<GenEventInfoProduct>(pset.getParameter<edm::InputTag>("GenEventInfoCollection"));
37  _useGENweights = pset.getParameter<bool>("useGENweights");
38  _GENweightNumber = pset.getParameter<int>("GENweightNumber");
39  _LHECollection = consumes<LHEEventProduct>(pset.getParameter<edm::InputTag>("LHECollection"));
40  _useLHEweights = pset.getParameter<bool>("useLHEweights");
41  _LHEweightNumber = pset.getParameter<int>("LHEweightNumber");
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, 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  _xsection = pset.getParameter<double>("CrossSection");
55  for (iana = ibeg; iana != iend; ++iana){
56  if ((*iana)->needsCrossSection())
57  (*iana)->setCrossSection(_xsection);
58  }
59  if (_produceDQM){
60  // book stuff needed for DQM
61  dbe = nullptr;
63  dbe->setVerbose(50);
64  }
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:50
edm::EDGetTokenT< edm::HepMCProduct > _hepmcCollection
Definition: RivetAnalyzer.h:42
double _xsection
Definition: RivetAnalyzer.h:55
DQMStore * dbe
Definition: RivetAnalyzer.h:57
std::string _outFileName
Definition: RivetAnalyzer.h:52
edm::EDGetTokenT< LHEEventProduct > _LHECollection
Definition: RivetAnalyzer.h:48
bool _useExternalWeight
Definition: RivetAnalyzer.h:43
edm::EDGetTokenT< GenEventInfoProduct > _genEventInfoCollection
Definition: RivetAnalyzer.h:49
RivetAnalyzer::~RivetAnalyzer ( )
override

Definition at line 68 of file RivetAnalyzer.cc.

68  {
69 }

Member Function Documentation

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

Definition at line 95 of file RivetAnalyzer.cc.

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

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

Reimplemented from edm::EDAnalyzer.

Definition at line 71 of file RivetAnalyzer.cc.

References AlCaHLTBitMon_QueryRunRegistry::string.

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

Definition at line 91 of file RivetAnalyzer.cc.

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

List of registered analysis data objects.

Reimplemented from edm::EDAnalyzer.

Definition at line 179 of file RivetAnalyzer.cc.

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

Definition at line 155 of file RivetAnalyzer.cc.

References _analysisHandler, _doFinalize, and _outFileName.

155  {
156  if (_doFinalize)
157  _analysisHandler.finalize();
158  else {
159  //if we don't finalize we just want to do the transformation from histograms to DPS
161  //normalizeTree();
162 
163  }
164  _analysisHandler.writeData(_outFileName);
165 
166  return;
167 }
Rivet::AnalysisHandler _analysisHandler
Definition: RivetAnalyzer.h:50
std::string _outFileName
Definition: RivetAnalyzer.h:52
void RivetAnalyzer::normalizeTree ( )
private

Definition at line 183 of file RivetAnalyzer.cc.

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

183  {
184  using namespace YODA;
185  std::vector<string> analyses = _analysisHandler.analysisNames();
186 
187  //tree.ls(".", true);
188  const string tmpdir = "/RivetNormalizeTmp";
189  //tree.mkdir(tmpdir);
190  foreach (const string& analysis, analyses) {
191  if (_produceDQM){
192  dbe->setCurrentFolder("Rivet/"+analysis);
193  //global variables that are always present
194  //sumOfWeights
195  TH1F nevent("nEvt", "n analyzed Events", 1, 0., 1.);
196  nevent.SetBinContent(1,_analysisHandler.sumOfWeights());
197  _mes.push_back(dbe->book1D("nEvt",&nevent));
198  }
199  //cross section
200  //TH1F xsection("xSection", "Cross Section", 1, 0., 1.);
201  //xsection.SetBinContent(1,_analysisHandler.crossSection());
202  //_mes.push_back(dbe->book1D("xSection",&xsection));
203  //now loop over the histograms
204 
205  /*
206  const vector<string> paths = tree.listObjectNames("/"+analysis, true); // args set recursive listing
207  std::cout << "Number of objects in YODA tree for analysis " << analysis << " = " << paths.size() << std::endl;
208  foreach (const string& path, paths) {
209  IManagedObject* hobj = tree.find(path);
210  if (hobj) {
211  // Weird seg fault on SLC4 when trying to dyn cast an IProfile ptr to a IHistogram
212  // Fix by attempting to cast to IProfile first, only try IHistogram if it fails.
213  IHistogram1D* histo = 0;
214  IProfile1D* prof = dynamic_cast<IProfile1D*>(hobj);
215  if (!prof) histo = dynamic_cast<IHistogram1D*>(hobj);
216 
217  std::cout << "Converting histo " << path << " to DPS" << std::endl;
218  tree.mv(path, tmpdir);
219  const size_t lastslash = path.find_last_of("/");
220  const string basename = path.substr(lastslash+1, path.length() - (lastslash+1));
221  const string tmppath = tmpdir + "/" + basename;
222 
223  // If it's a normal histo:
224  if (histo) {
225  IHistogram1D* tmphisto = dynamic_cast<IHistogram1D*>(tree.find(tmppath));
226  if (tmphisto) {
227  _analysisHandler.datapointsetFactory().create(path, *tmphisto);
228  }
229  //now convert to root and then ME
230  //need aida2flat (from Rivet 1.X) & flat2root here
231  TH1F* h = aida2root<IHistogram1D, TH1F>(histo, basename);
232  if (_produceDQM)
233  _mes.push_back(dbe->book1D(h->GetName(), h));
234  delete h;
235  tree.rm(tmppath);
236  }
237  // If it's a profile histo:
238  else if (prof) {
239  IProfile1D* tmpprof = dynamic_cast<IProfile1D*>(tree.find(tmppath));
240  if (tmpprof) {
241  _analysisHandler.datapointsetFactory().create(path, *tmpprof);
242  }
243  //now convert to root and then ME
244  //need aida2flat (from Rivet 1.X) & flat2root here
245  TProfile* p = aida2root<IProfile1D, TProfile>(prof, basename);
246  if (_produceDQM)
247  _mes.push_back(dbe->bookProfile(p->GetName(), p));
248  delete p;
249  tree.rm(tmppath);
250  }
251  }
252  }
253  */
254  }
255  //tree.rmdir(tmpdir);
256 
257 }
int nevent
Definition: AMPTWrapper.h:74
Rivet::AnalysisHandler _analysisHandler
Definition: RivetAnalyzer.h:50
DQMStore * dbe
Definition: RivetAnalyzer.h:57
std::vector< MonitorElement * > _mes
Definition: RivetAnalyzer.h:58

Member Data Documentation

Rivet::AnalysisHandler RivetAnalyzer::_analysisHandler
private

Definition at line 50 of file RivetAnalyzer.h.

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

bool RivetAnalyzer::_doFinalize
private

Definition at line 53 of file RivetAnalyzer.h.

Referenced by endRun().

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

Definition at line 49 of file RivetAnalyzer.h.

Referenced by analyze(), and RivetAnalyzer().

int RivetAnalyzer::_GENweightNumber
private

Definition at line 47 of file RivetAnalyzer.h.

Referenced by analyze(), and RivetAnalyzer().

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

Definition at line 42 of file RivetAnalyzer.h.

Referenced by analyze(), and RivetAnalyzer().

bool RivetAnalyzer::_isFirstEvent
private

Definition at line 51 of file RivetAnalyzer.h.

Referenced by analyze().

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

Definition at line 48 of file RivetAnalyzer.h.

Referenced by analyze(), and RivetAnalyzer().

int RivetAnalyzer::_LHEweightNumber
private

Definition at line 45 of file RivetAnalyzer.h.

Referenced by analyze(), and RivetAnalyzer().

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

Definition at line 58 of file RivetAnalyzer.h.

Referenced by normalizeTree().

std::string RivetAnalyzer::_outFileName
private

Definition at line 52 of file RivetAnalyzer.h.

Referenced by endRun().

bool RivetAnalyzer::_produceDQM
private

Definition at line 54 of file RivetAnalyzer.h.

Referenced by normalizeTree(), and RivetAnalyzer().

bool RivetAnalyzer::_useExternalWeight
private

Definition at line 43 of file RivetAnalyzer.h.

Referenced by analyze(), and RivetAnalyzer().

bool RivetAnalyzer::_useGENweights
private

Definition at line 46 of file RivetAnalyzer.h.

Referenced by analyze(), and RivetAnalyzer().

bool RivetAnalyzer::_useLHEweights
private

Definition at line 44 of file RivetAnalyzer.h.

Referenced by analyze(), and RivetAnalyzer().

double RivetAnalyzer::_xsection
private

Definition at line 55 of file RivetAnalyzer.h.

Referenced by analyze(), and RivetAnalyzer().

DQMStore* RivetAnalyzer::dbe
private

Definition at line 57 of file RivetAnalyzer.h.

Referenced by normalizeTree(), and RivetAnalyzer().