Skip to content

Commit

Permalink
Merge pull request #34754 from fabiocos/fc-fixAnalyzers
Browse files Browse the repository at this point in the history
Modernization of several simulation standalone test EDAnalyzers
  • Loading branch information
cmsbuild committed Aug 5, 2021
2 parents d066681 + eaab48d commit 1259e6a
Show file tree
Hide file tree
Showing 16 changed files with 631 additions and 663 deletions.
5 changes: 4 additions & 1 deletion IOMC/EventVertexGenerators/test/BuildFile.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<environment>
<use name="DataFormats/Common"/>
<use name="FWCore/Framework"/>
<use name="root"/>
<use name="SimDataFormats/GeneratorProducts"/>
<use name="clhep"/>
<use name="root"/>
<library file="VtxTester.cc" name="VtxSmearingTest">
<use name="PhysicsTools/UtilAlgos"/>
<use name="FWCore/ServiceRegistry"/>
<flags EDM_PLUGIN="1"/>
</library>

Expand Down
149 changes: 76 additions & 73 deletions IOMC/EventVertexGenerators/test/VtxTester.cc
Original file line number Diff line number Diff line change
@@ -1,96 +1,99 @@
#include "FWCore/Framework/interface/Frameworkfwd.h"
#include "FWCore/Framework/interface/one/EDAnalyzer.h"
#include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
#include "FWCore/Utilities/interface/EDGetToken.h"
#include "DataFormats/Common/interface/ValidHandle.h"

#include <iostream>

#include "IOMC/EventVertexGenerators/test/VtxTester.h"

#include "FWCore/Framework/interface/Event.h"
#include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/Utilities/interface/InputTag.h"

// essentials !!!
#include "FWCore/Framework/interface/Event.h"
//#include "DataFormats/Common/interface/Handle.h"
#include "DataFormats/Common/interface/Handle.h"
#include "FWCore/ServiceRegistry/interface/Service.h"
#include "CommonTools/UtilAlgos/interface/TFileService.h"

#include "TFile.h"
#include "TH1.h"
#include "TH2.h"

#include "CLHEP/Units/PhysicalConstants.h"
#include "CLHEP/Units/SystemOfUnits.h"

#include "FWCore/Framework/interface/MakerMacros.h"

using namespace edm;
using namespace std;

VtxTester::VtxTester(const ParameterSet&) {
fOutputFile = 0;
fVtxHistz = 0;
fVtxHistx = 0;
fVtxHisty = 0;
fVtxHistxy = 0;
fPhiHistOrg = 0;
fPhiHistSmr = 0;
fEtaHistOrg = 0;
fEtaHistSmr = 0;
class VtxTester : public edm::one::EDAnalyzer<> {
public:
//
explicit VtxTester(const edm::ParameterSet&);
virtual ~VtxTester() {}

void beginJob() override {}
void analyze(const edm::Event&, const edm::EventSetup&) override;
void endJob() override {}

static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);

private:
edm::EDGetTokenT<edm::HepMCProduct> srcHepMCToken_;

TH1D* fVtxHistTime_;
TH1D* fVtxHistz_;
TH2D* fVtxHistzTime_;
TH1D* fVtxHistx_;
TH1D* fVtxHisty_;
TH2D* fVtxHistxy_;
TH1D* fPhiHist_;
TH1D* fEtaHist_;
};

VtxTester::VtxTester(const edm::ParameterSet& cfg)
: srcHepMCToken_(consumes<edm::HepMCProduct>(cfg.getParameter<edm::InputTag>("src"))) {
edm::Service<TFileService> fs;

fVtxHistTime_ = fs->make<TH1D>("VtxHistTime", "#vtx, t [ns]", 60, -0.9, 0.9);
fVtxHistz_ = fs->make<TH1D>("VtxHistz", "#vtx, z [mm]", 400, -200., 200.);
fVtxHistzTime_ = fs->make<TH2D>("VtxHistzTime", "#vtx time [ns] vs z [mm]", 400, -200., 200., 60, -0.9, 0.9);
fVtxHistx_ = fs->make<TH1D>("VtxHistx", "#vtx, x [mm]", 200, -1., 1.);
fVtxHisty_ = fs->make<TH1D>("VtxHisty", "#vtx, y [mm]", 200, -1., 1.);
fVtxHistxy_ = fs->make<TH2D>("VtxHistxy", "#vtx y vs x [mm]", 200, -1., 1., 200, -1., 1.);

fPhiHist_ = fs->make<TH1D>("PhiHist", "#vtx phi", 70, -3.5, 3.5);
fEtaHist_ = fs->make<TH1D>("EtaHist", "#vtx eta", 120, -6., 6.);
}

void VtxTester::beginJob() {
fOutputFile = new TFile("VtxTest.root", "RECREATE");
fVtxHistz = new TH1D("VtxHistz", "Test Z-spread", 100, -250., 250.);
fVtxHistx = new TH1D("VtxHistx", "Test X-spread", 500, -1., 1.);
fVtxHisty = new TH1D("VtxHisty", "Test Y-spread", 500, -1., 1.);
fVtxHistxy = new TH2D("VtxHistxy", "Test X-Y spread", 700, -1., 1., 700, -1., 1.);
void VtxTester::analyze(const edm::Event& e, const edm::EventSetup&) {
auto theGenEvent = makeValid(e.getHandle(srcHepMCToken_));

fPhiHistOrg = new TH1D("PhiHistOrg", "Test Phi, org.", 80, -4., 4.);
fPhiHistSmr = new TH1D("PhiHistSmr", "Test Phi, smr.", 80, -4., 4.);
fEtaHistOrg = new TH1D("EtaHistOrg", "Test Eta, org.", 80, -4., 4.);
fEtaHistSmr = new TH1D("EtaHistSmr", "Test Eta, smr.", 80, -4., 4.);
const HepMC::GenEvent* Evt = theGenEvent->GetEvent();

return;
}
// take only 1st vertex for now - it's been tested only of PGuns...

HepMC::GenEvent::vertex_const_iterator Vtx = Evt->vertices_begin();

fVtxHistTime_->Fill((*Vtx)->position().t() * CLHEP::mm / CLHEP::c_light);
fVtxHistz_->Fill((*Vtx)->position().z() * CLHEP::mm);
fVtxHistzTime_->Fill((*Vtx)->position().z() * CLHEP::mm, (*Vtx)->position().t() * CLHEP::mm / CLHEP::c_light);
fVtxHistx_->Fill((*Vtx)->position().x() * CLHEP::mm);
fVtxHisty_->Fill((*Vtx)->position().y() * CLHEP::mm);
fVtxHistxy_->Fill((*Vtx)->position().x() * CLHEP::mm, (*Vtx)->position().y() * CLHEP::mm);

void VtxTester::analyze(const Event& e, const EventSetup&) {
vector<Handle<HepMCProduct> > EvtHandles;
e.getManyByType(EvtHandles);

for (unsigned int i = 0; i < EvtHandles.size(); i++) {
if (EvtHandles[i].isValid()) {
const HepMC::GenEvent* Evt = EvtHandles[i]->GetEvent();

// take only 1st vertex for now - it's been tested only of PGuns...
//
HepMC::GenEvent::vertex_const_iterator Vtx = Evt->vertices_begin();

for (HepMC::GenEvent::particle_const_iterator Part = Evt->particles_begin(); Part != Evt->particles_end();
Part++) {
//HepLorentzVector Mom = (*Part)->momentum() ;
HepMC::FourVector Mom = (*Part)->momentum();
double Phi = Mom.phi();
double Eta = -log(tan(Mom.theta() / 2.));

//if ( EvtHandles[i].provenance()->moduleLabel() == "VtxSmeared" )
//{
fVtxHistz->Fill((*Vtx)->position().z());
fVtxHistx->Fill((*Vtx)->position().x());
fVtxHisty->Fill((*Vtx)->position().y());
fVtxHistxy->Fill((*Vtx)->position().x(), (*Vtx)->position().y());
fPhiHistSmr->Fill(Phi);
fEtaHistSmr->Fill(Eta);
//}
//else
//{
//fPhiHistOrg->Fill( Phi ) ;
//fEtaHistOrg->Fill( Eta ) ;
//}
}
}
for (HepMC::GenEvent::particle_const_iterator Part = Evt->particles_begin(); Part != Evt->particles_end(); Part++) {
HepMC::FourVector Mom = (*Part)->momentum();
double Phi = Mom.phi();
double Eta = -log(tan(Mom.theta() / 2.));

fPhiHist_->Fill(Phi);
fEtaHist_->Fill(Eta);
}

return;
}

void VtxTester::endJob() {
fOutputFile->Write();
fOutputFile->Close();

return;
void VtxTester::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
edm::ParameterSetDescription desc;
desc.add<edm::InputTag>("src", edm::InputTag("generatorSmeared"))
->setComment("Input generated HepMC event after vtx smearing");
descriptions.add("vtxTester", desc);
}

DEFINE_FWK_MODULE(VtxTester);
34 changes: 0 additions & 34 deletions IOMC/EventVertexGenerators/test/VtxTester.h

This file was deleted.

72 changes: 72 additions & 0 deletions IOMC/EventVertexGenerators/test/test_vertex_cfg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import FWCore.ParameterSet.Config as cms

process = cms.Process('TestVertex')

# import of standard configurations
process.load('Configuration.StandardSequences.Services_cff')
process.load('SimGeneral.HepPDTESSource.pythiapdt_cfi')
process.load('FWCore.MessageService.MessageLogger_cfi')
process.load('Configuration.EventContent.EventContent_cff')
process.load('Configuration.StandardSequences.Generator_cff')
process.load('IOMC.EventVertexGenerators.VtxSmearedHLLHC14TeV_cfi')
process.load('GeneratorInterface.Core.genFilterSummary_cff')
process.load('Configuration.StandardSequences.EndOfProcess_cff')

process.maxEvents = cms.untracked.PSet(
input = cms.untracked.int32(200000),
# input = cms.untracked.int32(100),
output = cms.optional.untracked.allowed(cms.int32,cms.PSet)
)

# Input source
process.source = cms.Source("EmptySource")

# Output definition

process.RAWSIMoutput = cms.OutputModule("PoolOutputModule",
fileName = cms.untracked.string('file:step1.root'),
outputCommands = process.RAWSIMEventContent.outputCommands,
)

# Additional output definition

# Other statements
process.genstepfilter.triggerConditions=cms.vstring("generation_step")

process.generator = cms.EDFilter("Pythia8PtGun",
PGunParameters = cms.PSet(
AddAntiParticle = cms.bool(True),
MaxEta = cms.double(2.85),
MaxPhi = cms.double(3.14159265359),
MaxPt = cms.double(100.0),
MinEta = cms.double(-2.85),
MinPhi = cms.double(-3.14159265359),
MinPt = cms.double(2.0),
ParticleID = cms.vint32(-13)
),
PythiaParameters = cms.PSet(
parameterSets = cms.vstring()
),
Verbosity = cms.untracked.int32(0),
firstRun = cms.untracked.uint32(1),
psethack = cms.string('single mu pt 2 to 100')
)

process.vtxtester = cms.EDAnalyzer("VtxTester")

process.TFileService = cms.Service("TFileService",
fileName = cms.string('vtxTester.root')
)

# Path and EndPath definitions
process.generation_step = cms.Path(process.pgen+process.vtxtester)
process.genfiltersummary_step = cms.EndPath(process.genFilterSummary)
process.RAWSIMoutput_step = cms.EndPath(process.RAWSIMoutput)

# Schedule definition
process.schedule = cms.Schedule(process.generation_step,process.genfiltersummary_step,process.RAWSIMoutput_step)
# filter all path with the production filter sequence
for path in process.paths:
getattr(process,path).insert(0, process.generator)

process.MessageLogger.cerr.FwkReport.reportEvery = 10000
2 changes: 2 additions & 0 deletions PhysicsTools/HepMCCandAlgos/test/BuildFile.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<library file="DummyHepMCAnalyzer.cc">
<use name="PhysicsTools/UtilAlgos"/>
<use name="FWCore/ServiceRegistry"/>
<use name="PhysicsTools/HepMCCandAlgos"/>
<use name="SimDataFormats/GeneratorProducts"/>
<use name="FWCore/Framework"/>
Expand Down
66 changes: 38 additions & 28 deletions PhysicsTools/HepMCCandAlgos/test/DummyHepMCAnalyzer.cc
Original file line number Diff line number Diff line change
@@ -1,55 +1,65 @@
#include "FWCore/Framework/interface/EDAnalyzer.h"
#include "FWCore/Framework/interface/one/EDAnalyzer.h"
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/EventSetup.h"
#include "DataFormats/Common/interface/Handle.h"
#include "FWCore/Framework/interface/ESHandle.h"
#include "DataFormats/Common/interface/ValidHandle.h"
#include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
#include "FWCore/Utilities/interface/EDGetToken.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/Utilities/interface/InputTag.h"
#include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h"
#include "SimGeneral/HepPDTRecord/interface/ParticleDataTable.h"
#include "FWCore/Utilities/interface/EDMException.h"
#include <iostream>
using namespace std;
using namespace edm;
using namespace HepMC;
#include "FWCore/MessageLogger/interface/MessageLogger.h"

class DummyHepMCAnalyzer : public EDAnalyzer {
class DummyHepMCAnalyzer : public edm::one::EDAnalyzer<> {
private:
bool dumpHepMC_;
bool dumpPDF_;
bool checkPDG_;
edm::EDGetTokenT<edm::HepMCProduct> srcToken_;

public:
explicit DummyHepMCAnalyzer(const ParameterSet& cfg)
: dumpHepMC_(cfg.getUntrackedParameter<bool>("dumpHepMC", false)),
dumpPDF_(cfg.getUntrackedParameter<bool>("dumpPDF", false)),
checkPDG_(cfg.getUntrackedParameter<bool>("checkPDG", false)),
srcToken_(consumes<HepMCProduct>(cfg.getParameter<InputTag>("src"))) {}
explicit DummyHepMCAnalyzer(const edm::ParameterSet& cfg)
: dumpHepMC_(cfg.getUntrackedParameter<bool>("dumpHepMC")),
dumpPDF_(cfg.getUntrackedParameter<bool>("dumpPDF")),
checkPDG_(cfg.getUntrackedParameter<bool>("checkPDG")),
srcToken_(consumes<edm::HepMCProduct>(cfg.getParameter<edm::InputTag>("src"))) {}

private:
void analyze(const Event& evt, const EventSetup& es) override {
Handle<HepMCProduct> hepMC;
evt.getByToken(srcToken_, hepMC);
const GenEvent* mc = hepMC->GetEvent();
if (mc == 0)
throw edm::Exception(edm::errors::InvalidReference) << "HepMC has null pointer to GenEvent" << endl;
const size_t size = mc->particles_size();
cout << "\n particles #: " << size << endl;
if (dumpPDF_)
std::cout << "\n PDF info: " << mc->pdf_info() << std::endl;
if (dumpHepMC_)
static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
edm::ParameterSetDescription desc;
desc.addUntracked<bool>("dumpHepMC", true);
desc.addUntracked<bool>("dumpPDF", false);
desc.addUntracked<bool>("checkPDG", false);
desc.add<edm::InputTag>("src", edm::InputTag("generatorSmeared"))
->setComment("Input generated HepMC event after vtx smearing");
descriptions.add("dummyHepMCAnalyzer", desc);
}

void analyze(const edm::Event& evt, const edm::EventSetup& es) override {
auto hepMC = makeValid(evt.getHandle(srcToken_));

const HepMC::GenEvent* mc = hepMC->GetEvent();
edm::LogPrint("HepMCAnalyzer") << "\n particles #: " << mc->particles_size();

if (dumpPDF_) {
edm::LogPrint("HepMCAnalyzer") << "\n PDF info: " << mc->pdf_info();
}

if (dumpHepMC_) {
mc->print(std::cout);
}

if (checkPDG_) {
edm::ESHandle<HepPDT::ParticleDataTable> fPDGTable;
es.getData(fPDGTable);
for (HepMC::GenEvent::particle_const_iterator part = mc->particles_begin(); part != mc->particles_end(); ++part) {
const HepPDT::ParticleData* PData = fPDGTable->particle(HepPDT::ParticleID((*part)->pdg_id()));
if (!PData)
std::cout << "Missing entry in particle table for PDG code = " << (*part)->pdg_id() << std::endl;
if (!PData) {
edm::LogWarning("HepMCAnalyzer") << "Missing entry in particle table for PDG code = " << (*part)->pdg_id();
}
}
}
}
EDGetTokenT<HepMCProduct> srcToken_;
};

#include "FWCore/Framework/interface/MakerMacros.h"
Expand Down

0 comments on commit 1259e6a

Please sign in to comment.