CMS 3D CMS Logo

PATMuonSlimmer.cc
Go to the documentation of this file.
1 
11 
15 
23 
24 namespace pat {
25 
27  public:
28  explicit PATMuonSlimmer(const edm::ParameterSet & iConfig);
29  ~PATMuonSlimmer() override { }
30 
31  void produce(edm::Event & iEvent, const edm::EventSetup & iSetup) override;
33 
34  private:
36  std::vector<edm::EDGetTokenT<reco::PFCandidateCollection>> pf_;
37  std::vector<edm::EDGetTokenT<edm::Association<pat::PackedCandidateCollection>>> pf2pc_;
38  const bool linkToPackedPF_;
42  std::unique_ptr<pat::ObjectModifier<pat::Muon> > muonModifier_;
43  };
44 
45 } // namespace
46 
48  src_(consumes<pat::MuonCollection>(iConfig.getParameter<edm::InputTag>("src"))),
49  linkToPackedPF_(iConfig.getParameter<bool>("linkToPackedPFCandidates")),
50  saveTeVMuons_(iConfig.getParameter<std::string>("saveTeVMuons")),
51  dropDirectionalIso_(iConfig.getParameter<std::string>("dropDirectionalIso")),
52  dropPfP4_(iConfig.getParameter<std::string>("dropPfP4")),
53  slimCaloVars_(iConfig.getParameter<std::string>("slimCaloVars")),
54  slimKinkVars_(iConfig.getParameter<std::string>("slimKinkVars")),
55  slimCaloMETCorr_(iConfig.getParameter<std::string>("slimCaloMETCorr")),
56  slimMatches_(iConfig.getParameter<std::string>("slimMatches")),
57  segmentsMuonSelection_(iConfig.getParameter<std::string>("segmentsMuonSelection")),
58  saveSegments_(iConfig.getParameter<bool>("saveSegments")),
59  modifyMuon_(iConfig.getParameter<bool>("modifyMuons"))
60 {
61  if (linkToPackedPF_) {
62  const std::vector<edm::InputTag> & pf = iConfig.getParameter<std::vector<edm::InputTag>>("pfCandidates");
63  const std::vector<edm::InputTag> & pf2pc = iConfig.getParameter<std::vector<edm::InputTag>>("packedPFCandidates");
64  if (pf.size() != pf2pc.size()) throw cms::Exception("Configuration") << "Mismatching pfCandidates and packedPFCandidates\n";
65  for (const edm::InputTag &tag : pf) pf_.push_back(consumes<reco::PFCandidateCollection>(tag));
66  for (const edm::InputTag &tag : pf2pc) pf2pc_.push_back(consumes<edm::Association<pat::PackedCandidateCollection>>(tag));
67  }
68 
69  edm::ConsumesCollector sumes(consumesCollector());
70  if( modifyMuon_ ) {
71  const edm::ParameterSet& mod_config = iConfig.getParameter<edm::ParameterSet>("modifierConfig");
72  muonModifier_.reset(new pat::ObjectModifier<pat::Muon>(mod_config) );
73  muonModifier_->setConsumes(sumes);
74  } else {
75  muonModifier_.reset(nullptr);
76  }
77  produces<std::vector<pat::Muon> >();
78  if( saveSegments_ ) {
79  produces< DTRecSegment4DCollection >();
80  produces< CSCSegmentCollection >();
81  }
82 
83 }
84 
85 void
87  if( modifyMuon_ ) muonModifier_->setEventContent(iSetup);
88 }
89 
90 void
92  using namespace edm;
93  using namespace std;
94 
96  iEvent.getByToken(src_, src);
97 
98  auto out = std::make_unique<std::vector<pat::Muon>>();
99  out->reserve(src->size());
100 
101  auto outDTSegments = std::make_unique<DTRecSegment4DCollection>();
102  std::set<DTRecSegment4DRef> dtSegmentsRefs;
103  auto outCSCSegments = std::make_unique<CSCSegmentCollection>();
104  std::set<CSCSegmentRef> cscSegmentsRefs;
105 
106  if( modifyMuon_ ) { muonModifier_->setEvent(iEvent); }
107 
108  std::map<reco::CandidatePtr,pat::PackedCandidateRef> mu2pc;
109  if (linkToPackedPF_) {
112  for (unsigned int ipfh = 0, npfh = pf_.size(); ipfh < npfh; ++ipfh) {
113  iEvent.getByToken(pf_[ipfh], pf);
114  iEvent.getByToken(pf2pc_[ipfh], pf2pc);
115  const auto & pfcoll = (*pf);
116  const auto & pfmap = (*pf2pc);
117  for (unsigned int i = 0, n = pf->size(); i < n; ++i) {
118  const reco::PFCandidate &p = pfcoll[i];
119  if (p.muonRef().isNonnull()) mu2pc[refToPtr(p.muonRef())] = pfmap[reco::PFCandidateRef(pf, i)];
120  }
121  }
122  }
123 
124  for (vector<pat::Muon>::const_iterator it = src->begin(), ed = src->end(); it != ed; ++it) {
125  out->push_back(*it);
126  pat::Muon & mu = out->back();
127 
128  if( modifyMuon_ ) { muonModifier_->modify(mu); }
129 
130  if (saveTeVMuons_(mu)){mu.embedPickyMuon(); mu.embedTpfmsMuon(); mu.embedDytMuon();}
131  if (linkToPackedPF_) {
132  mu.refToOrig_ = refToPtr(mu2pc[mu.refToOrig_]);
133  }
134  if (dropDirectionalIso_(mu)) {
136  mu.setPFIsolation("pfIsoMeanDRProfileR03",zero);
137  mu.setPFIsolation("pfIsoSumDRProfileR03",zero);
138  mu.setPFIsolation("pfIsoMeanDRProfileR04",zero);
139  mu.setPFIsolation("pfIsoSumDRProfileR04",zero);
140  }
142  if (slimCaloVars_(mu) && mu.isEnergyValid()) {
143  reco::MuonEnergy ene = mu.calEnergy();
144  if (ene.tower) ene.tower = MiniFloatConverter::reduceMantissaToNbitsRounding<12>(ene.tower);
145  if (ene.towerS9) ene.towerS9 = MiniFloatConverter::reduceMantissaToNbitsRounding<12>(ene.towerS9);
146  if (ene.had) ene.had = MiniFloatConverter::reduceMantissaToNbitsRounding<12>(ene.had);
147  if (ene.hadS9) ene.hadS9 = MiniFloatConverter::reduceMantissaToNbitsRounding<12>(ene.hadS9);
148  if (ene.hadMax) ene.hadMax = MiniFloatConverter::reduceMantissaToNbitsRounding<12>(ene.hadMax);
149  if (ene.em) ene.em = MiniFloatConverter::reduceMantissaToNbitsRounding<12>(ene.em);
150  if (ene.emS25) ene.emS25 = MiniFloatConverter::reduceMantissaToNbitsRounding<12>(ene.emS25);
151  if (ene.emMax) ene.emMax = MiniFloatConverter::reduceMantissaToNbitsRounding<12>(ene.emMax);
152  if (ene.hcal_time) ene.hcal_time = MiniFloatConverter::reduceMantissaToNbitsRounding<12>(ene.hcal_time);
153  if (ene.hcal_timeError) ene.hcal_timeError = MiniFloatConverter::reduceMantissaToNbitsRounding<12>(ene.hcal_timeError);
154  if (ene.ecal_time) ene.ecal_time = MiniFloatConverter::reduceMantissaToNbitsRounding<12>(ene.ecal_time);
155  if (ene.ecal_timeError) ene.ecal_timeError = MiniFloatConverter::reduceMantissaToNbitsRounding<12>(ene.ecal_timeError);
156  ene.ecal_position = math::XYZPointF(MiniFloatConverter::reduceMantissaToNbitsRounding<14>(ene.ecal_position.X()),
157  MiniFloatConverter::reduceMantissaToNbitsRounding<14>(ene.ecal_position.Y()),
158  MiniFloatConverter::reduceMantissaToNbitsRounding<14>(ene.ecal_position.Z()));
159  ene.hcal_position = math::XYZPointF(MiniFloatConverter::reduceMantissaToNbitsRounding<12>(ene.hcal_position.X()),
160  MiniFloatConverter::reduceMantissaToNbitsRounding<12>(ene.hcal_position.Y()),
161  MiniFloatConverter::reduceMantissaToNbitsRounding<12>(ene.hcal_position.Z()));
162  mu.setCalEnergy(ene);
163  }
164  if (slimKinkVars_(mu) && mu.isQualityValid()) {
166  qual.tkKink_position = math::XYZPointF(MiniFloatConverter::reduceMantissaToNbitsRounding<12>(qual.tkKink_position.X()),
167  MiniFloatConverter::reduceMantissaToNbitsRounding<12>(qual.tkKink_position.Y()),
168  MiniFloatConverter::reduceMantissaToNbitsRounding<12>(qual.tkKink_position.Z()));
169  mu.setCombinedQuality(qual);
170  }
173  corrs = reco::MuonMETCorrectionData(corrs.type(), MiniFloatConverter::reduceMantissaToNbitsRounding<10>(corrs.corrX()), MiniFloatConverter::reduceMantissaToNbitsRounding<10>(corrs.corrY()));
174  mu.embedCaloMETMuonCorrs(corrs);
175  }
176  if (slimMatches_(mu) && mu.isMatchesValid()) {
177  for (reco::MuonChamberMatch & cmatch : mu.matches()) {
178  cmatch.edgeX = MiniFloatConverter::reduceMantissaToNbitsRounding<12>(cmatch.edgeX);
179  cmatch.edgeY = MiniFloatConverter::reduceMantissaToNbitsRounding<12>(cmatch.edgeY);
180  cmatch.xErr = MiniFloatConverter::reduceMantissaToNbitsRounding<12>(cmatch.xErr);
181  cmatch.yErr = MiniFloatConverter::reduceMantissaToNbitsRounding<12>(cmatch.yErr);
182  cmatch.dXdZErr = MiniFloatConverter::reduceMantissaToNbitsRounding<12>(cmatch.dXdZErr);
183  cmatch.dYdZErr = MiniFloatConverter::reduceMantissaToNbitsRounding<12>(cmatch.dYdZErr);
184  for (reco::MuonSegmentMatch & smatch : cmatch.segmentMatches) {
185  smatch.xErr = MiniFloatConverter::reduceMantissaToNbitsRounding<12>(smatch.xErr);
186  smatch.yErr = MiniFloatConverter::reduceMantissaToNbitsRounding<12>(smatch.yErr);
187  smatch.dXdZErr = MiniFloatConverter::reduceMantissaToNbitsRounding<12>(smatch.dXdZErr);
188  smatch.dYdZErr = MiniFloatConverter::reduceMantissaToNbitsRounding<12>(smatch.dYdZErr);
190  if(smatch.dtSegmentRef.isNonnull()) dtSegmentsRefs.insert(smatch.dtSegmentRef);
191  if(smatch.cscSegmentRef.isNonnull()) cscSegmentsRefs.insert(smatch.cscSegmentRef);
192  }
193  }
194  }
195  }
196  }
197 
198  if( saveSegments_ ) {
199  std::map<DTRecSegment4DRef,size_t> dtMap;
200  std::vector<DTRecSegment4D> outDTSegmentsTmp;
201  std::map<CSCSegmentRef,size_t> cscMap;
202  std::vector<CSCSegment> outCSCSegmentsTmp;
203  for(auto & seg : dtSegmentsRefs) {
204  dtMap[seg]=outDTSegments->size();
205  outDTSegmentsTmp.push_back(*seg);
206  }
207  for(auto & seg : cscSegmentsRefs) {
208  cscMap[seg]=outCSCSegments->size();
209  outCSCSegmentsTmp.push_back(*seg);
210  }
211  outDTSegments->put(DTChamberId(),outDTSegmentsTmp.begin(),outDTSegmentsTmp.end());
212  outCSCSegments->put(CSCDetId(),outCSCSegmentsTmp.begin(),outCSCSegmentsTmp.end());
213  auto dtHandle = iEvent.put(std::move(outDTSegments));
214  auto cscHandle = iEvent.put(std::move(outCSCSegments));
215  for( auto & mu : *out) {
216  if(mu.isMatchesValid()) {
217  for (reco::MuonChamberMatch & cmatch : mu.matches()) {
218  for (reco::MuonSegmentMatch & smatch : cmatch.segmentMatches) {
219  if (dtMap.find(smatch.dtSegmentRef) != dtMap.end() )
220  smatch.dtSegmentRef=DTRecSegment4DRef(dtHandle,dtMap[smatch.dtSegmentRef]);
221  if (cscMap.find(smatch.cscSegmentRef) != cscMap.end() )
222  smatch.cscSegmentRef=CSCSegmentRef(cscHandle,cscMap[smatch.cscSegmentRef]);
223  }
224  }
225  }
226  }
227  }
228  iEvent.put(std::move(out));
229 }
230 
232 using namespace pat;
const edm::EDGetTokenT< pat::MuonCollection > src_
T getParameter(std::string const &) const
float ecal_timeError
Definition: MuonEnergy.h:38
void embedDytMuon()
embed reference to the above dyt Track
DTRecSegment4DRef dtSegmentRef
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:136
const StringCutObjectSelector< pat::Muon > dropDirectionalIso_
const StringCutObjectSelector< pat::Muon > slimKinkVars_
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:253
void embedTpfmsMuon()
embed reference to the above tpfms Track
Ptr< typename C::value_type > refToPtr(Ref< C, typename C::value_type, refhelper::FindUsingAdvance< C, typename C::value_type > > const &ref)
Definition: RefToPtr.h:18
PATMuonSlimmer(const edm::ParameterSet &iConfig)
CSCSegmentRef cscSegmentRef
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:519
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
const bool linkToPackedPF_
std::vector< edm::EDGetTokenT< reco::PFCandidateCollection > > pf_
bool isMatchesValid() const
Definition: Muon.h:142
const StringCutObjectSelector< pat::Muon > slimCaloVars_
ROOT::Math::PositionVector3D< ROOT::Math::Cartesian3D< float > > XYZPointF
point in space with cartesian internal representation
Definition: Point3D.h:10
void setCalEnergy(const MuonEnergy &calEnergy)
set energy deposition information
Definition: Muon.h:113
std::unique_ptr< pat::ObjectModifier< pat::Muon > > muonModifier_
float towerS9
total energy in 3x3 tower shape
Definition: MuonEnergy.h:12
float ecal_time
Calorimeter timing.
Definition: MuonEnergy.h:37
Definition: HeavyIon.h:7
const StringCutObjectSelector< pat::Muon > segmentsMuonSelection_
int iEvent
Definition: GenABIO.cc:230
float emS25
energy deposited in 5x5 ECAL crystal shape around central crystal
Definition: MuonEnergy.h:20
~PATMuonSlimmer() override
MuonQuality combinedQuality() const
get energy deposition information
Definition: Muon.h:121
void setPFIsolation(const std::string &label, const reco::MuonPFIsolation &deposit)
const int mu
Definition: Constants.h:22
bool isQualityValid() const
Definition: Muon.h:119
math::XYZPointF hcal_position
Definition: MuonEnergy.h:44
bool isEnergyValid() const
Definition: Muon.h:109
reco::MuonRef muonRef() const
Definition: PFCandidate.cc:459
MuonEnergy calEnergy() const
get energy deposition information
Definition: Muon.h:111
Slimmer of PAT Muons.
float emMax
maximal energy of ECAL crystal in the 5x5 shape
Definition: MuonEnergy.h:22
const StringCutObjectSelector< pat::Muon > dropPfP4_
float hadMax
maximal energy of HCAL tower in the 3x3 shape
Definition: MuonEnergy.h:30
void setCombinedQuality(const MuonQuality &combinedQuality)
set energy deposition information
Definition: Muon.h:123
std::vector< MuonChamberMatch > & matches()
get muon matching information
Definition: Muon.h:144
const StringCutObjectSelector< pat::Muon > slimMatches_
void beginLuminosityBlock(const edm::LuminosityBlock &, const edm::EventSetup &) final
math::XYZPoint tkKink_position
Kink position for the tracker stub and global track.
Definition: MuonQuality.h:32
std::vector< Muon > MuonCollection
Definition: Muon.h:34
void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:40
HLT enums.
void embedCaloMETMuonCorrs(const reco::MuonMETCorrectionData &t)
std::vector< edm::EDGetTokenT< edm::Association< pat::PackedCandidateCollection > > > pf2pc_
bool isPFMuon() const
Definition: Muon.h:276
virtual void setPFP4(const reco::Candidate::LorentzVector &p4_)
float hcal_timeError
Definition: MuonEnergy.h:40
const StringCutObjectSelector< pat::Muon > saveTeVMuons_
const StringCutObjectSelector< pat::Muon > slimCaloMETCorr_
void embedPickyMuon()
embed reference to the above picky Track
math::XYZPointF ecal_position
Trajectory position at the calorimeter.
Definition: MuonEnergy.h:43
math::XYZTLorentzVector LorentzVector
Lorentz vector.
Definition: Particle.h:21
Analysis-level muon class.
Definition: Muon.h:50
edm::Ptr< reco::Candidate > refToOrig_
Definition: PATObject.h:421
const bool saveSegments_
float hadS9
energy deposited in 3x3 HCAL tower shape around central tower
Definition: MuonEnergy.h:28
def move(src, dest)
Definition: eostools.py:510
reco::MuonMETCorrectionData caloMETMuonCorrs() const
muon MET corrections for caloMET; returns the muon correction struct if embedded during pat tuple pro...
Definition: Muon.h:104