Skip to content

Commit

Permalink
Merge pull request #36984 from bsunanda/Run3-sim109A
Browse files Browse the repository at this point in the history
Run3-sim109A Cleanup some of the SimG4CMS/Calo code
  • Loading branch information
cmsbuild authored Feb 17, 2022
2 parents 1310939 + 7b21eda commit 2215975
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 153 deletions.
23 changes: 11 additions & 12 deletions SimG4CMS/Calo/plugins/CaloSimHitAnalysis.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
#include "FWCore/Utilities/interface/Exception.h"
#include "FWCore/Utilities/interface/InputTag.h"
#include "FWCore/Utilities/interface/transform.h"

#include "FWCore/ServiceRegistry/interface/Service.h"
#include "CommonTools/UtilAlgos/interface/TFileService.h"
Expand Down Expand Up @@ -65,8 +66,8 @@ class CaloSimHitAnalysis : public edm::one::EDAnalyzer<edm::one::WatchRuns, edm:
const int allSteps_;
const std::vector<std::string> detNames_;
const edm::ESGetToken<CaloGeometry, CaloGeometryRecord> tokGeom_;
std::vector<edm::EDGetTokenT<edm::PCaloHitContainer> > toks_calo_;
edm::EDGetTokenT<edm::PassiveHitContainer> tok_passive_;
const std::vector<edm::EDGetTokenT<edm::PCaloHitContainer> > toks_calo_;
const edm::EDGetTokenT<edm::PassiveHitContainer> tok_passive_;

const CaloGeometry* caloGeometry_;
const HcalGeometry* hcalGeom_;
Expand Down Expand Up @@ -96,14 +97,14 @@ CaloSimHitAnalysis::CaloSimHitAnalysis(const edm::ParameterSet& ps)
passive_(ps.getUntrackedParameter<bool>("passiveHits", false)),
allSteps_(ps.getUntrackedParameter<int>("allSteps", 100)),
detNames_(ps.getUntrackedParameter<std::vector<std::string> >("detNames")),
tokGeom_(esConsumes<CaloGeometry, CaloGeometryRecord>()) {
tokGeom_(esConsumes<CaloGeometry, CaloGeometryRecord>()),
toks_calo_{edm::vector_transform(hitLab_,
[this](const std::string& name) {
return consumes<edm::PCaloHitContainer>(edm::InputTag{g4Label_, name});
})},
tok_passive_(consumes<edm::PassiveHitContainer>(edm::InputTag(g4Label_, "AllPassiveHits"))) {
usesResource(TFileService::kSharedResource);

// register for data access
for (unsigned int i = 0; i < hitLab_.size(); i++)
toks_calo_.emplace_back(consumes<edm::PCaloHitContainer>(edm::InputTag(g4Label_, hitLab_[i])));
tok_passive_ = consumes<edm::PassiveHitContainer>(edm::InputTag(g4Label_, "AllPassiveHits"));

edm::LogVerbatim("HitStudy") << "Module Label: " << g4Label_ << " Hits|timeSliceUnit:";
for (unsigned int i = 0; i < hitLab_.size(); i++)
edm::LogVerbatim("HitStudy") << "[" << i << "] " << hitLab_[i] << " " << timeSliceUnit_[i];
Expand Down Expand Up @@ -282,8 +283,7 @@ void CaloSimHitAnalysis::analyze(edm::Event const& e, edm::EventSetup const& set
hcalGeom_ = static_cast<const HcalGeometry*>(caloGeometry_->getSubdetectorGeometry(DetId::Hcal, HcalBarrel));

for (unsigned int i = 0; i < toks_calo_.size(); i++) {
edm::Handle<edm::PCaloHitContainer> hitsCalo;
e.getByToken(toks_calo_[i], hitsCalo);
const edm::Handle<edm::PCaloHitContainer>& hitsCalo = e.getHandle(toks_calo_[i]);
bool getHits = (hitsCalo.isValid());
#ifdef EDM_ML_DEBUG
edm::LogVerbatim("HitStudy") << "CaloSimHitAnalysis: Input flags Hits[" << i << "]: " << getHits;
Expand All @@ -299,8 +299,7 @@ void CaloSimHitAnalysis::analyze(edm::Event const& e, edm::EventSetup const& set
}

if (passive_) {
edm::Handle<edm::PassiveHitContainer> hitsPassive;
e.getByToken(tok_passive_, hitsPassive);
const edm::Handle<edm::PassiveHitContainer>& hitsPassive = e.getHandle(tok_passive_);
bool getHits = (hitsPassive.isValid());
#ifdef EDM_ML_DEBUG
edm::LogVerbatim("HitStudy") << "CaloSimHitAnalysis: Passive: " << getHits;
Expand Down
142 changes: 66 additions & 76 deletions SimG4CMS/Calo/plugins/CaloSimHitStudy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
#include "FWCore/Utilities/interface/Exception.h"
#include "FWCore/Utilities/interface/InputTag.h"
#include "FWCore/Utilities/interface/transform.h"

#include "FWCore/ServiceRegistry/interface/Service.h"
#include "CommonTools/UtilAlgos/interface/TFileService.h"
Expand Down Expand Up @@ -48,74 +49,68 @@ class CaloSimHitStudy : public edm::one::EDAnalyzer<edm::one::WatchRuns, edm::on
void endRun(edm::Run const&, edm::EventSetup const&) override {}

void analyzeHits(std::vector<PCaloHit>&, int);
void analyzeHits(edm::Handle<edm::PSimHitContainer>&, int);
void analyzeHits(const edm::Handle<edm::PSimHitContainer>&, int);
void analyzeHits(std::vector<PCaloHit>&, std::vector<PCaloHit>&, std::vector<PCaloHit>&);

private:
std::string g4Label, hitLab[4];
edm::EDGetTokenT<edm::HepMCProduct> tok_evt_;
edm::EDGetTokenT<edm::PCaloHitContainer> toks_calo_[4];
edm::EDGetTokenT<edm::PSimHitContainer> toks_track_[4];
edm::EDGetTokenT<edm::PSimHitContainer> toks_tkHigh_[6];
edm::EDGetTokenT<edm::PSimHitContainer> toks_tkLow_[6];
std::string muonLab[4], tkHighLab[6], tkLowLab[6];
double tmax_, eMIP_;
bool storeRL_, testNumber_;
const std::string g4Label_;
const std::vector<std::string> hitLab_;
const double maxEnergy_, tmax_, eMIP_;
const bool storeRL_, testNumber_;
const edm::EDGetTokenT<edm::HepMCProduct> tok_evt_;
const std::vector<edm::EDGetTokenT<edm::PCaloHitContainer>> toks_calo_;
const std::vector<edm::EDGetTokenT<edm::PSimHitContainer>> toks_track_;
const std::vector<edm::EDGetTokenT<edm::PSimHitContainer>> toks_tkHigh_;
const std::vector<edm::EDGetTokenT<edm::PSimHitContainer>> toks_tkLow_;

const std::vector<std::string> muonLab_ = {"MuonRPCHits", "MuonCSCHits", "MuonDTHits", "MuonGEMHits"};
const std::vector<std::string> tkHighLab_ = {"TrackerHitsPixelBarrelHighTof",
"TrackerHitsPixelEndcapHighTof",
"TrackerHitsTECHighTof",
"TrackerHitsTIBHighTof",
"TrackerHitsTIDHighTof",
"TrackerHitsTOBHighTof"};
const std::vector<std::string> tkLowLab_ = {"TrackerHitsPixelBarrelLowTof",
"TrackerHitsPixelEndcapLowTof",
"TrackerHitsTECLowTof",
"TrackerHitsTIBLowTof",
"TrackerHitsTIDLowTof",
"TrackerHitsTOBLowTof"};

TH1F *hit_[9], *time_[9], *edepEM_[9], *edepHad_[9], *edep_[9];
TH1F *etot_[9], *etotg_[9], *timeAll_[9], *hitMu, *hitHigh;
TH1F *hitLow, *eneInc_, *etaInc_, *phiInc_, *ptInc_;
TH1F *hitTk_[16], *edepTk_[16], *tofTk_[16], *edepC_[9], *edepT_[9];
};

CaloSimHitStudy::CaloSimHitStudy(const edm::ParameterSet& ps) {
CaloSimHitStudy::CaloSimHitStudy(const edm::ParameterSet& ps)
: g4Label_(ps.getUntrackedParameter<std::string>("ModuleLabel")),
hitLab_(ps.getUntrackedParameter<std::vector<std::string>>("CaloCollection")),
maxEnergy_(ps.getUntrackedParameter<double>("MaxEnergy", 200.0)),
tmax_(ps.getUntrackedParameter<double>("TimeCut", 100.0)),
eMIP_(ps.getUntrackedParameter<double>("MIPCut", 0.70)),
storeRL_(ps.getUntrackedParameter<bool>("StoreRL", false)),
testNumber_(ps.getUntrackedParameter<bool>("TestNumbering", true)),
tok_evt_(consumes<edm::HepMCProduct>(edm::InputTag(ps.getUntrackedParameter<std::string>("SourceLabel")))),
toks_calo_{edm::vector_transform(hitLab_,
[this](const std::string& name) {
return consumes<edm::PCaloHitContainer>(edm::InputTag{g4Label_, name});
})},
toks_track_{edm::vector_transform(muonLab_,
[this](const std::string& name) {
return consumes<edm::PSimHitContainer>(edm::InputTag{g4Label_, name});
})},
toks_tkHigh_{edm::vector_transform(tkHighLab_,
[this](const std::string& name) {
return consumes<edm::PSimHitContainer>(edm::InputTag{g4Label_, name});
})},
toks_tkLow_{edm::vector_transform(tkLowLab_, [this](const std::string& name) {
return consumes<edm::PSimHitContainer>(edm::InputTag{g4Label_, name});
})} {
usesResource(TFileService::kSharedResource);

tok_evt_ =
consumes<edm::HepMCProduct>(edm::InputTag(ps.getUntrackedParameter<std::string>("SourceLabel", "VtxSmeared")));
g4Label = ps.getUntrackedParameter<std::string>("ModuleLabel", "g4SimHits");
hitLab[0] = ps.getUntrackedParameter<std::string>("EBCollection", "EcalHitsEB");
hitLab[1] = ps.getUntrackedParameter<std::string>("EECollection", "EcalHitsEE");
hitLab[2] = ps.getUntrackedParameter<std::string>("ESCollection", "EcalHitsES");
hitLab[3] = ps.getUntrackedParameter<std::string>("HCCollection", "HcalHits");

double maxEnergy_ = ps.getUntrackedParameter<double>("MaxEnergy", 200.0);
tmax_ = ps.getUntrackedParameter<double>("TimeCut", 100.0);
eMIP_ = ps.getUntrackedParameter<double>("MIPCut", 0.70);
storeRL_ = ps.getUntrackedParameter<bool>("StoreRL", false);
testNumber_ = ps.getUntrackedParameter<bool>("TestNumbering", true);

muonLab[0] = "MuonRPCHits";
muonLab[1] = "MuonCSCHits";
muonLab[2] = "MuonDTHits";
muonLab[3] = "MuonGEMHits";
tkHighLab[0] = "TrackerHitsPixelBarrelHighTof";
tkHighLab[1] = "TrackerHitsPixelEndcapHighTof";
tkHighLab[2] = "TrackerHitsTECHighTof";
tkHighLab[3] = "TrackerHitsTIBHighTof";
tkHighLab[4] = "TrackerHitsTIDHighTof";
tkHighLab[5] = "TrackerHitsTOBHighTof";
tkLowLab[0] = "TrackerHitsPixelBarrelLowTof";
tkLowLab[1] = "TrackerHitsPixelEndcapLowTof";
tkLowLab[2] = "TrackerHitsTECLowTof";
tkLowLab[3] = "TrackerHitsTIBLowTof";
tkLowLab[4] = "TrackerHitsTIDLowTof";
tkLowLab[5] = "TrackerHitsTOBLowTof";

// register for data access
for (unsigned i = 0; i != 4; i++)
toks_calo_[i] = consumes<edm::PCaloHitContainer>(edm::InputTag(g4Label, hitLab[i]));

for (unsigned i = 0; i != 4; i++)
toks_track_[i] = consumes<edm::PSimHitContainer>(edm::InputTag(g4Label, muonLab[i]));

for (unsigned i = 0; i != 6; i++) {
toks_tkHigh_[i] = consumes<edm::PSimHitContainer>(edm::InputTag(g4Label, tkHighLab[i]));
toks_tkLow_[i] = consumes<edm::PSimHitContainer>(edm::InputTag(g4Label, tkLowLab[i]));
}

edm::LogVerbatim("HitStudy") << "Module Label: " << g4Label << " Hits: " << hitLab[0] << ", " << hitLab[1] << ", "
<< hitLab[2] << ", " << hitLab[3] << " MaxEnergy: " << maxEnergy_
edm::LogVerbatim("HitStudy") << "Module Label: " << g4Label_ << " Hits: " << hitLab_[0] << ", " << hitLab_[1]
<< ", " << hitLab_[2] << ", " << hitLab_[3] << " MaxEnergy: " << maxEnergy_
<< " Tmax: " << tmax_ << " MIP Cut: " << eMIP_;

edm::Service<TFileService> tfile;
Expand Down Expand Up @@ -277,12 +272,10 @@ CaloSimHitStudy::CaloSimHitStudy(const edm::ParameterSet& ps) {

void CaloSimHitStudy::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
edm::ParameterSetDescription desc;
std::vector<std::string> calonames = {"EcalHitsEB", "EcalHitsEE", "EcalHitsES", "HcalHits"};
desc.addUntracked<std::string>("SourceLabel", "generatorSmeared");
desc.addUntracked<std::string>("ModuleLabel", "g4SimHits");
desc.addUntracked<std::string>("EBCollection", "EcalHitsEB");
desc.addUntracked<std::string>("EECollection", "EcalHitsEE");
desc.addUntracked<std::string>("ESCollection", "EcalHitsES");
desc.addUntracked<std::string>("HCCollection", "HcalHits");
desc.addUntracked<std::vector<std::string>>("CaloCollection", calonames);
desc.addUntracked<double>("MaxEnergy", 200.0);
desc.addUntracked<double>("TimeCut", 100.0);
desc.addUntracked<double>("MIPCut", 0.70);
Expand All @@ -294,8 +287,7 @@ void CaloSimHitStudy::fillDescriptions(edm::ConfigurationDescriptions& descripti
void CaloSimHitStudy::analyze(edm::Event const& e, edm::EventSetup const&) {
edm::LogVerbatim("HitStudy") << "CaloSimHitStudy:Run = " << e.id().run() << " Event = " << e.id().event();

edm::Handle<edm::HepMCProduct> EvtHandle;
e.getByToken(tok_evt_, EvtHandle);
const edm::Handle<edm::HepMCProduct> EvtHandle = e.getHandle(tok_evt_);
const HepMC::GenEvent* myGenEvent = EvtHandle->GetEvent();

double eInc = 0, etaInc = 0, phiInc = 0;
Expand All @@ -312,10 +304,9 @@ void CaloSimHitStudy::analyze(edm::Event const& e, edm::EventSetup const&) {
phiInc_->Fill(phiInc);

std::vector<PCaloHit> ebHits, eeHits, hcHits;
for (int i = 0; i < 4; i++) {
for (unsigned int i = 0; i < toks_calo_.size(); i++) {
bool getHits = false;
edm::Handle<edm::PCaloHitContainer> hitsCalo;
e.getByToken(toks_calo_[i], hitsCalo);
const edm::Handle<edm::PCaloHitContainer>& hitsCalo = e.getHandle(toks_calo_[i]);
if (hitsCalo.isValid())
getHits = true;
#ifdef EDM_ML_DEBUG
Expand All @@ -339,9 +330,8 @@ void CaloSimHitStudy::analyze(edm::Event const& e, edm::EventSetup const&) {
analyzeHits(ebHits, eeHits, hcHits);

std::vector<PSimHit> muonHits;
edm::Handle<edm::PSimHitContainer> hitsTrack;
for (int i = 0; i < 4; i++) {
e.getByToken(toks_track_[i], hitsTrack);
for (unsigned int i = 0; i < toks_track_.size(); i++) {
const edm::Handle<edm::PSimHitContainer>& hitsTrack = e.getHandle(toks_track_[i]);
if (hitsTrack.isValid()) {
muonHits.insert(muonHits.end(), hitsTrack->begin(), hitsTrack->end());
analyzeHits(hitsTrack, i + 12);
Expand All @@ -350,8 +340,8 @@ void CaloSimHitStudy::analyze(edm::Event const& e, edm::EventSetup const&) {
unsigned int nhmu = muonHits.size();
hitMu->Fill(double(nhmu));
std::vector<PSimHit> tkHighHits;
for (int i = 0; i < 6; i++) {
e.getByToken(toks_tkHigh_[i], hitsTrack);
for (unsigned int i = 0; i < toks_tkHigh_.size(); i++) {
const edm::Handle<edm::PSimHitContainer>& hitsTrack = e.getHandle(toks_tkHigh_[i]);
if (hitsTrack.isValid()) {
tkHighHits.insert(tkHighHits.end(), hitsTrack->begin(), hitsTrack->end());
analyzeHits(hitsTrack, i);
Expand All @@ -360,8 +350,8 @@ void CaloSimHitStudy::analyze(edm::Event const& e, edm::EventSetup const&) {
unsigned int nhtkh = tkHighHits.size();
hitHigh->Fill(double(nhtkh));
std::vector<PSimHit> tkLowHits;
for (int i = 0; i < 6; i++) {
e.getByToken(toks_tkLow_[i], hitsTrack);
for (unsigned int i = 0; i < toks_tkLow_.size(); i++) {
const edm::Handle<edm::PSimHitContainer>& hitsTrack = e.getHandle(toks_tkLow_[i]);
if (hitsTrack.isValid()) {
tkLowHits.insert(tkLowHits.end(), hitsTrack->begin(), hitsTrack->end());
analyzeHits(hitsTrack, i + 6);
Expand Down Expand Up @@ -538,16 +528,16 @@ void CaloSimHitStudy::analyzeHits(std::vector<PCaloHit>& hits, int indx) {
}
}

void CaloSimHitStudy::analyzeHits(edm::Handle<edm::PSimHitContainer>& hits, int indx) {
void CaloSimHitStudy::analyzeHits(const edm::Handle<edm::PSimHitContainer>& hits, int indx) {
int nHit = 0;
edm::PSimHitContainer::const_iterator ihit;
std::string label(" ");
if (indx >= 0 && indx < 6)
label = tkHighLab[indx];
label = tkHighLab_[indx];
else if (indx >= 6 && indx < 12)
label = tkLowLab[indx - 6];
label = tkLowLab_[indx - 6];
else if (indx >= 12 && indx < 16)
label = muonLab[indx - 12];
label = muonLab_[indx - 12];
for (ihit = hits->begin(); ihit != hits->end(); ihit++) {
edepTk_[indx]->Fill(ihit->energyLoss());
tofTk_[indx]->Fill(ihit->timeOfFlight());
Expand Down
44 changes: 24 additions & 20 deletions SimG4CMS/Calo/plugins/CaloSteppingAction.cc
Original file line number Diff line number Diff line change
Expand Up @@ -106,36 +106,40 @@ class CaloSteppingAction : public SimProducer,
std::unique_ptr<HcalNumberingScheme> hcNumberingScheme_;
std::unique_ptr<CaloSlaveSD> slave_[nSD_];

std::vector<std::string> nameEBSD_, nameEESD_, nameHCSD_;
std::vector<std::string> nameHitC_;
const edm::ParameterSet iC_;
const std::vector<std::string> nameEBSD_, nameEESD_, nameHCSD_;
const std::vector<std::string> nameHitC_;
std::vector<const G4LogicalVolume*> volEBSD_, volEESD_, volHCSD_;
std::map<const G4LogicalVolume*, double> xtalMap_;
std::map<const G4LogicalVolume*, std::string> mapLV_;
int allSteps_, count_, eventID_;
double slopeLY_, birkC1EC_, birkSlopeEC_;
double birkCutEC_, birkC1HC_, birkC2HC_;
double birkC3HC_, timeSliceUnit_;
const int allSteps_;
const double slopeLY_, birkC1EC_, birkSlopeEC_;
const double birkCutEC_, birkC1HC_, birkC2HC_;
const double birkC3HC_, timeSliceUnit_;
int count_, eventID_;
std::map<std::pair<int, CaloHitID>, CaloGVHit> hitMap_[nSD_];
typedef std::tuple<const G4LogicalVolume*, uint32_t, int, int, double, double, double, double, double, double, double>
PassiveData;
std::vector<PassiveData> store_;
};

CaloSteppingAction::CaloSteppingAction(const edm::ParameterSet& p) : count_(0) {
CaloSteppingAction::CaloSteppingAction(const edm::ParameterSet& p)
: iC_(p.getParameter<edm::ParameterSet>("CaloSteppingAction")),
nameEBSD_(iC_.getParameter<std::vector<std::string> >("EBSDNames")),
nameEESD_(iC_.getParameter<std::vector<std::string> >("EESDNames")),
nameHCSD_(iC_.getParameter<std::vector<std::string> >("HCSDNames")),
nameHitC_(iC_.getParameter<std::vector<std::string> >("HitCollNames")),
allSteps_(iC_.getParameter<int>("AllSteps")),
slopeLY_(iC_.getParameter<double>("SlopeLightYield")),
birkC1EC_(iC_.getParameter<double>("BirkC1EC")),
birkSlopeEC_(iC_.getParameter<double>("BirkSlopeEC")),
birkCutEC_(iC_.getParameter<double>("BirkCutEC")),
birkC1HC_(iC_.getParameter<double>("BirkC1HC")),
birkC2HC_(iC_.getParameter<double>("BirkC2HC")),
birkC3HC_(iC_.getParameter<double>("BirkC3HC")),
timeSliceUnit_(iC_.getUntrackedParameter<double>("TimeSliceUnit", 1.0)),
count_(0) {
edm::ParameterSet iC = p.getParameter<edm::ParameterSet>("CaloSteppingAction");
nameEBSD_ = iC.getParameter<std::vector<std::string> >("EBSDNames");
nameEESD_ = iC.getParameter<std::vector<std::string> >("EESDNames");
nameHCSD_ = iC.getParameter<std::vector<std::string> >("HCSDNames");
nameHitC_ = iC.getParameter<std::vector<std::string> >("HitCollNames");
allSteps_ = iC.getParameter<int>("AllSteps");
slopeLY_ = iC.getParameter<double>("SlopeLightYield");
birkC1EC_ = iC.getParameter<double>("BirkC1EC");
birkSlopeEC_ = iC.getParameter<double>("BirkSlopeEC");
birkCutEC_ = iC.getParameter<double>("BirkCutEC");
birkC1HC_ = iC.getParameter<double>("BirkC1HC");
birkC2HC_ = iC.getParameter<double>("BirkC2HC");
birkC3HC_ = iC.getParameter<double>("BirkC3HC");
timeSliceUnit_ = iC.getUntrackedParameter<double>("TimeSliceUnit", 1.0);

edm::LogVerbatim("Step") << "CaloSteppingAction:: " << nameEBSD_.size() << " names for EB SD's";
for (unsigned int k = 0; k < nameEBSD_.size(); ++k)
Expand Down
3 changes: 1 addition & 2 deletions SimG4CMS/Calo/plugins/EcalSimHitDump.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ void EcalSimHitDump::analyze(const edm::Event& e, const edm::EventSetup&) {

if ((kount_ <= maxEvent_) || (maxEvent_ <= 0)) {
for (unsigned int k = 0; k < toksCalo_.size(); ++k) {
edm::Handle<edm::PCaloHitContainer> hitsCalo;
e.getByToken(toksCalo_[k], hitsCalo);
const edm::Handle<edm::PCaloHitContainer>& hitsCalo = e.getHandle(toksCalo_[k]);
if (hitsCalo.isValid())
edm::LogVerbatim("HitStudy") << "EcalSimHitDump: Input " << hitsCalo->size() << " hits of type " << types_[k];
unsigned int i(0);
Expand Down
Loading

0 comments on commit 2215975

Please sign in to comment.