CMS 3D CMS Logo

L1GenTreeProducer.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: L1TriggerDPG/L1Ntuples
4 // Class: L1GenTreeProducer
5 //
13 //
14 // Original Author:
15 // Created:
16 // $Id: L1GenTreeProducer.cc,v 1.8 2012/08/29 12:44:03 jbrooke Exp $
17 //
18 //
19 
20 
21 // system include files
22 #include <memory>
23 
24 // framework
31 
32 // input data formats
35 
38 #include "HepMC/GenParticle.h"
39 #include "HepMC/GenVertex.h"
41 
43 
44 
45 // ROOT output stuff
48 #include "TTree.h"
49 
51 
52 //
53 // class declaration
54 //
55 
57 public:
58  explicit L1GenTreeProducer(const edm::ParameterSet&);
59  ~L1GenTreeProducer() override;
60 
61 
62 private:
63  void beginJob(void) override ;
64  void analyze(const edm::Event&, const edm::EventSetup&) override;
65  void endJob() override;
66 
67 private:
68 
69  unsigned maxL1Upgrade_;
70 
71  // output file
73 
74  // tree
75  TTree * tree_;
76 
77  // data format
78  std::unique_ptr<L1Analysis::L1AnalysisGeneratorDataFormat> l1GenData_;
79 
80  // EDM input tags
85 
86 };
87 
88 
89 
91 {
92 
93  genJetToken_ = consumes<reco::GenJetCollection>(iConfig.getUntrackedParameter<edm::InputTag>("genJetToken"));
94  genParticleToken_ = consumes<reco::GenParticleCollection>(iConfig.getUntrackedParameter<edm::InputTag>("genParticleToken"));
95  pileupInfoToken_ = consumes<std::vector<PileupSummaryInfo> >(iConfig.getUntrackedParameter<edm::InputTag>("pileupInfoToken"));
96  genInfoToken_ = consumes<GenEventInfoProduct>(iConfig.getParameter<edm::InputTag>("genInfoToken"));
97 
98  l1GenData_ = std::make_unique<L1Analysis::L1AnalysisGeneratorDataFormat>();
99 
100  // set up output
101  tree_=fs_->make<TTree>("L1GenTree", "L1GenTree");
102  tree_->Branch("Generator", "L1Analysis::L1AnalysisGeneratorDataFormat", l1GenData_.get(), 32000, 3);
103 }
104 
105 
107 {
108 
109  // do anything here that needs to be done at desctruction time
110  // (e.g. close files, deallocate resources etc.)
111 
112 }
113 
114 
115 //
116 // member functions
117 //
118 
119 // ------------ method called to for each event ------------
120 void
122 {
124  iEvent.getByToken(genInfoToken_, genInfo);
125 
126  l1GenData_->Reset();
127 
128  if (genInfo.isValid()){
129  l1GenData_->weight = genInfo->weight();
130  l1GenData_->pthat = genInfo->hasBinningValues() ? (genInfo->binningValues())[0] : 0.0;
131  }
132 
134  iEvent.getByToken(genJetToken_, genJets);
135 
136 
137  if (genJets.isValid()){
138 
139  reco::GenJetCollection::const_iterator jetItr = genJets->begin();
140  reco::GenJetCollection::const_iterator jetEnd = genJets->end();
141  for( ; jetItr != jetEnd ; ++jetItr) {
142  l1GenData_->jetPt.push_back( jetItr->pt() );
143  l1GenData_->jetEta.push_back( jetItr->eta() );
144  l1GenData_->jetPhi.push_back( jetItr->phi() );
145  l1GenData_->nJet++;
146  }
147 
148  } else {
149  edm::LogWarning("MissingProduct") << "Gen jets not found. Branch will not be filled" << std::endl;
150  }
151 
153  iEvent.getByToken(genParticleToken_, genParticles);
154 
155  if (genParticles.isValid()) {
156 
157  int nPart {0};
158 
159  for(size_t i = 0; i < genParticles->size(); ++ i) {
160  const reco::GenParticle & p = (*genParticles)[i];
161  int id = p.pdgId();
162 
163  // See if the parent was interesting
164  int parentID = -10000;
165  unsigned int nMo=p.numberOfMothers();
166  for(unsigned int i=0;i<nMo;++i){
167  int thisParentID = dynamic_cast
168  <const reco::GenParticle*>(p.mother(i))->pdgId();
169  //
170  // Is this a bottom hadron?
171  int hundredsIndex = abs(thisParentID)/100;
172  int thousandsIndex = abs(thisParentID)/1000;
173  if ( ((abs(thisParentID) >= 23) &&
174  (abs(thisParentID) <= 25)) ||
175  (abs(thisParentID) == 6) ||
176  (hundredsIndex == 5) ||
177  (hundredsIndex == 4) ||
178  (thousandsIndex == 5) ||
179  (thousandsIndex == 4)
180  )
181  parentID = thisParentID;
182  }
183  if ((parentID == -10000) && (nMo > 0))
184  parentID = dynamic_cast
185  <const reco::GenParticle*>(p.mother(0))->pdgId();
186  //
187  // If the parent of this particle is interesting, store all of the info
188  if ((parentID != p.pdgId()) &&
189  ((parentID > -9999)
190  || (abs(id) == 11)
191  || (abs(id) == 13)
192  || (abs(id) == 23)
193  || (abs(id) == 24)
194  || (abs(id) == 25)
195  || (abs(id) == 4)
196  || (abs(id) == 5)
197  || (abs(id) == 6))
198  )
199  {
200  l1GenData_->partId.push_back(p.pdgId());
201  l1GenData_->partStat.push_back(p.status());
202  l1GenData_->partPt.push_back(p.pt());
203  l1GenData_->partEta.push_back(p.eta());
204  l1GenData_->partPhi.push_back(p.phi());
205  l1GenData_->partE.push_back(p.energy());
206  l1GenData_->partParent.push_back(parentID);
207  l1GenData_->partCh.push_back(p.charge());
208  ++nPart;
209  }
210  }
211  l1GenData_->nPart = nPart;
212  }
213 
214 
216  iEvent.getByToken(pileupInfoToken_, puInfoCollection);
217 
218  if (!puInfoCollection.isValid()) {
219  throw cms::Exception("ProductNotValid") << "pileupInfoSource not valid";
220  }
221 
222  // Loop over vector, find in-time entry, then store the relevant info
223  std::vector<PileupSummaryInfo>::const_iterator puItr = puInfoCollection->begin();
224  std::vector<PileupSummaryInfo>::const_iterator puEnd = puInfoCollection->end();
225  for( ; puItr != puEnd; ++puItr) {
226  int bx = puItr->getBunchCrossing();
227  if (bx == 0) {
228  l1GenData_->nMeanPU = puItr->getTrueNumInteractions();
229  l1GenData_->nVtx = puItr->getPU_NumInteractions();
230  break;
231  }
232  }
233 
234 
235 
236  tree_->Fill();
237 
238 }
239 
240 // ------------ method called once each job just before starting event loop ------------
241 void
243 {
244 }
245 
246 // ------------ method called once each job just after ending the event loop ------------
247 void
249 }
250 
251 //define this as a plug-in
T getParameter(std::string const &) const
int pdgId() const final
PDG identifier.
T getUntrackedParameter(std::string const &, T const &) const
double eta() const final
momentum pseudorapidity
const std::vector< double > & binningValues() const
bool hasBinningValues() const
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
edm::EDGetTokenT< std::vector< PileupSummaryInfo > > pileupInfoToken_
size_t numberOfMothers() const override
number of mothers
void analyze(const edm::Event &, const edm::EventSetup &) override
double pt() const final
transverse momentum
T * make(const Args &...args) const
make new ROOT object
Definition: TFileService.h:64
edm::EDGetTokenT< GenEventInfoProduct > genInfoToken_
int charge() const final
electric charge
Definition: LeafCandidate.h:91
double weight() const
~L1GenTreeProducer() override
edm::Service< TFileService > fs_
int iEvent
Definition: GenABIO.cc:224
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
void beginJob(void) override
edm::EDGetTokenT< reco::GenJetCollection > genJetToken_
edm::EDGetTokenT< reco::GenParticleCollection > genParticleToken_
double energy() const final
energy
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
bool isValid() const
Definition: HandleBase.h:74
TString nPart(Int_t part, TString string, TString delimit=";", Bool_t removerest=true)
std::unique_ptr< L1Analysis::L1AnalysisGeneratorDataFormat > l1GenData_
L1GenTreeProducer(const edm::ParameterSet &)
int status() const final
status word
void endJob() override
double phi() const final
momentum azimuthal angle
const Candidate * mother(size_type=0) const override
return mother at a given position, i = 0, ... numberOfMothers() - 1 (read only mode) ...