CMS 3D CMS Logo

QGTagger.cc
Go to the documentation of this file.
1 #include <memory>
2 
9 
17 
22 
29  jetsToken( consumes<edm::View<reco::Jet>>( iConfig.getParameter<edm::InputTag>("srcJets"))),
30  jetCorrectorToken( consumes<reco::JetCorrector>( iConfig.getParameter<edm::InputTag>("jec"))),
31  vertexToken( consumes<reco::VertexCollection>( iConfig.getParameter<edm::InputTag>("srcVertexCollection"))),
32  rhoToken( consumes<double>( iConfig.getParameter<edm::InputTag>("srcRho"))),
33  jetsLabel( iConfig.getParameter<std::string>("jetsLabel")),
34  systLabel( iConfig.getParameter<std::string>("systematicsLabel")),
35  useQC( iConfig.getParameter<bool>("useQualityCuts")),
36  useJetCorr( !iConfig.getParameter<edm::InputTag>("jec").label().empty()),
37  produceSyst( systLabel != "")
38 {
39  produces<edm::ValueMap<float>>("qgLikelihood");
40  produces<edm::ValueMap<float>>("axis2");
41  produces<edm::ValueMap<int>>("mult");
42  produces<edm::ValueMap<float>>("ptD");
43  if(produceSyst){
44  produces<edm::ValueMap<float>>("qgLikelihoodSmearedQuark");
45  produces<edm::ValueMap<float>>("qgLikelihoodSmearedGluon");
46  produces<edm::ValueMap<float>>("qgLikelihoodSmearedAll");
47  }
49 }
50 
51 
54  std::vector<float>* qgProduct = new std::vector<float>;
55  std::vector<float>* axis2Product = new std::vector<float>;
56  std::vector<int>* multProduct = new std::vector<int>;
57  std::vector<float>* ptDProduct = new std::vector<float>;
58  std::vector<float>* smearedQuarkProduct = new std::vector<float>;
59  std::vector<float>* smearedGluonProduct = new std::vector<float>;
60  std::vector<float>* smearedAllProduct = new std::vector<float>;
61 
66 
68  QGLikelihoodRcd const & rcdhandle = iSetup.get<QGLikelihoodRcd>();
69  rcdhandle.get(jetsLabel, QGLParamsColl);
70 
72  if(produceSyst){
73  QGLikelihoodSystematicsRcd const & systrcdhandle = iSetup.get<QGLikelihoodSystematicsRcd>();
74  systrcdhandle.get(systLabel, QGLSystColl);
75  }
76 
77  bool weStillNeedToCheckJetCandidates = true;
78  bool weAreUsingPackedCandidates = false;
79  for(auto jet = jets->begin(); jet != jets->end(); ++jet){
80  if(weStillNeedToCheckJetCandidates) {
81  weAreUsingPackedCandidates = isPackedCandidate(&*jet);
82  weStillNeedToCheckJetCandidates = false;
83  }
84  float pt = (useJetCorr ? jet->pt()*jetCorr->correction(*jet) : jet->pt());
85 
86  float ptD, axis2; int mult;
87  std::tie(mult, ptD, axis2) = calcVariables(&*jet, vertexCollection, weAreUsingPackedCandidates);
88 
89  float qgValue;
90  if(mult > 2) qgValue = qgLikelihood->computeQGLikelihood(QGLParamsColl, pt, jet->eta(), *rho, {(float) mult, ptD, -std::log(axis2)});
91  else qgValue = -1;
92 
93  qgProduct->push_back(qgValue);
94  if(produceSyst){
95  smearedQuarkProduct->push_back(qgLikelihood->systematicSmearing(QGLSystColl, pt, jet->eta(), *rho, qgValue, 0));
96  smearedGluonProduct->push_back(qgLikelihood->systematicSmearing(QGLSystColl, pt, jet->eta(), *rho, qgValue, 1));
97  smearedAllProduct->push_back(qgLikelihood->systematicSmearing( QGLSystColl, pt, jet->eta(), *rho, qgValue, 2));
98  }
99  axis2Product->push_back(axis2);
100  multProduct->push_back(mult);
101  ptDProduct->push_back(ptD);
102  }
103 
104  putInEvent("qgLikelihood", jets, qgProduct, iEvent);
105  putInEvent("axis2", jets, axis2Product, iEvent);
106  putInEvent("mult", jets, multProduct, iEvent);
107  putInEvent("ptD", jets, ptDProduct, iEvent);
108  if(produceSyst){
109  putInEvent("qgLikelihoodSmearedQuark", jets, smearedQuarkProduct, iEvent);
110  putInEvent("qgLikelihoodSmearedGluon", jets, smearedGluonProduct, iEvent);
111  putInEvent("qgLikelihoodSmearedAll", jets, smearedAllProduct, iEvent);
112  }
113 }
114 
116 template <typename T> void QGTagger::putInEvent(const std::string& name, const edm::Handle<edm::View<reco::Jet>>& jets, std::vector<T>* product, edm::Event& iEvent) const {
117  auto out = std::make_unique<edm::ValueMap<T>>();
119  filler.insert(jets, product->begin(), product->end());
120  filler.fill();
121  iEvent.put(std::move(out), name);
122  delete product;
123 }
124 
125 
128  for(auto candidate : jet->getJetConstituentsQuick()){
129  if(typeid(pat::PackedCandidate)==typeid(*candidate)) return true;
130  else if(typeid(reco::PFCandidate)==typeid(*candidate)) return false;
131  else throw cms::Exception("WrongJetCollection", "Jet constituents are not particle flow candidates");
132  }
133  return false;
134 }
135 
136 
138 std::tuple<int, float, float> QGTagger::calcVariables(const reco::Jet *jet, edm::Handle<reco::VertexCollection>& vC, bool weAreUsingPackedCandidates) const {
139  float sum_weight = 0., sum_deta = 0., sum_dphi = 0., sum_deta2 = 0., sum_dphi2 = 0., sum_detadphi = 0., sum_pt = 0.;
140  int mult = 0;
141 
142  //Loop over the jet constituents
143  for(auto daughter : jet->getJetConstituentsQuick()){
144  if(weAreUsingPackedCandidates){ //packed candidate situation
145  auto part = static_cast<const pat::PackedCandidate*>(daughter);
146 
147  if(part->charge()){
148  if(!(part->fromPV() > 1 && part->trackHighPurity())) continue;
149  if(useQC){
150  if((part->dz()*part->dz())/(part->dzError()*part->dzError()) > 25.) continue;
151  if((part->dxy()*part->dxy())/(part->dxyError()*part->dxyError()) < 25.) ++mult;
152  } else ++mult;
153  } else {
154  if(part->pt() < 1.0) continue;
155  ++mult;
156  }
157  } else {
158  auto part = static_cast<const reco::PFCandidate*>(daughter);
159 
160  reco::TrackRef itrk = part->trackRef();
161  if(itrk.isNonnull()){ //Track exists --> charged particle
162  auto vtxLead = vC->begin();
163  auto vtxClose = vC->begin(); //Search for closest vertex to track
164  for(auto vtx = vC->begin(); vtx != vC->end(); ++vtx){
165  if(fabs(itrk->dz(vtx->position())) < fabs(itrk->dz(vtxClose->position()))) vtxClose = vtx;
166  }
167  if(!(vtxClose == vtxLead && itrk->quality(reco::TrackBase::qualityByName("highPurity")))) continue;
168 
169  if(useQC){ //If useQC, require dz and d0 cuts
170  float dz = itrk->dz(vtxClose->position());
171  float d0 = itrk->dxy(vtxClose->position());
172  float dz_sigma_square = pow(itrk->dzError(),2) + pow(vtxClose->zError(),2);
173  float d0_sigma_square = pow(itrk->d0Error(),2) + pow(vtxClose->xError(),2) + pow(vtxClose->yError(),2);
174  if(dz*dz/dz_sigma_square > 25.) continue;
175  if(d0*d0/d0_sigma_square < 25.) ++mult;
176  } else ++mult;
177  } else { //No track --> neutral particle
178  if(part->pt() < 1.0) continue; //Only use neutrals with pt > 1 GeV
179  ++mult;
180  }
181  }
182 
183  float deta = daughter->eta() - jet->eta();
184  float dphi = reco::deltaPhi(daughter->phi(), jet->phi());
185  float partPt = daughter->pt();
186  float weight = partPt*partPt;
187 
188  sum_weight += weight;
189  sum_pt += partPt;
190  sum_deta += deta*weight;
191  sum_dphi += dphi*weight;
192  sum_deta2 += deta*deta*weight;
193  sum_detadphi += deta*dphi*weight;
194  sum_dphi2 += dphi*dphi*weight;
195  }
196 
197  //Calculate axis2 and ptD
198  float a = 0., b = 0., c = 0.;
199  float ave_deta = 0., ave_dphi = 0., ave_deta2 = 0., ave_dphi2 = 0.;
200  if(sum_weight > 0){
201  ave_deta = sum_deta/sum_weight;
202  ave_dphi = sum_dphi/sum_weight;
203  ave_deta2 = sum_deta2/sum_weight;
204  ave_dphi2 = sum_dphi2/sum_weight;
205  a = ave_deta2 - ave_deta*ave_deta;
206  b = ave_dphi2 - ave_dphi*ave_dphi;
207  c = -(sum_detadphi/sum_weight - ave_deta*ave_dphi);
208  }
209  float delta = sqrt(fabs((a-b)*(a-b)+4*c*c));
210  float axis2 = (a+b-delta > 0 ? sqrt(0.5*(a+b-delta)) : 0);
211  float ptD = (sum_weight > 0 ? sqrt(sum_weight)/sum_pt : 0);
212  return std::make_tuple(mult, ptD, axis2);
213 }
214 
215 
219  desc.add<edm::InputTag>("srcJets");
220  desc.add<edm::InputTag>("srcRho");
221  desc.add<std::string>("jetsLabel");
222  desc.add<std::string>("systematicsLabel", "");
223  desc.add<bool>("useQualityCuts");
224  desc.add<edm::InputTag>("jec", edm::InputTag())->setComment("Jet correction service: only applied when non-empty");
225  desc.add<edm::InputTag>("srcVertexCollection")->setComment("Ignored for miniAOD, possible to keep empty");
226  descriptions.add("QGTagger", desc);
227 }
228 
229 
230 //define this as a plug-in
dbl * delta
Definition: mlp_gen.cc:36
constexpr double deltaPhi(double phi1, double phi2)
Definition: deltaPhi.h:22
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:125
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:251
double eta() const final
momentum pseudorapidity
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
Base class for all types of Jets.
Definition: Jet.h:20
void putInEvent(const std::string &, const edm::Handle< edm::View< reco::Jet >> &, std::vector< T > *, edm::Event &) const
Function to put product into event.
Definition: QGTagger.cc:116
edm::EDGetTokenT< reco::VertexCollection > vertexToken
Definition: QGTagger.h:30
const bool useJetCorr
Definition: QGTagger.h:33
std::vector< Vertex > VertexCollection
collection of Vertex objects
Definition: VertexFwd.h:9
Definition: weight.py:1
double correction(const LorentzVector &fJet) const
get correction using Jet information only
Definition: JetCorrector.h:49
bool isPackedCandidate(const reco::Jet *jet) const
Function to tell us if we are using packedCandidates, only test for first candidate.
Definition: QGTagger.cc:127
QGLikelihoodCalculator * qgLikelihood
Definition: QGTagger.h:34
float systematicSmearing(edm::ESHandle< QGLikelihoodSystematicsObject > &QGLParamsColl, float pt, float eta, float rho, float qgValue, int qgIndex) const
std::string systLabel
Definition: QGTagger.h:32
QGTagger(const edm::ParameterSet &)
Definition: QGTagger.cc:28
PRODUCT const & get(ESGetToken< PRODUCT, T > const &iToken) const
char const * label
int iEvent
Definition: GenABIO.cc:224
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
float computeQGLikelihood(edm::ESHandle< QGLikelihoodObject > &QGLParamsColl, float pt, float eta, float rho, std::vector< float > vars) const
Compute likelihood for a jet using the QGLikelihoodObject information and a set of variables...
Definition: Jet.py:1
const bool produceSyst
Definition: QGTagger.h:33
T sqrt(T t)
Definition: SSEVec.h:18
vector< PseudoJet > jets
void produce(edm::StreamID, edm::Event &, const edm::EventSetup &) const override
Produce qgLikelihood using {mult, ptD, -log(axis2)}.
Definition: QGTagger.cc:53
ParameterDescriptionBase * add(U const &iLabel, T const &value)
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
Descriptions method.
Definition: QGTagger.cc:217
std::tuple< int, float, float > calcVariables(const reco::Jet *, edm::Handle< reco::VertexCollection > &, bool) const
Calculation of axis2, mult and ptD.
Definition: QGTagger.cc:138
edm::EDGetTokenT< edm::View< reco::Jet > > jetsToken
Definition: QGTagger.h:28
static TrackQuality qualityByName(const std::string &name)
Definition: TrackBase.cc:134
part
Definition: HCALResponse.h:20
std::string jetsLabel
Definition: QGTagger.h:32
double b
Definition: hdecay.h:120
void add(std::string const &label, ParameterSetDescription const &psetDescription)
edm::EDGetTokenT< double > rhoToken
Definition: QGTagger.h:31
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:40
fixed size matrix
HLT enums.
double a
Definition: hdecay.h:121
T get() const
Definition: EventSetup.h:71
edm::EDGetTokenT< reco::JetCorrector > jetCorrectorToken
Definition: QGTagger.h:29
virtual std::vector< const reco::Candidate * > getJetConstituentsQuick() const
quick list of constituents
double phi() const final
momentum azimuthal angle
const bool useQC
Definition: QGTagger.h:33
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:40
def move(src, dest)
Definition: eostools.py:511