Skip to content

Commit

Permalink
Merge pull request #10853 from perrozzi/CMSSW_7_5_X
Browse files Browse the repository at this point in the history
rivet with lhe weights
  • Loading branch information
cmsbuild committed Sep 23, 2015
2 parents ec16a08 + e6bf3f4 commit 704228d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 16 deletions.
3 changes: 3 additions & 0 deletions GeneratorInterface/RivetInterface/interface/RivetAnalyzer.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ class RivetAnalyzer : public edm::EDAnalyzer

edm::InputTag _hepmcCollection;
bool _useExternalWeight;
bool _useLHEweights;
int _LHEweightNumber;
edm::InputTag _LHECollection;
edm::InputTag _genEventInfoCollection;
Rivet::AnalysisHandler _analysisHandler;
bool _isFirstEvent;
Expand Down
48 changes: 32 additions & 16 deletions GeneratorInterface/RivetInterface/plugins/RivetAnalyzer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h"
#include "SimDataFormats/GeneratorProducts/interface/GenEventInfoProduct.h"
#include "SimDataFormats/GeneratorProducts/interface/LHEEventProduct.h"
#include "DataFormats/Common/interface/Handle.h"

#include "Rivet/AnalysisHandler.hh"
Expand Down Expand Up @@ -40,6 +41,10 @@ _produceDQM(pset.getParameter<bool>("ProduceDQMOutput"))
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 " ;
}
_genEventInfoCollection = pset.getParameter<edm::InputTag>("GenEventInfoCollection");
_LHECollection = pset.getParameter<edm::InputTag>("LHECollection");
_useLHEweights = pset.getParameter<bool>("useLHEweights");
_LHEweightNumber = pset.getParameter<int>("LHEweightNumber");

}

//get the analyses
Expand All @@ -55,7 +60,7 @@ _produceDQM(pset.getParameter<bool>("ProduceDQMOutput"))
xsection = pset.getParameter<double>("CrossSection");
for (iana = ibeg; iana != iend; ++iana){
if ((*iana)->needsCrossSection())
(*iana)->setCrossSection(xsection);
(*iana)->setCrossSection(xsection);
}
if (_produceDQM){
// book stuff needed for DQM
Expand Down Expand Up @@ -92,21 +97,32 @@ void RivetAnalyzer::analyze(const edm::Event& iEvent,const edm::EventSetup& iSet

// get HepMC GenEvent
const HepMC::GenEvent *myGenEvent = evt->GetEvent();
//if you want to use an external weight we have to clene the GenEvent and change the weight

//if you want to use an external weight we have to clone the GenEvent and change the weight
if ( _useExternalWeight ){

HepMC::GenEvent * tmpGenEvtPtr = new HepMC::GenEvent( *(evt->GetEvent()) );
if (tmpGenEvtPtr->weights().size() == 0) {
throw cms::Exception("RivetAnalyzer") << "Original weight container has 0 size ";
}
if (tmpGenEvtPtr->weights().size() > 1) {
edm::LogWarning("RivetAnalyzer") << "Original event weight size is " << tmpGenEvtPtr->weights().size() << ". Will change only the first one ";
}
edm::Handle<GenEventInfoProduct> genEventInfoProduct;
iEvent.getByLabel(_genEventInfoCollection, genEventInfoProduct);
tmpGenEvtPtr->weights()[0] = genEventInfoProduct->weight();
myGenEvent = tmpGenEvtPtr;
}

if(!_useLHEweights){
edm::Handle<GenEventInfoProduct> genEventInfoProduct;
iEvent.getByLabel(_genEventInfoCollection, genEventInfoProduct);
tmpGenEvtPtr->weights()[0] = genEventInfoProduct->weight();
}else{
edm::Handle<LHEEventProduct> lheEventHandle;
iEvent.getByLabel(_LHECollection,lheEventHandle);
const LHEEventProduct::WGT& wgt = lheEventHandle->weights().at(_LHEweightNumber);
tmpGenEvtPtr->weights()[0] = wgt.wgt;
}
myGenEvent = tmpGenEvtPtr;

}


//aaply the beams initialization on the first event
if (_isFirstEvent){
Expand All @@ -119,13 +135,13 @@ void RivetAnalyzer::analyze(const edm::Event& iEvent,const edm::EventSetup& iSet

//if we have cloned the GenEvent, we delete it
if ( _useExternalWeight )
delete myGenEvent;
delete myGenEvent;
}


void RivetAnalyzer::endRun(const edm::Run& iRun,const edm::EventSetup& iSetup){
if (_doFinalize)
_analysisHandler.finalize();
_analysisHandler.finalize();
else {
//if we don't finalize we just want to do the transformation from histograms to DPS
////normalizeTree(_analysisHandler.tree());
Expand All @@ -139,11 +155,11 @@ void RivetAnalyzer::endRun(const edm::Run& iRun,const edm::EventSetup& iSetup){



//from Rivet 2.X: Analysis.hh (cls 18Feb2014)
/// List of registered analysis data objects
//const vector<AnalysisObjectPtr>& analysisObjects() const {
//return _analysisobjects;
//}
//from Rivet 2.X: Analysis.hh (cls 18Feb2014)
/// List of registered analysis data objects
//const vector<AnalysisObjectPtr>& analysisObjects() const {
//return _analysisobjects;
//}



Expand Down Expand Up @@ -198,7 +214,7 @@ void RivetAnalyzer::normalizeTree() {
_analysisHandler.datapointsetFactory().create(path, *tmphisto);
}
//now convert to root and then ME
//need aida2flat (from Rivet 1.X) & flat2root here
//need aida2flat (from Rivet 1.X) & flat2root here
TH1F* h = aida2root<IHistogram1D, TH1F>(histo, basename);
if (_produceDQM)
_mes.push_back(dbe->book1D(h->GetName(), h));
Expand All @@ -212,7 +228,7 @@ void RivetAnalyzer::normalizeTree() {
_analysisHandler.datapointsetFactory().create(path, *tmpprof);
}
//now convert to root and then ME
//need aida2flat (from Rivet 1.X) & flat2root here
//need aida2flat (from Rivet 1.X) & flat2root here
TProfile* p = aida2root<IProfile1D, TProfile>(prof, basename);
if (_produceDQM)
_mes.push_back(dbe->bookProfile(p->GetName(), p));
Expand Down
3 changes: 3 additions & 0 deletions GeneratorInterface/RivetInterface/python/rivetAnalyzer_cfi.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
HepMCCollection = cms.InputTag('generator'),
UseExternalWeight = cms.bool(False),
GenEventInfoCollection = cms.InputTag('generator'),
useLHEweights = cms.bool(False),
LHEweightNumber = cms.int32(0),
LHECollection = cms.InputTag('source'),
CrossSection = cms.double(1000),
DoFinalize = cms.bool(True),
ProduceDQMOutput = cms.bool(False),
Expand Down

0 comments on commit 704228d

Please sign in to comment.