CMS 3D CMS Logo

HTXSRivetProducer.cc
Go to the documentation of this file.
1 /* \class HTXSRivetProducer
2  *
3  * \author David Sperka, University of Florida
4  *
5  * $Id: HTXSRivetProducer.cc,v 1.1 2016/09/27 13:07:29 dsperka Exp $
6  *
7  */
8 
14 
17 
18 #include "Rivet/Particle.hh"
19 #include "Rivet/AnalysisHandler.hh"
22 
23 #include <memory>
24 
25 #include <vector>
26 #include <cstdio>
27 #include <cstring>
28 
29 using namespace Rivet;
30 using namespace edm;
31 using namespace std;
32 
33 class HTXSRivetProducer : public edm::one::EDProducer<edm::one::WatchRuns, edm::one::SharedResources> {
34 public:
36  : _hepmcCollection(consumes<HepMC3Product>(cfg.getParameter<edm::InputTag>("HepMCCollection"))),
37  _lheRunInfo(consumes<LHERunInfoProduct, edm::InRun>(cfg.getParameter<edm::InputTag>("LHERunInfo"))) {
38  usesResource("Rivet");
39  _prodMode = cfg.getParameter<string>("ProductionMode");
40  m_HiggsProdMode = HTXS::UNKNOWN;
41  _HTXS = nullptr;
42  _analysisHandler = nullptr;
43  produces<HTXS::HiggsClassification>("HiggsClassification").setBranchAlias("HiggsClassification");
44  }
45 
46 private:
47  void produce(edm::Event&, const edm::EventSetup&) override;
48 
49  void beginRun(edm::Run const& iRun, edm::EventSetup const& es) override;
50  void endRun(edm::Run const& iRun, edm::EventSetup const& es) override;
51 
54 
55  std::unique_ptr<Rivet::AnalysisHandler> _analysisHandler;
57 
60 
62 };
63 
65  //get the hepmc product from the event
67 
68  bool product_exists = iEvent.getByToken(_hepmcCollection, evt);
69  if (product_exists) {
70  // get HepMC GenEvent
71  const HepMC3::GenEventData* genEventData = evt->GetEvent();
72  std::unique_ptr<HepMC3::GenEvent> myGenEvent = std::make_unique<HepMC3::GenEvent>();
73  myGenEvent->read_data(*genEventData);
74 
75  if (_prodMode == "AUTO") {
76  // for these prod modes, don't change what is set in BeginRun
77  if (m_HiggsProdMode != HTXS::GGF && m_HiggsProdMode != HTXS::VBF && m_HiggsProdMode != HTXS::GG2ZH) {
78  unsigned nWs = 0;
79  unsigned nZs = 0;
80  unsigned nTs = 0;
81  unsigned nBs = 0;
82  unsigned nHs = 0;
83 
84  ConstGenVertexPtr HSvtx = myGenEvent->vertices()[0];
85 
86  if (HSvtx) {
87  for (auto ptcl : HepMCUtils::particles(HSvtx, Relatives::CHILDREN)) {
88  if (std::abs(ptcl->pdg_id()) == 24)
89  ++nWs;
90  if (ptcl->pdg_id() == 23)
91  ++nZs;
92  if (abs(ptcl->pdg_id()) == 6)
93  ++nTs;
94  if (abs(ptcl->pdg_id()) == 5)
95  ++nBs;
96  if (ptcl->pdg_id() == 25)
97  ++nHs;
98  }
99  }
100 
101  if (nZs == 1 && nHs == 1 && (nWs + nTs) == 0) {
102  m_HiggsProdMode = HTXS::QQ2ZH;
103  } else if (nWs == 1 && nHs == 1 && (nZs + nTs) == 0) {
104  m_HiggsProdMode = HTXS::WH;
105  } else if (nTs == 2 && nHs == 1 && nZs == 0) {
106  m_HiggsProdMode = HTXS::TTH;
107  } else if (nTs == 1 && nHs == 1 && nZs == 0) {
108  m_HiggsProdMode = HTXS::TH;
109  } else if (nBs == 2 && nHs == 1 && nZs == 0) {
110  m_HiggsProdMode = HTXS::BBH;
111  }
112  }
113  }
114 
115  if (!_HTXS || !_HTXS->hasProjection("FS")) {
116  _analysisHandler = std::make_unique<Rivet::AnalysisHandler>();
117  _HTXS = new Rivet::HiggsTemplateCrossSections();
118  _analysisHandler->addAnalysis(_HTXS);
119 
120  // set the production mode if not done already
121  if (_prodMode == "GGF")
122  m_HiggsProdMode = HTXS::GGF;
123  else if (_prodMode == "VBF")
124  m_HiggsProdMode = HTXS::VBF;
125  else if (_prodMode == "WH")
126  m_HiggsProdMode = HTXS::WH;
127  else if (_prodMode == "ZH")
128  m_HiggsProdMode = HTXS::QQ2ZH;
129  else if (_prodMode == "QQ2ZH")
130  m_HiggsProdMode = HTXS::QQ2ZH;
131  else if (_prodMode == "GG2ZH")
132  m_HiggsProdMode = HTXS::GG2ZH;
133  else if (_prodMode == "TTH")
134  m_HiggsProdMode = HTXS::TTH;
135  else if (_prodMode == "BBH")
136  m_HiggsProdMode = HTXS::BBH;
137  else if (_prodMode == "TH")
138  m_HiggsProdMode = HTXS::TH;
139  else if (_prodMode == "AUTO") {
140  edm::LogInfo("HTXSRivetProducer")
141  << "Using AUTO for HiggsProdMode, found it to be: " << m_HiggsProdMode << "\n";
142  edm::LogInfo("HTXSRivetProducer")
143  << "(UNKNOWN=0, GGF=1, VBF=2, WH=3, QQ2ZH=4, GG2ZH=5, TTH=6, BBH=7, TH=8)" << endl;
144  } else {
145  throw cms::Exception("HTXSRivetProducer")
146  << "ProductionMode must be one of: GGF,VBF,WH,ZH,QQ2ZH,GG2ZH,TTH,BBH,TH,AUTO ";
147  }
148  _HTXS->setHiggsProdMode(m_HiggsProdMode);
149 
150  // at this point the production mode must be known
151  if (m_HiggsProdMode == HTXS::UNKNOWN) {
152  edm::LogInfo("HTXSRivetProducer") << "HTXSRivetProducer WARNING: HiggsProduction mode is UNKNOWN" << endl;
153  }
154 
155  // initialize rivet analysis
156  _analysisHandler->init(*myGenEvent);
157  }
158 
159  // Create the Rivet event wrapper
160  const Rivet::Event event(const_cast<GenEvent&>(*myGenEvent));
161  // classify the event
162  Rivet::HiggsClassification rivet_cat = _HTXS->classifyEvent(event, m_HiggsProdMode);
163  cat_ = HTXS::Rivet2Root(rivet_cat);
164 
165  unique_ptr<HTXS::HiggsClassification> cat(new HTXS::HiggsClassification(cat_));
166 
167  iEvent.put(std::move(cat), "HiggsClassification");
168  }
169 }
170 
171 void HTXSRivetProducer::endRun(edm::Run const& iRun, edm::EventSetup const& es) {
172  if (_HTXS)
173  _HTXS->printClassificationSummary();
174 }
175 
177  if (_prodMode == "AUTO") {
179  bool product_exists = iRun.getByLabel(edm::InputTag("externalLHEProducer"), run);
180  if (product_exists) {
181  typedef std::vector<LHERunInfoProduct::Header>::const_iterator headers_const_iterator;
182  LHERunInfoProduct myLHERunInfoProduct = *(run.product());
183  for (headers_const_iterator iter = myLHERunInfoProduct.headers_begin(); iter != myLHERunInfoProduct.headers_end();
184  iter++) {
185  std::vector<std::string> lines = iter->lines();
186  for (unsigned int iLine = 0; iLine < lines.size(); iLine++) {
187  std::string line = lines.at(iLine);
188  // POWHEG
189  if (line.find("gg_H_quark-mass-effects") != std::string::npos) {
190  edm::LogInfo("HTXSRivetProducer") << iLine << " " << line << std::endl;
191  m_HiggsProdMode = HTXS::GGF;
192  break;
193  }
194  if (line.find("Process: HJ") != std::string::npos) {
195  edm::LogInfo("HTXSRivetProducer") << iLine << " " << line << std::endl;
196  m_HiggsProdMode = HTXS::GGF;
197  break;
198  }
199  if (line.find("Process: HJJ") != std::string::npos) {
200  edm::LogInfo("HTXSRivetProducer") << iLine << " " << line << std::endl;
201  m_HiggsProdMode = HTXS::GGF;
202  break;
203  }
204  if (line.find("VBF_H") != std::string::npos) {
205  edm::LogInfo("HTXSRivetProducer") << iLine << " " << line << std::endl;
206  m_HiggsProdMode = HTXS::VBF;
207  break;
208  }
209  if (line.find("HZJ") != std::string::npos) {
210  edm::LogInfo("HTXSRivetProducer") << iLine << " " << line << std::endl;
211  m_HiggsProdMode = HTXS::QQ2ZH;
212  break;
213  }
214  if (line.find("ggHZ") != std::string::npos) {
215  edm::LogInfo("HTXSRivetProducer") << iLine << " " << line << std::endl;
216  m_HiggsProdMode = HTXS::GG2ZH;
217  break;
218  }
219  // MC@NLO
220  if (line.find("ggh012j") != std::string::npos) {
221  edm::LogInfo("HTXSRivetProducer") << iLine << " " << line << std::endl;
222  m_HiggsProdMode = HTXS::GGF;
223  break;
224  }
225  if (line.find("vbfh") != std::string::npos) {
226  edm::LogInfo("HTXSRivetProducer") << iLine << " " << line << std::endl;
227  m_HiggsProdMode = HTXS::VBF;
228  break;
229  }
230  if (line.find("zh012j") != std::string::npos) {
231  edm::LogInfo("HTXSRivetProducer") << iLine << " " << line << std::endl;
232  m_HiggsProdMode = HTXS::QQ2ZH;
233  break;
234  }
235  if (line.find("ggzh01j") != std::string::npos) {
236  edm::LogInfo("HTXSRivetProducer") << iLine << " " << line << std::endl;
237  m_HiggsProdMode = HTXS::GG2ZH;
238  break;
239  }
240  }
241 
242  if (m_HiggsProdMode != HTXS::UNKNOWN)
243  break;
244  }
245  }
246  }
247 }
248 
250 
headers_const_iterator headers_begin() const
HiggsProdMode
Higgs production modes, corresponding to input sample.
HTXS::HiggsProdMode m_HiggsProdMode
std::unique_ptr< Rivet::AnalysisHandler > _analysisHandler
Rivet::HiggsTemplateCrossSections * _HTXS
headers_const_iterator headers_end() const
int iEvent
Definition: GenABIO.cc:224
def cat(path)
Definition: eostools.py:401
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
void produce(edm::Event &, const edm::EventSetup &) override
bool getByLabel(std::string const &label, Handle< PROD > &result) const
Definition: Run.h:278
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
edm::EDGetTokenT< edm::HepMC3Product > _hepmcCollection
Log< level::Info, false > LogInfo
void beginRun(edm::Run const &iRun, edm::EventSetup const &es) override
Rivet routine for classifying MC events according to the Higgs template cross section categories...
HLT enums.
HTXS::HiggsClassification Rivet2Root(category const &htxs_cat_rivet)
HTXS::HiggsClassification cat_
void endRun(edm::Run const &iRun, edm::EventSetup const &es) override
HTXSRivetProducer(const edm::ParameterSet &cfg)
def move(src, dest)
Definition: eostools.py:511
const HepMC3::GenEventData * GetEvent() const
Definition: HepMC3Product.h:36
edm::EDGetTokenT< LHERunInfoProduct > _lheRunInfo
Definition: event.py:1
Definition: Run.h:45