Skip to content

Commit

Permalink
Enable HGCal in phase2 premixing
Browse files Browse the repository at this point in the history
  • Loading branch information
makortel committed May 10, 2018
1 parent 03b3812 commit fcbee47
Show file tree
Hide file tree
Showing 20 changed files with 454 additions and 21 deletions.
3 changes: 3 additions & 0 deletions Configuration/StandardSequences/python/SimL1EmulatorDM_cff.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
#
simHcalTechTrigDigis.ttpDigiCollection = "DMHcalTTPDigis"
#
hgcalTriggerPrimitiveDigiProducer.eeDigis.setModuleLabel("mixData")
hgcalTriggerPrimitiveDigiProducer.fhDigis.setModuleLabel("mixData")
hgcalTriggerPrimitiveDigiProducer.bhDigis.setModuleLabel("mixData")

from Configuration.Eras.Modifier_stage2L1Trigger_cff import stage2L1Trigger
if not stage2L1Trigger.isChosen():
Expand Down
156 changes: 156 additions & 0 deletions DataFormats/HGCDigi/interface/PHGCSimAccumulator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
#ifndef DataFormats_HGCDigi_PHGCSimAccumulator_h
#define DataFormats_HGCDigi_PHGCSimAccumulator_h

#include "DataFormats/DetId/interface/DetId.h"

#include <vector>
#include <cassert>

class PHGCSimAccumulator {
public:
// These two structs are public only because of dictionary generation
class DetIdSize {
public:
// Use top 27 bits to hold the details of the DetId
constexpr static unsigned detIdOffset = 5;
constexpr static unsigned detIdMask = 0x7ffffff;
// Use the last 5 bits to index 2x15 elements
constexpr static unsigned sizeOffset = 27;
constexpr static unsigned sizeMask = 0x1f;
// With this arrangement increasing the size component is faster

DetIdSize(): detIdSize_(0) {}
DetIdSize(unsigned int detId): detIdSize_(detId << detIdOffset) {}

void increaseSize() {
assert(size()+1 < 1<<detIdOffset);
++detIdSize_;
}

unsigned int detIdDetails() const { return detIdSize_ >> detIdOffset; }
unsigned int size() const { return detIdSize_ & sizeMask; }

private:
unsigned int detIdSize_;
};
class Data {
public:
constexpr static unsigned energyOffset = 15;
constexpr static unsigned energyMask = 0x1;
constexpr static unsigned sampleOffset = 11;
constexpr static unsigned sampleMask = 0xf;
constexpr static unsigned dataOffset = 0;
constexpr static unsigned dataMask = 0x7ff;

Data(): data_(0) {}
Data(unsigned short ei, unsigned short si, unsigned short d):
data_((ei << energyOffset) | (si << sampleOffset) | d)
{}

unsigned int energyIndex() const { return data_ >> energyOffset; }
unsigned int sampleIndex() const { return (data_ >> sampleOffset) & sampleMask; }
unsigned int data() const { return data_ & dataMask; }

private:
unsigned short data_;
};

PHGCSimAccumulator() = default;
PHGCSimAccumulator(unsigned int detId): detSubdetId_(detId >> DetId::kSubdetOffset) {}
~PHGCSimAccumulator() = default;

void reserve(size_t size) {
detIdSize_.reserve(size);
data_.reserve(size);
}

void shrink_to_fit() {
detIdSize_.shrink_to_fit();
data_.shrink_to_fit();
}

void emplace_back(unsigned int detId, unsigned short energyIndex, unsigned short sampleIndex, unsigned short data) {
assert( (detId >> DetId::kSubdetOffset) == detSubdetId_ );

// TODO: add checks
if(detIdSize_.empty() || detIdSize_.back().detIdDetails() != (detId & DetIdSize::detIdMask)) {
detIdSize_.emplace_back(detId);
}
data_.emplace_back(energyIndex, sampleIndex, data);
detIdSize_.back().increaseSize();
}

class TmpElem {
public:
TmpElem(unsigned int detId, Data data): detId_(detId), data_(data) {}

unsigned int detId() const { return detId_; }
unsigned short energyIndex() const { return data_.energyIndex(); }
unsigned short sampleIndex() const { return data_.sampleIndex(); }
unsigned short data() const { return data_.data(); }
private:
unsigned int detId_;
Data data_;
};

class const_iterator {
public:
// begin
const_iterator(const PHGCSimAccumulator *acc):
acc_(acc), iDet_(0), iData_(0),
endData_(acc->detIdSize_.empty() ? 0 : acc->detIdSize_.front().size())
{}

// end
const_iterator(const PHGCSimAccumulator *acc, unsigned int detSize, unsigned int dataSize):
acc_(acc), iDet_(detSize), iData_(dataSize), endData_(0)
{}

bool operator==(const const_iterator& other) const {
return iDet_ == other.iDet_ && iData_ == other.iData_;
}
bool operator!=(const const_iterator& other) const {
return !operator==(other);
}
const_iterator& operator++() {
++iData_;
if(iData_ == endData_) {
++iDet_;
endData_ += (iDet_ == acc_->detIdSize_.size()) ? 0 : acc_->detIdSize_[iDet_].size();
}
return *this;
}
const_iterator operator++(int) {
auto tmp = *this;
++(*this);
return tmp;
}
TmpElem operator*() {
return TmpElem((acc_->detSubdetId_ << DetId::kSubdetOffset) | acc_->detIdSize_[iDet_].detIdDetails(),
acc_->data_[iData_]);
}

private:
const PHGCSimAccumulator *acc_;
unsigned int iDet_;
unsigned int iData_;
unsigned int endData_;
};

TmpElem back() const {
return TmpElem((detSubdetId_ << DetId::kSubdetOffset) | detIdSize_.back().detIdDetails(),
data_.back());
}

const_iterator cbegin() const { return const_iterator(this); }
const_iterator begin() const { return cbegin(); }
const_iterator cend() const { return const_iterator(this, detIdSize_.size(), data_.size()); }
const_iterator end() const { return cend(); }

private:
std::vector<DetIdSize> detIdSize_;
std::vector<Data> data_;
unsigned short detSubdetId_ = 0;
};

#endif
9 changes: 9 additions & 0 deletions DataFormats/HGCDigi/src/classes.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <vector>
#include "DataFormats/HGCDigi/interface/HGCDigiCollections.h"
#include "DataFormats/HGCDigi/interface/PHGCSimAccumulator.h"

namespace DataFormats_HGCDigi {
struct dictionary {
Expand Down Expand Up @@ -33,6 +34,14 @@ namespace DataFormats_HGCDigi {
edm::Wrapper< edm::SortedCollection< HGCDataFrame<HGCHEDetId,HGCSample> > > prodHGCHEDataFrames;
HGCHEDigiCollection dcHGCHE;
edm::Wrapper<HGCHEDigiCollection> wdcHGCHE;

// Sim cell accumulator (for premixing)
PHGCSimAccumulator saHGC;
PHGCSimAccumulator::Data saHGCdata;
PHGCSimAccumulator::DetIdSize saHGCdis;
std::vector<PHGCSimAccumulator::Data> vsaHGCdata;
std::vector<PHGCSimAccumulator::DetIdSize> vsaHGCdis;
edm::Wrapper<PHGCSimAccumulator> wsaHGC;
};
}

7 changes: 7 additions & 0 deletions DataFormats/HGCDigi/src/classes_def.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,11 @@
<class name="edm::SortedCollection<HGCDataFrame<HGCHEDetId,HGCSample>, edm::StrictWeakOrdering<HGCDataFrame<HGCHEDetId,HGCSample> > >"/>
<class name="edm::Wrapper<edm::SortedCollection<HGCDataFrame<HGCHEDetId,HGCSample>,edm::StrictWeakOrdering<HGCDataFrame<HGCHEDetId,HGCSample> > > >" />
<class name="edm::Wrapper<HGCHEDigiCollection>"/>

<class name="PHGCSimAccumulator"/>
<class name="PHGCSimAccumulator::Data"/>
<class name="PHGCSimAccumulator::DetIdSize"/>
<class name="std::vector<PHGCSimAccumulator::Data>"/>
<class name="std::vector<PHGCSimAccumulator::DetIdSize>"/>
<class name="edm::Wrapper<PHGCSimAccumulator>"/>
</lcgdict>
4 changes: 4 additions & 0 deletions L1Trigger/Configuration/python/SimL1Emulator_cff.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,13 @@
phase2_hgcal.toReplaceWith( SimL1Emulator , _phase2_siml1emulator )

# If PreMixing, don't run these modules during first step
# TODO: Do we actually need anything from here run in stage1?
from Configuration.ProcessModifiers.premix_stage1_cff import premix_stage1
premix_stage1.toReplaceWith(SimL1Emulator, SimL1Emulator.copyAndExclude([
SimL1TCalorimeter,
SimL1TechnicalTriggers,
SimL1TGlobal
]))
(premix_stage1 & phase2_hgcal).toReplaceWith(SimL1Emulator, SimL1Emulator.copyAndExclude([
hgcalTriggerPrimitives
]))
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,10 @@

algo = cms.string("HGCalUncalibRecHitWorkerWeights")
)

from Configuration.ProcessModifiers.premix_stage2_cff import premix_stage2
premix_stage2.toModify(HGCalUncalibRecHit,
HGCEEdigiCollection = 'mixData:HGCDigisEE',
HGCHEFdigiCollection = 'mixData:HGCDigisHEfront',
HGCHEBdigiCollection = 'mixData:HGCDigisHEback',
)
11 changes: 11 additions & 0 deletions SimCalorimetry/HGCalSimProducers/interface/HGCDigitizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "SimCalorimetry/HGCalSimProducers/interface/HGCHEfrontDigitizer.h"
#include "SimCalorimetry/HGCalSimProducers/interface/HGCHEbackDigitizer.h"
#include "DataFormats/HGCDigi/interface/HGCDigiCollections.h"
#include "DataFormats/HGCDigi/interface/PHGCSimAccumulator.h"
#include "FWCore/Framework/interface/ESHandle.h"
#include "Geometry/HGCalGeometry/interface/HGCalGeometry.h"
#include "Geometry/HcalTowerAlgo/interface/HcalGeometry.h"
Expand Down Expand Up @@ -55,6 +56,8 @@ class HGCDigitizer
void accumulate(PileUpEventPrincipal const& e, edm::EventSetup const& c, CLHEP::HepRandomEngine* hre);
template<typename GEOM>
void accumulate(edm::Handle<edm::PCaloHitContainer> const &hits, int bxCrossing,const GEOM *geom, CLHEP::HepRandomEngine* hre);
// for premixing
void accumulate(const PHGCSimAccumulator& simAccumulator);

/**
@short actions at the start/end of event
Expand Down Expand Up @@ -83,6 +86,14 @@ private :
//digitization type (it's up to the specializations to decide it's meaning)
int digitizationType_;

// if true, we're running mixing in premixing stage1 and have to produce the output differently
bool premixStage1_;

// Minimum charge threshold for premixing stage1
double premixStage1MinCharge_;
// Maximum charge for packing in premixing stage1
double premixStage1MaxCharge_;

//handle sim hits
int maxSimHitsAccTime_;
double bxTime_, ev_per_eh_pair_;
Expand Down
1 change: 1 addition & 0 deletions SimCalorimetry/HGCalSimProducers/plugins/BuildFile.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<use name="DataFormats/Candidate"/>
<use name="DataFormats/HepMCCandidate"/>
<use name="SimGeneral/MixingModule"/>
<use name="SimGeneral/PreMixingModule"/>
<use name="CommonTools/UtilAlgos"/>
<use name="SimCalorimetry/HGCalSimProducers"/>
<use name="geant4core"/>
Expand Down
17 changes: 11 additions & 6 deletions SimCalorimetry/HGCalSimProducers/plugins/HGCDigiProducer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,17 @@ HGCDigiProducer::HGCDigiProducer(edm::ParameterSet const& pset, edm::ProducerBas
edm::ConsumesCollector& iC) :
HGCDigiProducer(pset, iC)
{
if( theDigitizer_.producesEEDigis() )
mixMod.produces<HGCEEDigiCollection>(theDigitizer_.digiCollection());
if( theDigitizer_.producesHEfrontDigis() )
mixMod.produces<HGCHEDigiCollection>(theDigitizer_.digiCollection());
if( theDigitizer_.producesHEbackDigis() )
mixMod.produces<HGCBHDigiCollection>(theDigitizer_.digiCollection());
if(pset.getParameter<bool>("premixStage1")) {
mixMod.produces<PHGCSimAccumulator>(theDigitizer_.digiCollection());
}
else {
if( theDigitizer_.producesEEDigis() )
mixMod.produces<HGCEEDigiCollection>(theDigitizer_.digiCollection());
if( theDigitizer_.producesHEfrontDigis() )
mixMod.produces<HGCHEDigiCollection>(theDigitizer_.digiCollection());
if( theDigitizer_.producesHEbackDigis() )
mixMod.produces<HGCBHDigiCollection>(theDigitizer_.digiCollection());
}
}

HGCDigiProducer::HGCDigiProducer(edm::ParameterSet const& pset, edm::ConsumesCollector& iC) :
Expand Down
83 changes: 83 additions & 0 deletions SimCalorimetry/HGCalSimProducers/plugins/PreMixingHGCalWorker.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/EventSetup.h"
#include "FWCore/Framework/interface/ConsumesCollector.h"
#include "FWCore/Framework/interface/ProducerBase.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "SimGeneral/MixingModule/interface/PileUpEventPrincipal.h"

#include "FWCore/ServiceRegistry/interface/Service.h"
#include "FWCore/Utilities/interface/RandomNumberGenerator.h"

#include "DataFormats/Common/interface/Handle.h"
#include "DataFormats/HGCDigi/interface/PHGCSimAccumulator.h"
#include "SimCalorimetry/HGCalSimProducers/interface/HGCDigitizer.h"

#include "SimGeneral/PreMixingModule/interface/PreMixingWorker.h"
#include "SimGeneral/PreMixingModule/interface/PreMixingWorkerFactory.h"

class PreMixingHGCalWorker: public PreMixingWorker {
public:
PreMixingHGCalWorker(const edm::ParameterSet& ps, edm::ProducerBase& producer, edm::ConsumesCollector&& iC);
~PreMixingHGCalWorker() override = default;

PreMixingHGCalWorker(const PreMixingHGCalWorker&) = delete;
PreMixingHGCalWorker& operator=(const PreMixingHGCalWorker&) = delete;

void beginRun(const edm::Run& run, const edm::EventSetup& ES) override;
void endRun() override;
void initializeEvent(const edm::Event &e, const edm::EventSetup& ES) override {}
void addSignals(const edm::Event &e, const edm::EventSetup& ES) override;
void addPileups(const PileUpEventPrincipal&, const edm::EventSetup& ES) override;
void put(edm::Event &e,const edm::EventSetup& ES, std::vector<PileupSummaryInfo> const& ps, int bs) override;

private:
edm::EDGetTokenT<PHGCSimAccumulator> signalToken_;

edm::InputTag pileInputTag_;

HGCDigitizer digitizer_;
};

PreMixingHGCalWorker::PreMixingHGCalWorker(const edm::ParameterSet& ps, edm::ProducerBase& producer, edm::ConsumesCollector&& iC):
signalToken_(iC.consumes<PHGCSimAccumulator>(ps.getParameter<edm::InputTag>("digiTagSig"))),
pileInputTag_(ps.getParameter<edm::InputTag>("pileInputTag")),
digitizer_(ps, iC)
{
if(digitizer_.producesEEDigis()) {
producer.produces<HGCEEDigiCollection>(digitizer_.digiCollection());
}
if(digitizer_.producesHEfrontDigis()) {
producer.produces<HGCHEDigiCollection>(digitizer_.digiCollection());
}
if(digitizer_.producesHEbackDigis()) {
producer.produces<HGCBHDigiCollection>(digitizer_.digiCollection());
}
}

void PreMixingHGCalWorker::beginRun(const edm::Run& run, const edm::EventSetup& ES) {
digitizer_.beginRun(ES);
}

void PreMixingHGCalWorker::endRun() {
digitizer_.endRun();
}

void PreMixingHGCalWorker::addSignals(const edm::Event &e,const edm::EventSetup& ES) {
edm::Handle<PHGCSimAccumulator> handle;
e.getByToken(signalToken_, handle);
digitizer_.accumulate(*handle);
}

void PreMixingHGCalWorker::addPileups(const PileUpEventPrincipal& pep, const edm::EventSetup& ES) {
edm::Handle<PHGCSimAccumulator> handle;
pep.getByLabel(pileInputTag_, handle);
digitizer_.accumulate(*handle);
}

void PreMixingHGCalWorker::put(edm::Event &e,const edm::EventSetup& ES, std::vector<PileupSummaryInfo> const& ps, int bs) {
edm::Service<edm::RandomNumberGenerator> rng;
digitizer_.finalizeEvent(e, ES, &rng->getEngine(e.streamID()));
}

DEFINE_EDM_PLUGIN(PreMixingWorkerFactory, PreMixingHGCalWorker, "PreMixingHGCalWorker");
Loading

0 comments on commit fcbee47

Please sign in to comment.