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 // system include files
21 #include <memory>
22 
23 // framework
30 
31 // input data formats
34 
37 #include "HepMC/GenParticle.h"
38 #include "HepMC/GenVertex.h"
40 
42 
43 // ROOT output stuff
46 #include "TTree.h"
47 
49 
50 //
51 // class declaration
52 //
53 
55 public:
56  explicit L1GenTreeProducer(const edm::ParameterSet&);
57  ~L1GenTreeProducer() override;
58 
59 private:
60  void beginJob(void) override;
61  void analyze(const edm::Event&, const edm::EventSetup&) override;
62  void endJob() override;
63 
64 private:
65  unsigned maxL1Upgrade_;
66 
67  // output file
69 
70  // tree
71  TTree* tree_;
72 
73  // data format
74  std::unique_ptr<L1Analysis::L1AnalysisGeneratorDataFormat> l1GenData_;
75 
76  // EDM input tags
81 };
82 
84  genJetToken_ = consumes<reco::GenJetCollection>(iConfig.getUntrackedParameter<edm::InputTag>("genJetToken"));
86  consumes<reco::GenParticleCollection>(iConfig.getUntrackedParameter<edm::InputTag>("genParticleToken"));
88  consumes<std::vector<PileupSummaryInfo>>(iConfig.getUntrackedParameter<edm::InputTag>("pileupInfoToken"));
89  genInfoToken_ = consumes<GenEventInfoProduct>(iConfig.getParameter<edm::InputTag>("genInfoToken"));
90 
91  l1GenData_ = std::make_unique<L1Analysis::L1AnalysisGeneratorDataFormat>();
92 
93  // set up output
94  tree_ = fs_->make<TTree>("L1GenTree", "L1GenTree");
95  tree_->Branch("Generator", "L1Analysis::L1AnalysisGeneratorDataFormat", l1GenData_.get(), 32000, 3);
96 }
97 
99  // do anything here that needs to be done at desctruction time
100  // (e.g. close files, deallocate resources etc.)
101 }
102 
103 //
104 // member functions
105 //
106 
107 // ------------ method called to for each event ------------
110  iEvent.getByToken(genInfoToken_, genInfo);
111 
112  l1GenData_->Reset();
113 
114  if (genInfo.isValid()) {
115  l1GenData_->weight = genInfo->weight();
116  l1GenData_->pthat = genInfo->hasBinningValues() ? (genInfo->binningValues())[0] : 0.0;
117  }
118 
120  iEvent.getByToken(genJetToken_, genJets);
121 
122  if (genJets.isValid()) {
123  reco::GenJetCollection::const_iterator jetItr = genJets->begin();
124  reco::GenJetCollection::const_iterator jetEnd = genJets->end();
125  for (; jetItr != jetEnd; ++jetItr) {
126  l1GenData_->jetPt.push_back(jetItr->pt());
127  l1GenData_->jetEta.push_back(jetItr->eta());
128  l1GenData_->jetPhi.push_back(jetItr->phi());
129  l1GenData_->nJet++;
130  }
131 
132  } else {
133  edm::LogWarning("MissingProduct") << "Gen jets not found. Branch will not be filled" << std::endl;
134  }
135 
138 
139  if (genParticles.isValid()) {
140  int nPart{0};
141 
142  for (size_t i = 0; i < genParticles->size(); ++i) {
143  const reco::GenParticle& p = (*genParticles)[i];
144  int id = p.pdgId();
145 
146  // See if the parent was interesting
147  int parentID = -10000;
148  unsigned int nMo = p.numberOfMothers();
149  for (unsigned int i = 0; i < nMo; ++i) {
150  int thisParentID = dynamic_cast<const reco::GenParticle*>(p.mother(i))->pdgId();
151  //
152  // Is this a bottom hadron?
153  int hundredsIndex = abs(thisParentID) / 100;
154  int thousandsIndex = abs(thisParentID) / 1000;
155  if (((abs(thisParentID) >= 23) && (abs(thisParentID) <= 25)) || (abs(thisParentID) == 6) ||
156  (hundredsIndex == 5) || (hundredsIndex == 4) || (thousandsIndex == 5) || (thousandsIndex == 4))
157  parentID = thisParentID;
158  }
159  if ((parentID == -10000) && (nMo > 0))
160  parentID = dynamic_cast<const reco::GenParticle*>(p.mother(0))->pdgId();
161  //
162  // If the parent of this particle is interesting, store all of the info
163  if ((parentID != p.pdgId()) &&
164  ((parentID > -9999) || (abs(id) == 11) || (abs(id) == 13) || (abs(id) == 23) || (abs(id) == 24) ||
165  (abs(id) == 25) || (abs(id) == 4) || (abs(id) == 5) || (abs(id) == 6))) {
166  l1GenData_->partId.push_back(p.pdgId());
167  l1GenData_->partStat.push_back(p.status());
168  l1GenData_->partPt.push_back(p.pt());
169  l1GenData_->partEta.push_back(p.eta());
170  l1GenData_->partPhi.push_back(p.phi());
171  l1GenData_->partE.push_back(p.energy());
172  l1GenData_->partParent.push_back(parentID);
173  l1GenData_->partCh.push_back(p.charge());
174  ++nPart;
175  }
176  }
177  l1GenData_->nPart = nPart;
178  }
179 
181  iEvent.getByToken(pileupInfoToken_, puInfoCollection);
182 
183  if (!puInfoCollection.isValid()) {
184  throw cms::Exception("ProductNotValid") << "pileupInfoSource not valid";
185  }
186 
187  // Loop over vector, find in-time entry, then store the relevant info
188  std::vector<PileupSummaryInfo>::const_iterator puItr = puInfoCollection->begin();
189  std::vector<PileupSummaryInfo>::const_iterator puEnd = puInfoCollection->end();
190  for (; puItr != puEnd; ++puItr) {
191  int bx = puItr->getBunchCrossing();
192  if (bx == 0) {
193  l1GenData_->nMeanPU = puItr->getTrueNumInteractions();
194  l1GenData_->nVtx = puItr->getPU_NumInteractions();
195  break;
196  }
197  }
198 
199  tree_->Fill();
200 }
201 
202 // ------------ method called once each job just before starting event loop ------------
204 
205 // ------------ method called once each job just after ending the event loop ------------
207 
208 //define this as a plug-in
GenEventInfoProduct::hasBinningValues
bool hasBinningValues() const
Definition: GenEventInfoProduct.h:49
L1GenTreeProducer::maxL1Upgrade_
unsigned maxL1Upgrade_
Definition: L1GenTreeProducer.cc:65
GenJetCollection.h
PileupSummaryInfo.h
mps_fire.i
i
Definition: mps_fire.py:355
MessageLogger.h
genParticles2HepMC_cfi.genParticles
genParticles
Definition: genParticles2HepMC_cfi.py:4
reco::GenParticle
Definition: GenParticle.h:21
edm::EDGetTokenT< reco::GenJetCollection >
AlCaHLTBitMon_ParallelJobs.p
p
Definition: AlCaHLTBitMon_ParallelJobs.py:153
GenEventInfoProduct::binningValues
const std::vector< double > & binningValues() const
Definition: GenEventInfoProduct.h:48
L1GenTreeProducer::~L1GenTreeProducer
~L1GenTreeProducer() override
Definition: L1GenTreeProducer.cc:98
L1GenTreeProducer
Definition: L1GenTreeProducer.cc:54
edm::ParameterSet::getUntrackedParameter
T getUntrackedParameter(std::string const &, T const &) const
EDAnalyzer.h
L1GenTreeProducer::analyze
void analyze(const edm::Event &, const edm::EventSetup &) override
Definition: L1GenTreeProducer.cc:108
edm::Handle
Definition: AssociativeIterator.h:50
L1GenTreeProducer::genInfoToken_
edm::EDGetTokenT< GenEventInfoProduct > genInfoToken_
Definition: L1GenTreeProducer.cc:80
edm::EDAnalyzer
Definition: EDAnalyzer.h:29
GenParticle.h
simKBmtfDigis_cfi.bx
bx
Definition: simKBmtfDigis_cfi.py:55
MakerMacros.h
DEFINE_FWK_MODULE
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
Service.h
L1GenTreeProducer::genParticleToken_
edm::EDGetTokenT< reco::GenParticleCollection > genParticleToken_
Definition: L1GenTreeProducer.cc:78
L1GenTreeProducer::beginJob
void beginJob(void) override
Definition: L1GenTreeProducer.cc:203
nPart
TString nPart(Int_t part, TString string, TString delimit=";", Bool_t removerest=true)
edm::LogWarning
Definition: MessageLogger.h:141
TFileService.h
L1GenTreeProducer::fs_
edm::Service< TFileService > fs_
Definition: L1GenTreeProducer.cc:68
edm::ParameterSet
Definition: ParameterSet.h:36
GenEventInfoProduct.h
Event.h
L1GenTreeProducer::l1GenData_
std::unique_ptr< L1Analysis::L1AnalysisGeneratorDataFormat > l1GenData_
Definition: L1GenTreeProducer.cc:74
edm::Service< TFileService >
iEvent
int iEvent
Definition: GenABIO.cc:224
L1GenTreeProducer::genJetToken_
edm::EDGetTokenT< reco::GenJetCollection > genJetToken_
Definition: L1GenTreeProducer.cc:77
EgammaValidation_cff.pdgId
pdgId
Definition: EgammaValidation_cff.py:118
ttbarCategorization_cff.genJets
genJets
Definition: ttbarCategorization_cff.py:29
edm::EventSetup
Definition: EventSetup.h:57
edm::ParameterSet::getParameter
T getParameter(std::string const &) const
Frameworkfwd.h
L1AnalysisGeneratorDataFormat.h
Exception
Definition: hltDiff.cc:246
L1GenTreeProducer::tree_
TTree * tree_
Definition: L1GenTreeProducer.cc:71
funct::abs
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
L1GenTreeProducer::L1GenTreeProducer
L1GenTreeProducer(const edm::ParameterSet &)
Definition: L1GenTreeProducer.cc:83
ParameterSet.h
HepMCProduct.h
edm::HandleBase::isValid
bool isValid() const
Definition: HandleBase.h:70
edm::Event
Definition: Event.h:73
L1GenTreeProducer::endJob
void endJob() override
Definition: L1GenTreeProducer.cc:206
edm::InputTag
Definition: InputTag.h:15
TFileService::make
T * make(const Args &... args) const
make new ROOT object
Definition: TFileService.h:64
GenEventInfoProduct::weight
double weight() const
Definition: GenEventInfoProduct.h:35
L1GenTreeProducer::pileupInfoToken_
edm::EDGetTokenT< std::vector< PileupSummaryInfo > > pileupInfoToken_
Definition: L1GenTreeProducer.cc:79