CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
VirtualJetProducer.cc
Go to the documentation of this file.
1 //
3 // VirtualJetProducer
4 // ------------------
5 //
6 // 04/21/2009 Philipp Schieferdecker <philipp.schieferdecker@cern.ch>
8 
12 
21 
36 
37 #include "fastjet/SISConePlugin.hh"
38 #include "fastjet/CMSIterativeConePlugin.hh"
39 #include "fastjet/ATLASConePlugin.hh"
40 #include "fastjet/CDFMidPointPlugin.hh"
41 
42 #include <iostream>
43 #include <memory>
44 #include <algorithm>
45 #include <limits>
46 #include <cmath>
47 #include <vdt/vdtMath.h>
48 
49 using namespace std;
50 
51 
52 namespace reco {
53  namespace helper {
55  bool operator()( const fastjet::PseudoJet & t1, const fastjet::PseudoJet & t2 ) const {
56  return t1.perp() > t2.perp();
57  }
58  };
59 
60  }
61 }
62 
63 //______________________________________________________________________________
64 const char *VirtualJetProducer::JetType::names[] = {
65  "BasicJet","GenJet","CaloJet","PFJet","TrackJet","PFClusterJet"
66 };
67 
68 
69 //______________________________________________________________________________
72 {
73  const char **pos = std::find(names, names + LastJetType, name);
74  if (pos == names + LastJetType) {
75  std::string errorMessage="Requested jetType not supported: "+name+"\n";
76  throw cms::Exception("Configuration",errorMessage);
77  }
78  return (Type)(pos-names);
79 }
80 
81 
83 {
84 
85 
86  if ( writeCompound_ ) {
87  produces<reco::BasicJetCollection>();
88  }
89 
90  if (makeCaloJet(jetTypeE)) {
91  produces<reco::CaloJetCollection>(tag).setBranchAlias(alias);
92  }
93  else if (makePFJet(jetTypeE)) {
94  produces<reco::PFJetCollection>(tag).setBranchAlias(alias);
95  }
96  else if (makeGenJet(jetTypeE)) {
97  produces<reco::GenJetCollection>(tag).setBranchAlias(alias);
98  }
99  else if (makeTrackJet(jetTypeE)) {
100  produces<reco::TrackJetCollection>(tag).setBranchAlias(alias);
101  }
102  else if (makePFClusterJet(jetTypeE)) {
103  produces<reco::PFClusterJetCollection>(tag).setBranchAlias(alias);
104  }
105  else if (makeBasicJet(jetTypeE)) {
106  produces<reco::BasicJetCollection>(tag).setBranchAlias(alias);
107  }
108 }
109 
111 // construction / destruction
113 
114 //______________________________________________________________________________
116  : moduleLabel_ (iConfig.getParameter<string> ("@module_label"))
117  , src_ (iConfig.getParameter<edm::InputTag>("src"))
118  , srcPVs_ (iConfig.getParameter<edm::InputTag>("srcPVs"))
119  , jetType_ (iConfig.getParameter<string> ("jetType"))
120  , jetAlgorithm_ (iConfig.getParameter<string> ("jetAlgorithm"))
121  , rParam_ (iConfig.getParameter<double> ("rParam"))
122  , inputEtMin_ (iConfig.getParameter<double> ("inputEtMin"))
123  , inputEMin_ (iConfig.getParameter<double> ("inputEMin"))
124  , jetPtMin_ (iConfig.getParameter<double> ("jetPtMin"))
125  , doPVCorrection_(iConfig.getParameter<bool> ("doPVCorrection"))
126  , restrictInputs_(false)
127  , maxInputs_(99999999)
128  , doAreaFastjet_ (iConfig.getParameter<bool> ("doAreaFastjet"))
129  , useExplicitGhosts_(false)
130  , doAreaDiskApprox_ (false)
131  , doRhoFastjet_ (iConfig.getParameter<bool> ("doRhoFastjet"))
132  , voronoiRfact_ (-9)
133  , doPUOffsetCorr_(iConfig.getParameter<bool> ("doPUOffsetCorr"))
134  , puWidth_(0)
135  , nExclude_(0)
136  , jetCollInstanceName_ ("")
137  , writeCompound_ ( false )
138  , verbosity_(0)
139 {
140  anomalousTowerDef_ = std::auto_ptr<AnomalousTower>(new AnomalousTower(iConfig));
141 
142  //
143  // additional parameters to think about:
144  // - overlap threshold (set to 0.75 for the time being)
145  // - p parameter for generalized kT (set to -2 for the time being)
146  // - fastjet PU subtraction parameters (not yet considered)
147  //
148  if (jetAlgorithm_=="SISCone") {
149  fjPlugin_ = PluginPtr( new fastjet::SISConePlugin(rParam_,0.75,0,0.0,false,
150  fastjet::SISConePlugin::SM_pttilde) );
151  fjJetDefinition_= JetDefPtr( new fastjet::JetDefinition(&*fjPlugin_) );
152  }
153  else if (jetAlgorithm_=="IterativeCone") {
154  fjPlugin_ = PluginPtr(new fastjet::CMSIterativeConePlugin(rParam_,1.0));
155  fjJetDefinition_= JetDefPtr(new fastjet::JetDefinition(&*fjPlugin_));
156  }
157  else if (jetAlgorithm_=="CDFMidPoint") {
158  fjPlugin_ = PluginPtr(new fastjet::CDFMidPointPlugin(rParam_,0.75));
159  fjJetDefinition_= JetDefPtr(new fastjet::JetDefinition(&*fjPlugin_));
160  }
161  else if (jetAlgorithm_=="ATLASCone") {
162  fjPlugin_ = PluginPtr(new fastjet::ATLASConePlugin(rParam_));
163  fjJetDefinition_= JetDefPtr(new fastjet::JetDefinition(&*fjPlugin_));
164  }
165  else if (jetAlgorithm_=="Kt")
166  fjJetDefinition_= JetDefPtr(new fastjet::JetDefinition(fastjet::kt_algorithm,rParam_));
167  else if (jetAlgorithm_=="CambridgeAachen")
168  fjJetDefinition_= JetDefPtr(new fastjet::JetDefinition(fastjet::cambridge_algorithm,
169  rParam_) );
170  else if (jetAlgorithm_=="AntiKt")
171  fjJetDefinition_= JetDefPtr( new fastjet::JetDefinition(fastjet::antikt_algorithm,rParam_) );
172  else if (jetAlgorithm_=="GeneralizedKt")
173  fjJetDefinition_= JetDefPtr( new fastjet::JetDefinition(fastjet::genkt_algorithm,
174  rParam_,-2) );
175  else
176  throw cms::Exception("Invalid jetAlgorithm")
177  <<"Jet algorithm for VirtualJetProducer is invalid, Abort!\n";
178 
180 
181  if ( iConfig.exists("jetCollInstanceName") ) {
182  jetCollInstanceName_ = iConfig.getParameter<string>("jetCollInstanceName");
183  }
184 
185  if ( doPUOffsetCorr_ ) {
186 
187  if(iConfig.exists("subtractorName")) puSubtractorName_ = iConfig.getParameter<string> ("subtractorName");
189 
190  if(puSubtractorName_.empty()){
191  edm::LogWarning("VirtualJetProducer") << "Pile Up correction on; however, pile up type is not specified. Using default... \n";
192  subtractor_ = boost::shared_ptr<PileUpSubtractor>(new PileUpSubtractor(iConfig, consumesCollector()));
193  }else{
194  subtractor_ = boost::shared_ptr<PileUpSubtractor>(PileUpSubtractorFactory::get()->create( puSubtractorName_, iConfig, consumesCollector()));
195  }
196  }
197 
198  // use explicit ghosts in the fastjet clustering sequence?
199  if ( iConfig.exists("useExplicitGhosts") ) {
200  useExplicitGhosts_ = iConfig.getParameter<bool>("useExplicitGhosts");
201  }
202 
203  // do approximate disk-based area calculation => warn if conflicting request
204  if (iConfig.exists("doAreaDiskApprox")) {
205  doAreaDiskApprox_ = iConfig.getParameter<bool>("doAreaDiskApprox");
207  throw cms::Exception("Conflicting area calculations") << "Both the calculation of jet area via fastjet and via an analytical disk approximation have been requested. Please decide on one.\n";
208 
209  }
210  // turn off jet collection output for speed
211  // Voronoi-based area calculation allows for an empirical scale factor
212  if (iConfig.exists("voronoiRfact"))
213  voronoiRfact_ = iConfig.getParameter<double>("voronoiRfact");
214 
215 
216  // do fasjet area / rho calcluation? => accept corresponding parameters
217  if ( doAreaFastjet_ || doRhoFastjet_ ) {
218  // Eta range of jets to be considered for Rho calculation
219  // Should be at most (jet acceptance - jet radius)
220  double rhoEtaMax=iConfig.getParameter<double>("Rho_EtaMax");
221  // default Ghost_EtaMax should be 5
222  double ghostEtaMax = iConfig.getParameter<double>("Ghost_EtaMax");
223  // default Active_Area_Repeats 1
224  int activeAreaRepeats = iConfig.getParameter<int> ("Active_Area_Repeats");
225  // default GhostArea 0.01
226  double ghostArea = iConfig.getParameter<double> ("GhostArea");
227  if (voronoiRfact_ <= 0) {
228  fjActiveArea_ = ActiveAreaSpecPtr(new fastjet::GhostedAreaSpec(ghostEtaMax,activeAreaRepeats,ghostArea));
229  fjActiveArea_->set_fj2_placement(true);
230  if ( ! useExplicitGhosts_ ) {
231  fjAreaDefinition_ = AreaDefinitionPtr( new fastjet::AreaDefinition(fastjet::active_area, *fjActiveArea_ ) );
232  } else {
233  fjAreaDefinition_ = AreaDefinitionPtr( new fastjet::AreaDefinition(fastjet::active_area_explicit_ghosts, *fjActiveArea_ ) );
234  }
235  }
236  fjRangeDef_ = RangeDefPtr( new fastjet::RangeDefinition(rhoEtaMax) );
237  }
238 
239  // restrict inputs to first "maxInputs" towers?
240  if ( iConfig.exists("restrictInputs") ) {
241  restrictInputs_ = iConfig.getParameter<bool>("restrictInputs");
242  maxInputs_ = iConfig.getParameter<unsigned int>("maxInputs");
243  }
244 
245 
246  string alias=iConfig.getUntrackedParameter<string>("alias",moduleLabel_);
247 
248 
249  // Check to see if we are writing compound jets for substructure
250  // and jet grooming
251  if ( iConfig.exists("writeCompound") ) {
252  writeCompound_ = iConfig.getParameter<bool>("writeCompound");
253  }
254 
255  // make the "produces" statements
257 
258  doFastJetNonUniform_ = false;
259  if(iConfig.exists("doFastJetNonUniform")) doFastJetNonUniform_ = iConfig.getParameter<bool> ("doFastJetNonUniform");
261  puCenters_ = iConfig.getParameter<std::vector<double> >("puCenters");
262  puWidth_ = iConfig.getParameter<double>("puWidth");
263  nExclude_ = iConfig.getParameter<unsigned int>("nExclude");
264  }
265 
266  useDeterministicSeed_ = false;
267  minSeed_ = 0;
268  if ( iConfig.exists("useDeterministicSeed") ) {
269  useDeterministicSeed_ = iConfig.getParameter<bool>("useDeterministicSeed");
270  minSeed_ = iConfig.getParameter<unsigned int>("minSeed");
271  }
272 
273  if ( iConfig.exists("verbosity" ) ) {
274  verbosity_ = iConfig.getParameter<int>("verbosity");
275  }
276 
277  produces<std::vector<double> >("rhos");
278  produces<std::vector<double> >("sigmas");
279  produces<double>("rho");
280  produces<double>("sigma");
281 
282  if (!srcPVs_.label().empty()) input_vertex_token_ = consumes<reco::VertexCollection>(srcPVs_);
283  input_candidateview_token_ = consumes<reco::CandidateView>(src_);
284  input_candidatefwdptr_token_ = consumes<std::vector<edm::FwdPtr<reco::PFCandidate> > >(src_);
285 
286 }
287 
288 //______________________________________________________________________________
290 {
291 }
292 
293 
295 // implementation of member functions
297 
298 //______________________________________________________________________________
300 {
301 
302  // If requested, set the fastjet random seed to a deterministic function
303  // of the run/lumi/event.
304  // NOTE!!! The fastjet random number sequence is a global singleton.
305  // Thus, we have to create an object and get access to the global singleton
306  // in order to change it.
307  if ( useDeterministicSeed_ ) {
308  fastjet::GhostedAreaSpec gas;
309  std::vector<int> seeds(2);
310  unsigned int runNum_uint = static_cast <unsigned int> (iEvent.id().run());
311  unsigned int evNum_uint = static_cast <unsigned int> (iEvent.id().event());
312  seeds[0] = std::max(runNum_uint,minSeed_ + 3) + 3 * evNum_uint;
313  seeds[1] = std::max(runNum_uint,minSeed_ + 5) + 5 * evNum_uint;
314  gas.set_random_status(seeds);
315  }
316 
317  LogDebug("VirtualJetProducer") << "Entered produce\n";
318  //determine signal vertex2
319  vertex_=reco::Jet::Point(0,0,0);
321  LogDebug("VirtualJetProducer") << "Adding PV info\n";
323  iEvent.getByToken(input_vertex_token_ , pvCollection);
324  if (pvCollection->size()>0) vertex_=pvCollection->begin()->position();
325  }
326 
327  // For Pileup subtraction using offset correction:
328  // set up geometry map
329  if ( doPUOffsetCorr_ ) {
330  subtractor_->setupGeometryMap(iEvent, iSetup);
331  }
332 
333  // clear data
334  LogDebug("VirtualJetProducer") << "Clear data\n";
335  fjInputs_.clear();
336  fjJets_.clear();
337  inputs_.clear();
338 
339  // get inputs and convert them to the fastjet format (fastjet::PeudoJet)
341 
343 
344  bool isView = iEvent.getByToken(input_candidateview_token_, inputsHandle);
345  if ( isView ) {
346  for (size_t i = 0; i < inputsHandle->size(); ++i) {
347  inputs_.push_back(inputsHandle->ptrAt(i));
348  }
349  } else {
350  iEvent.getByToken(input_candidatefwdptr_token_, pfinputsHandleAsFwdPtr);
351  for (size_t i = 0; i < pfinputsHandleAsFwdPtr->size(); ++i) {
352  if ( (*pfinputsHandleAsFwdPtr)[i].ptr().isAvailable() ) {
353  inputs_.push_back( (*pfinputsHandleAsFwdPtr)[i].ptr() );
354  }
355  else if ( (*pfinputsHandleAsFwdPtr)[i].backPtr().isAvailable() ) {
356  inputs_.push_back( (*pfinputsHandleAsFwdPtr)[i].backPtr() );
357  }
358  }
359  }
360  LogDebug("VirtualJetProducer") << "Got inputs\n";
361 
362  // Convert candidates to fastjet::PseudoJets.
363  // Also correct to Primary Vertex. Will modify fjInputs_
364  // and use inputs_
365  fjInputs_.reserve(inputs_.size());
366  inputTowers();
367  LogDebug("VirtualJetProducer") << "Inputted towers\n";
368 
369  // For Pileup subtraction using offset correction:
370  // Subtract pedestal.
371  if ( doPUOffsetCorr_ ) {
372  subtractor_->setDefinition(fjJetDefinition_);
374  subtractor_->calculatePedestal(fjInputs_);
375  subtractor_->subtractPedestal(fjInputs_);
376  LogDebug("VirtualJetProducer") << "Subtracted pedestal\n";
377  }
378  // Run algorithm. Will modify fjJets_ and allocate fjClusterSeq_.
379  // This will use fjInputs_
380  runAlgorithm( iEvent, iSetup );
381 
382  // if ( doPUOffsetCorr_ ) {
383  // subtractor_->setAlgorithm(fjClusterSeq_);
384  // }
385 
386  LogDebug("VirtualJetProducer") << "Ran algorithm\n";
387  // For Pileup subtraction using offset correction:
388  // Now we find jets and need to recalculate their energy,
389  // mark towers participated in jet,
390  // remove occupied towers from the list and recalculate mean and sigma
391  // put the initial towers collection to the jet,
392  // and subtract from initial towers in jet recalculated mean and sigma of towers
393  if ( doPUOffsetCorr_ ) {
394  LogDebug("VirtualJetProducer") << "Do PUOffsetCorr\n";
395  vector<fastjet::PseudoJet> orphanInput;
396  subtractor_->calculateOrphanInput(orphanInput);
397  subtractor_->calculatePedestal(orphanInput);
398  subtractor_->offsetCorrectJets();
399  }
400  // Write the output jets.
401  // This will (by default) call the member function template
402  // "writeJets", but can be overridden.
403  // this will use inputs_
404  output( iEvent, iSetup );
405  LogDebug("VirtualJetProducer") << "Wrote jets\n";
406 
407  return;
408 }
409 
410 //______________________________________________________________________________
411 
413 {
414  std::vector<edm::Ptr<reco::Candidate> >::const_iterator inBegin = inputs_.begin(),
415  inEnd = inputs_.end(), i = inBegin;
416  for (; i != inEnd; ++i ) {
418  // std::cout << "CaloTowerVI jets " << input->pt() << " " << input->et() << ' '<< input->energy() << ' ' << (isAnomalousTower(input) ? " bad" : " ok") << std::endl;
419  if (edm::isNotFinite(input->pt())) continue;
420  if (input->et() <inputEtMin_) continue;
421  if (input->energy()<inputEMin_) continue;
422  if (isAnomalousTower(input)) continue;
423  // Change by SRR : this is no longer an error nor warning, this can happen with PU mitigation algos.
424  // Also switch to something more numerically safe.
425  if (input->pt() < 100 * std::numeric_limits<double>::epsilon() ) {
426  continue;
427  }
429  const CaloTower* tower=dynamic_cast<const CaloTower*>(input.get());
431  fjInputs_.push_back(fastjet::PseudoJet(ct.px(),ct.py(),ct.pz(),ct.energy()));
432  //std::cout << "tower:" << *tower << '\n';
433  }
434  else {
435  /*
436  if(makePFJet(jetTypeE)) {
437  reco::PFCandidate* pfc = (reco::PFCandidate*)input.get();
438  std::cout << "PF cand:" << *pfc << '\n';
439  }
440  */
441  fjInputs_.push_back(fastjet::PseudoJet(input->px(),input->py(),input->pz(),
442  input->energy()));
443  }
444  fjInputs_.back().set_user_index(i - inBegin);
445  }
446 
447  if ( restrictInputs_ && fjInputs_.size() > maxInputs_ ) {
449  std::sort(fjInputs_.begin(), fjInputs_.end(), pTComparator);
450  fjInputs_.resize(maxInputs_);
451  edm::LogWarning("JetRecoTooManyEntries") << "Too many inputs in the event, limiting to first " << maxInputs_ << ". Output is suspect.";
452  }
453 }
454 
455 //______________________________________________________________________________
457 {
458  if (!makeCaloJet(jetTypeE))
459  return false;
460  else
461  return (*anomalousTowerDef_)(*input);
462 }
463 
464 //------------------------------------------------------------------------------
465 // This is pure virtual.
466 //______________________________________________________________________________
467 // void VirtualJetProducer::runAlgorithm( edm::Event & iEvent, edm::EventSetup const& iSetup,
468 // std::vector<edm::Ptr<reco::Candidate> > const & inputs_);
469 
470 //______________________________________________________________________________
471 void VirtualJetProducer::copyConstituents(const vector<fastjet::PseudoJet>& fjConstituents,
472  reco::Jet* jet)
473 {
474  for (unsigned int i=0;i<fjConstituents.size();++i) {
475  int index = fjConstituents[i].user_index();
476  if ( index >= 0 && static_cast<unsigned int>(index) < inputs_.size() )
477  jet->addDaughter(inputs_[index]);
478  }
479 }
480 
481 
482 //______________________________________________________________________________
483 vector<reco::CandidatePtr>
484 VirtualJetProducer::getConstituents(const vector<fastjet::PseudoJet>&fjConstituents)
485 {
486  vector<reco::CandidatePtr> result;
487  for (unsigned int i=0;i<fjConstituents.size();i++) {
488  int index = fjConstituents[i].user_index();
489  if ( index >= 0 && static_cast<unsigned int>(index) < inputs_.size() ) {
490  reco::CandidatePtr candidate = inputs_[index];
491  result.push_back(candidate);
492  }
493  }
494  return result;
495 }
496 
497 
498 //_____________________________________________________________________________
499 
501 {
502  // Write jets and constitutents. Will use fjJets_, inputs_
503  // and fjClusterSeq_
504 
505  if ( !writeCompound_ ) {
506  switch( jetTypeE ) {
507  case JetType::CaloJet :
508  writeJets<reco::CaloJet>( iEvent, iSetup);
509  break;
510  case JetType::PFJet :
511  writeJets<reco::PFJet>( iEvent, iSetup);
512  break;
513  case JetType::GenJet :
514  writeJets<reco::GenJet>( iEvent, iSetup);
515  break;
516  case JetType::TrackJet :
517  writeJets<reco::TrackJet>( iEvent, iSetup);
518  break;
519  case JetType::PFClusterJet :
520  writeJets<reco::PFClusterJet>( iEvent, iSetup);
521  break;
522  case JetType::BasicJet :
523  writeJets<reco::BasicJet>( iEvent, iSetup);
524  break;
525  default:
526  throw cms::Exception("InvalidInput") << "invalid jet type in VirtualJetProducer\n";
527  break;
528  };
529  } else {
530  // Write jets and constitutents.
531  switch( jetTypeE ) {
532  case JetType::CaloJet :
533  writeCompoundJets<reco::CaloJet>( iEvent, iSetup );
534  break;
535  case JetType::PFJet :
536  writeCompoundJets<reco::PFJet>( iEvent, iSetup );
537  break;
538  case JetType::GenJet :
539  writeCompoundJets<reco::GenJet>( iEvent, iSetup );
540  break;
541  case JetType::BasicJet :
542  writeCompoundJets<reco::BasicJet>( iEvent, iSetup );
543  break;
544  default:
545  throw cms::Exception("InvalidInput") << "invalid jet type in CompoundJetProducer\n";
546  break;
547  };
548  }
549 }
550 
551 namespace {
552 template< typename T >
553 struct Area { static float get(T const &) {return 0;}};
554 
555 template<>
556 struct Area<reco::CaloJet>{ static float get(reco::CaloJet const & jet) {
557  return jet.getSpecific().mTowersArea;
558 }
559 };
560 }
561 
562 template< typename T >
564 {
565  // std::cout << "writeJets " << typeid(T).name()
566  // << (doRhoFastjet_ ? " doRhoFastjet " : "")
567  // << (doAreaFastjet_ ? " doAreaFastjet " : "")
568  // << (doAreaDiskApprox_ ? " doAreaDiskApprox " : "")
569  // << std::endl;
570 
571  if (doRhoFastjet_) {
572  // declare jet collection without the two jets,
573  // for unbiased background estimation.
574  std::vector<fastjet::PseudoJet> fjexcluded_jets;
575  fjexcluded_jets=fjJets_;
576 
577  if(fjexcluded_jets.size()>2) fjexcluded_jets.resize(nExclude_);
578 
580  std::auto_ptr<std::vector<double> > rhos(new std::vector<double>);
581  std::auto_ptr<std::vector<double> > sigmas(new std::vector<double>);
582  int nEta = puCenters_.size();
583  rhos->reserve(nEta);
584  sigmas->reserve(nEta);
585  fastjet::ClusterSequenceAreaBase const* clusterSequenceWithArea =
586  dynamic_cast<fastjet::ClusterSequenceAreaBase const *> ( &*fjClusterSeq_ );
587 
588 
589  for(int ie = 0; ie < nEta; ++ie){
590  double eta = puCenters_[ie];
591  double etamin=eta-puWidth_;
592  double etamax=eta+puWidth_;
593  fastjet::RangeDefinition range_rho(etamin,etamax);
594  fastjet::BackgroundEstimator bkgestim(*clusterSequenceWithArea,range_rho);
595  bkgestim.set_excluded_jets(fjexcluded_jets);
596  rhos->push_back(bkgestim.rho());
597  sigmas->push_back(bkgestim.sigma());
598  }
599  iEvent.put(rhos,"rhos");
600  iEvent.put(sigmas,"sigmas");
601  }else{
602  std::auto_ptr<double> rho(new double(0.0));
603  std::auto_ptr<double> sigma(new double(0.0));
604  double mean_area = 0;
605 
606  fastjet::ClusterSequenceAreaBase const* clusterSequenceWithArea =
607  dynamic_cast<fastjet::ClusterSequenceAreaBase const *> ( &*fjClusterSeq_ );
608  /*
609  const double nemptyjets = clusterSequenceWithArea->n_empty_jets(*fjRangeDef_);
610  if(( nemptyjets < -15 ) || ( nemptyjets > fjRangeDef_->area()+ 15)) {
611  edm::LogWarning("StrangeNEmtpyJets") << "n_empty_jets is : " << clusterSequenceWithArea->n_empty_jets(*fjRangeDef_) << " with range " << fjRangeDef_->description() << ".";
612  }
613  */
614  clusterSequenceWithArea->get_median_rho_and_sigma(*fjRangeDef_,false,*rho,*sigma,mean_area);
615  if((*rho < 0)|| (edm::isNotFinite(*rho))) {
616  edm::LogError("BadRho") << "rho value is " << *rho << " area:" << mean_area << " and n_empty_jets: " << clusterSequenceWithArea->n_empty_jets(*fjRangeDef_) << " with range " << fjRangeDef_->description()
617  <<". Setting rho to rezo.";
618  *rho = 0;
619  }
620  iEvent.put(rho,"rho");
621  iEvent.put(sigma,"sigma");
622  }
623  } // doRhoFastjet_
624 
625  // produce output jet collection
626 
627  using namespace reco;
628 
629  std::auto_ptr<std::vector<T> > jets(new std::vector<T>() );
630  jets->reserve(fjJets_.size());
631 
632  // Distance between jet centers and overlap area -- for disk-based area calculation
633  using RIJ = std::pair<double,double>;
634  std::vector<std::vector<RIJ> > rij(fjJets_.size());
635 
636  float etaJ[fjJets_.size()], phiJ[fjJets_.size()];
637  auto etaFromXYZ = [](float x, float y, float z)->float { float t(z/std::sqrt(x*x+y*y)); return vdt::fast_logf(t + std::sqrt(t*t+1.f));};
638  for (auto ijet=0U;ijet<fjJets_.size();++ijet) {
639  float x = fjJets_[ijet].px();
640  float y = fjJets_[ijet].py();
641  float z = fjJets_[ijet].pz();
642  phiJ[ijet] = vdt::fast_atan2(y,x);
643  etaJ[ijet] =etaFromXYZ(x,y,z);
644  }
645  for (unsigned int ijet=0;ijet<fjJets_.size();++ijet) {
646  // allocate this jet
647  T jet;
648  // get the fastjet jet
649  const fastjet::PseudoJet& fjJet = fjJets_[ijet];
650  // get the constituents from fastjet
651  std::vector<fastjet::PseudoJet> fjConstituents = fastjet::sorted_by_pt(fjJet.constituents());
652  // convert them to CandidatePtr vector
653  std::vector<CandidatePtr> constituents =
654  getConstituents(fjConstituents);
655 
656  // calcuate the jet area
657  double jetArea=0.0;
658  if ( doAreaFastjet_ && fjJet.has_area() ) {
659  jetArea = fjJet.area();
660  }
661  else if ( doAreaDiskApprox_ ) {
662  // Here it is assumed that fjJets_ is in decreasing order of pT,
663  // which should happen in FastjetJetProducer::runAlgorithm()
664  jetArea = M_PI;
665  if (0!=ijet) {
666  std::vector<RIJ>& distance = rij[ijet];
667  distance.resize(ijet);
668  for (unsigned jJet = 0; jJet < ijet; ++jJet) {
669  distance[jJet].first = std::sqrt(reco::deltaR2(etaJ[ijet],phiJ[ijet], etaJ[jJet],phiJ[jJet])) / rParam_;
670  distance[jJet].second = reco::helper::VirtualJetProducerHelper::intersection(distance[jJet].first);
671  jetArea -=distance[jJet].second;
672  for (unsigned kJet = 0; kJet < jJet; ++kJet) {
673  jetArea += reco::helper::VirtualJetProducerHelper::intersection(distance[jJet].first, distance[kJet].first, rij[jJet][kJet].first,
674  distance[jJet].second, distance[kJet].second, rij[jJet][kJet].second);
675  } // end loop over harder jets
676  } // end loop over harder jets
677  }
678  jetArea *= rParam_;
679  jetArea *= rParam_;
680  }
681  // write the specifics to the jet (simultaneously sets 4-vector, vertex).
682  // These are overridden functions that will call the appropriate
683  // specific allocator.
684  writeSpecific(jet,
685  Particle::LorentzVector(fjJet.px(),
686  fjJet.py(),
687  fjJet.pz(),
688  fjJet.E()),
689  vertex_,
690  constituents, iSetup);
691 
692  jet.setJetArea (jetArea);
693 
694  if(doPUOffsetCorr_){
695  jet.setPileup(subtractor_->getPileUpEnergy(ijet));
696  }else{
697  jet.setPileup (0.0);
698  }
699 
700 
701  // std::cout << "area " << ijet << " " << jetArea << " " << Area<T>::get(jet) << std::endl;
702  // std::cout << "JetVI " << ijet << jet.pt() << " " << jet.et() << ' '<< jet.energy() << ' '<< jet.mass() << std::endl;
703 
704  // add to the list
705  jets->push_back(jet);
706  }
707  // put the jets in the collection
708  iEvent.put(jets,jetCollInstanceName_);
709 }
710 
712 template< class T>
714 {
715  if ( verbosity_ >= 1 ) {
716  std::cout << "<VirtualJetProducer::writeCompoundJets (moduleLabel = " << moduleLabel_ << ")>:" << std::endl;
717  }
718 
719  // get a list of output jets
720  std::auto_ptr<reco::BasicJetCollection> jetCollection( new reco::BasicJetCollection() );
721  // get a list of output subjets
722  std::auto_ptr<std::vector<T> > subjetCollection( new std::vector<T>() );
723 
724  // This will store the handle for the subjets after we write them
725  edm::OrphanHandle< std::vector<T> > subjetHandleAfterPut;
726  // this is the mapping of subjet to hard jet
727  std::vector< std::vector<int> > indices;
728  // this is the list of hardjet 4-momenta
729  std::vector<math::XYZTLorentzVector> p4_hardJets;
730  // this is the hardjet areas
731  std::vector<double> area_hardJets;
732 
733  // Loop over the hard jets
734  std::vector<fastjet::PseudoJet>::const_iterator it = fjJets_.begin(),
735  iEnd = fjJets_.end(),
736  iBegin = fjJets_.begin();
737  indices.resize( fjJets_.size() );
738  for ( ; it != iEnd; ++it ) {
739  fastjet::PseudoJet const & localJet = *it;
740  unsigned int jetIndex = it - iBegin;
741  // Get the 4-vector for the hard jet
742  p4_hardJets.push_back( math::XYZTLorentzVector(localJet.px(), localJet.py(), localJet.pz(), localJet.e() ));
743  double localJetArea = 0.0;
744  if ( doAreaFastjet_ && localJet.has_area() ) {
745  localJetArea = localJet.area();
746  }
747  area_hardJets.push_back( localJetArea );
748 
749  // create the subjet list
750  std::vector<fastjet::PseudoJet> constituents;
751  if ( it->has_pieces() ) {
752  constituents = it->pieces();
753  } else if ( it->has_constituents() ) {
754  constituents = it->constituents();
755  }
756 
757  std::vector<fastjet::PseudoJet>::const_iterator itSubJetBegin = constituents.begin(),
758  itSubJet = itSubJetBegin, itSubJetEnd = constituents.end();
759  for (; itSubJet != itSubJetEnd; ++itSubJet ){
760 
761  fastjet::PseudoJet const & subjet = *itSubJet;
762  if ( verbosity_ >= 1 ) {
763  std::cout << "subjet #" << (itSubJet - itSubJetBegin) << ": Pt = " << subjet.pt() << ", eta = " << subjet.eta() << ", phi = " << subjet.phi() << ", mass = " << subjet.m()
764  << " (#constituents = " << subjet.constituents().size() << ")" << std::endl;
765  std::vector<fastjet::PseudoJet> subjet_constituents = subjet.constituents();
766  int idx_constituent = 0;
767  for ( std::vector<fastjet::PseudoJet>::const_iterator constituent = subjet_constituents.begin();
768  constituent != subjet_constituents.end(); ++constituent ) {
769  if ( constituent->pt() < 1.e-3 ) continue; // CV: skip ghosts
770  std::cout << " constituent #" << idx_constituent << ": Pt = " << constituent->pt() << ", eta = " << constituent->eta() << ", phi = " << constituent->phi() << ","
771  << " mass = " << constituent->m() << std::endl;
772  ++idx_constituent;
773  }
774  }
775 
776  if ( verbosity_ >= 1 ) {
777  std::cout << "subjet #" << (itSubJet - itSubJetBegin) << ": Pt = " << subjet.pt() << ", eta = " << subjet.eta() << ", phi = " << subjet.phi() << ", mass = " << subjet.m()
778  << " (#constituents = " << subjet.constituents().size() << ")" << std::endl;
779  std::vector<fastjet::PseudoJet> subjet_constituents = subjet.constituents();
780  int idx_constituent = 0;
781  for ( std::vector<fastjet::PseudoJet>::const_iterator constituent = subjet_constituents.begin();
782  constituent != subjet_constituents.end(); ++constituent ) {
783  if ( constituent->pt() < 1.e-3 ) continue; // CV: skip ghosts
784  std::cout << " constituent #" << idx_constituent << ": Pt = " << constituent->pt() << ", eta = " << constituent->eta() << ", phi = " << constituent->phi() << ","
785  << " mass = " << constituent->m() << std::endl;
786  ++idx_constituent;
787  }
788  }
789 
790  math::XYZTLorentzVector p4Subjet(subjet.px(), subjet.py(), subjet.pz(), subjet.e() );
792 
793  // This will hold ptr's to the subjets
794  std::vector<reco::CandidatePtr> subjetConstituents;
795 
796  // Get the transient subjet constituents from fastjet
797  std::vector<fastjet::PseudoJet> subjetFastjetConstituents = subjet.constituents();
798  std::vector<reco::CandidatePtr> constituents =
799  getConstituents(subjetFastjetConstituents );
800 
801  indices[jetIndex].push_back( subjetCollection->size() );
802 
803  // Add the concrete subjet type to the subjet list to write to event record
804  T jet;
805  reco::writeSpecific( jet, p4Subjet, point, constituents, iSetup);
806  double subjetArea = 0.0;
807  if ( doAreaFastjet_ && itSubJet->has_area() ){
808  subjetArea = itSubJet->area();
809  }
810  jet.setJetArea( subjetArea );
811  subjetCollection->push_back( jet );
812  }
813  }
814 
815  // put subjets into event record
816  subjetHandleAfterPut = iEvent.put( subjetCollection, jetCollInstanceName_ );
817 
818  // Now create the hard jets with ptr's to the subjets as constituents
819  std::vector<math::XYZTLorentzVector>::const_iterator ip4 = p4_hardJets.begin(),
820  ip4Begin = p4_hardJets.begin(),
821  ip4End = p4_hardJets.end();
822 
823  for ( ; ip4 != ip4End; ++ip4 ) {
824  int p4_index = ip4 - ip4Begin;
825  std::vector<int> & ind = indices[p4_index];
826  std::vector<reco::CandidatePtr> i_hardJetConstituents;
827  // Add the subjets to the hard jet
828  for( std::vector<int>::const_iterator isub = ind.begin();
829  isub != ind.end(); ++isub ) {
830  reco::CandidatePtr candPtr( subjetHandleAfterPut, *isub, false );
831  i_hardJetConstituents.push_back( candPtr );
832  }
834  reco::BasicJet toput( *ip4, point, i_hardJetConstituents);
835  toput.setJetArea( area_hardJets[ip4 - ip4Begin] );
836  jetCollection->push_back( toput );
837  }
838 
839  // put hard jets into event record
840  iEvent.put( jetCollection);
841 }
#define LogDebug(id)
RunNumber_t run() const
Definition: EventID.h:39
boost::shared_ptr< fastjet::AreaDefinition > AreaDefinitionPtr
void writeJets(edm::Event &iEvent, edm::EventSetup const &iSetup)
JetType::Type jetTypeE
T getParameter(std::string const &) const
EventNumber_t event() const
Definition: EventID.h:41
T getUntrackedParameter(std::string const &, T const &) const
int i
Definition: DBlmapReader.cc:9
reco::Particle::Point vertex_
Jets made from CaloTowers.
Definition: CaloJet.h:29
static const HistoName names[]
math::PtEtaPhiMLorentzVector p4(double vtxZ) const
Definition: CaloTower.cc:129
edm::EDGetTokenT< reco::CandidateView > input_candidateview_token_
virtual std::vector< reco::CandidatePtr > getConstituents(const std::vector< fastjet::PseudoJet > &fjConstituents)
std::vector< fastjet::PseudoJet > fjJets_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:446
virtual void inputTowers()
T const * get() const
Returns C++ pointer to the item.
Definition: Ptr.h:143
Base class for all types of Jets.
Definition: Jet.h:20
boost::shared_ptr< fastjet::JetDefinition::Plugin > PluginPtr
Definition: DDAxes.h:10
bool exists(std::string const &parameterName) const
checks if a parameter exists
std::string puSubtractorName_
std::vector< double > puCenters_
T eta() const
virtual void copyConstituents(const std::vector< fastjet::PseudoJet > &fjConstituents, reco::Jet *jet)
Jets made from CaloTowers.
Definition: BasicJet.h:20
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:7
float float float z
virtual void runAlgorithm(edm::Event &iEvent, const edm::EventSetup &iSetup)=0
static std::string const input
Definition: EdmProvDump.cc:44
void writeCompoundJets(edm::Event &iEvent, edm::EventSetup const &iSetup)
function template to write out the outputs
bool operator()(const fastjet::PseudoJet &t1, const fastjet::PseudoJet &t2) const
boost::shared_ptr< fastjet::RangeDefinition > RangeDefPtr
U second(std::pair< T, U > const &p)
PtEtaPhiMLorentzVectorD PtEtaPhiMLorentzVector
Lorentz vector with cartesian internal representation.
Definition: LorentzVector.h:25
bool makePFJet(const JetType::Type &fTag)
XYZTLorentzVectorD XYZTLorentzVector
Lorentz vector with cylindrical internal representation using pseudorapidity.
Definition: LorentzVector.h:29
virtual bool isAnomalousTower(reco::CandidatePtr input)
virtual void setJetArea(float fArea)
set jet area
Definition: Jet.h:103
static const char * names[]
std::vector< fastjet::PseudoJet > fjInputs_
int iEvent
Definition: GenABIO.cc:230
std::string jetCollInstanceName_
boost::shared_ptr< PileUpSubtractor > subtractor_
bool isNotFinite(T x)
Definition: isFinite.h:10
std::vector< edm::Ptr< reco::Candidate > > inputs_
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:113
ClusterSequencePtr fjClusterSeq_
virtual void makeProduces(std::string s, std::string tag="")
ConsumesCollector consumesCollector()
Use a ConsumesCollector to gather consumes information from helper functions.
T sqrt(T t)
Definition: SSEVec.h:48
vector< PseudoJet > jets
tuple result
Definition: query.py:137
math::XYZPoint Point
point in the space
Definition: Particle.h:31
double deltaR2(const T1 &t1, const T2 &t2)
Definition: deltaR.h:36
void set_excluded_jets(const std::vector< PseudoJet > &excluded_jets)
double f[11][100]
boost::shared_ptr< fastjet::JetDefinition > JetDefPtr
std::auto_ptr< AnomalousTower > anomalousTowerDef_
bool first
Definition: L1TdeRCT.cc:75
ActiveAreaSpecPtr fjActiveArea_
#define M_PI
edm::EDGetTokenT< reco::VertexCollection > input_vertex_token_
virtual void produce(edm::Event &iEvent, const edm::EventSetup &iSetup)
double rho()
synonym for median rho [[do we have this? Both?]]
edm::EDGetTokenT< std::vector< edm::FwdPtr< reco::PFCandidate > > > input_candidatefwdptr_token_
double sigma()
get the sigma
std::string const & label() const
Definition: InputTag.h:42
edm::EventID id() const
Definition: EventBase.h:56
VirtualJetProducer(const edm::ParameterSet &iConfig)
boost::shared_ptr< fastjet::GhostedAreaSpec > ActiveAreaSpecPtr
void addDaughter(const CandidatePtr &)
add a daughter via a reference
tuple cout
Definition: gather_cfg.py:121
AreaDefinitionPtr fjAreaDefinition_
virtual void output(edm::Event &iEvent, edm::EventSetup const &iSetup)
volatile std::atomic< bool > shutdown_flag false
Definition: DDAxes.h:10
moduleLabel_(iConfig.getParameter< string >("@module_label"))
long double T
bool makeCaloJet(const JetType::Type &fTag)
static Type byName(const std::string &name)
float fast_logf(float x)
T get(const Candidate &c)
Definition: component.h:55
*vegas h *****************************************************used in the default bin number in original ***version of VEGAS is ***a higher bin number might help to derive a more precise ***grade subtle point
Definition: invegas.h:5
math::PtEtaPhiELorentzVectorF LorentzVector
math::XYZPoint Point
point in the space
Definition: LeafCandidate.h:32
void writeSpecific(reco::CaloJet &jet, reco::Particle::LorentzVector const &p4, reco::Particle::Point const &point, std::vector< reco::CandidatePtr > const &constituents, edm::EventSetup const &c)
Definition: JetSpecific.cc:41
std::vector< BasicJet > BasicJetCollection
collection of BasicJet objects