CMS 3D CMS Logo

FFTJetPileupProcessor.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: RecoJets/FFTJetProducers
4 // Class: FFTJetPileupProcessor
5 //
13 //
14 // Original Author: Igor Volobouev
15 // Created: Wed Apr 20 13:52:23 CDT 2011
16 //
17 //
18 
19 #include <cmath>
20 #include <fstream>
21 
22 // FFTJet headers
23 #include "fftjet/FrequencyKernelConvolver.hh"
24 #include "fftjet/DiscreteGauss2d.hh"
25 #include "fftjet/EquidistantSequence.hh"
26 #include "fftjet/interpolate.hh"
27 
28 // framework include files
32 
35 
37 
38 // parameter parser header
40 
41 // useful utilities collected in the second base
43 
44 // Loader for the lookup tables
46 
47 using namespace fftjetcms;
48 
49 //
50 // class declaration
51 //
53 public:
55  ~FFTJetPileupProcessor() override;
56 
57 protected:
58  // methods
59  void beginJob() override;
60  void produce(edm::Event&, const edm::EventSetup&) override;
61  void endJob() override;
62 
63 private:
64  FFTJetPileupProcessor() = delete;
66  FFTJetPileupProcessor& operator=(const FFTJetPileupProcessor&) = delete;
67 
68  void buildKernelConvolver(const edm::ParameterSet&);
69  void mixExtraGrid();
70  void loadFlatteningFactors(const edm::EventSetup& iSetup);
71 
72  // The FFT engine
73  std::unique_ptr<MyFFTEngine> engine;
74 
75  // The pattern recognition kernel(s)
76  std::unique_ptr<fftjet::AbsFrequencyKernel> kernel2d;
77 
78  // The kernel convolver
79  std::unique_ptr<fftjet::AbsConvolverBase<Real>> convolver;
80 
81  // Storage for convolved energy flow
82  std::unique_ptr<fftjet::Grid2d<fftjetcms::Real>> convolvedFlow;
83 
84  // Filtering scales
85  std::unique_ptr<fftjet::EquidistantInLogSpace> filterScales;
86 
87  // Eta-dependent factors to use for flattening the distribution
88  // _after_ the filtering
89  std::vector<double> etaFlatteningFactors;
90 
91  // Number of percentile points to use
92  unsigned nPercentiles;
93 
94  // Bin range. Both values of 0 means use the whole range.
95  unsigned convolverMinBin;
96  unsigned convolverMaxBin;
97 
98  // Density conversion factor
100 
101  // Variable related to mixing additional grids
102  std::vector<std::string> externalGridFiles;
103  std::ifstream gridStream;
105  unsigned currentFileNum;
106 
107  // Some memory to hold the percentiles found
108  std::vector<double> percentileData;
109 
110  // Variables to load the flattening factors from
111  // the calibration database (this has to be done
112  // in sync with configuring the appropriate event
113  // setup source)
118 };
119 
120 //
121 // constructors and destructor
122 //
124  : FFTJetInterface(ps),
125  etaFlatteningFactors(ps.getParameter<std::vector<double>>("etaFlatteningFactors")),
126  nPercentiles(ps.getParameter<unsigned>("nPercentiles")),
127  convolverMinBin(ps.getParameter<unsigned>("convolverMinBin")),
128  convolverMaxBin(ps.getParameter<unsigned>("convolverMaxBin")),
129  pileupEtaPhiArea(ps.getParameter<double>("pileupEtaPhiArea")),
130  externalGridFiles(ps.getParameter<std::vector<std::string>>("externalGridFiles")),
131  externalGridMaxEnergy(ps.getParameter<double>("externalGridMaxEnergy")),
132  currentFileNum(externalGridFiles.size() + 1U),
133  flatteningTableRecord(ps.getParameter<std::string>("flatteningTableRecord")),
134  flatteningTableName(ps.getParameter<std::string>("flatteningTableName")),
135  flatteningTableCategory(ps.getParameter<std::string>("flatteningTableCategory")),
136  loadFlatteningFactorsFromDB(ps.getParameter<bool>("loadFlatteningFactorsFromDB")) {
137  // Build the discretization grid
139  checkConfig(energyFlow, "invalid discretization grid");
140 
141  // Copy of the grid which will be used for convolutions
142  convolvedFlow = std::unique_ptr<fftjet::Grid2d<fftjetcms::Real>>(new fftjet::Grid2d<fftjetcms::Real>(*energyFlow));
143 
144  // Make sure the size of flattening factors is appropriate
145  if (!etaFlatteningFactors.empty()) {
146  if (etaFlatteningFactors.size() != convolvedFlow->nEta())
147  throw cms::Exception("FFTJetBadConfig") << "ERROR in FFTJetPileupProcessor constructor:"
148  " number of elements in the \"etaFlatteningFactors\""
149  " vector is inconsistent with the discretization grid binning"
150  << std::endl;
151  }
152 
153  // Build the FFT engine(s), pattern recognition kernel(s),
154  // and the kernel convolver
156 
157  // Build the set of pattern recognition scales
158  const unsigned nScales = ps.getParameter<unsigned>("nScales");
159  const double minScale = ps.getParameter<double>("minScale");
160  const double maxScale = ps.getParameter<double>("maxScale");
161  if (minScale <= 0.0 || maxScale < minScale || nScales == 0U)
162  throw cms::Exception("FFTJetBadConfig") << "invalid filter scales" << std::endl;
163 
164  filterScales =
165  std::unique_ptr<fftjet::EquidistantInLogSpace>(new fftjet::EquidistantInLogSpace(minScale, maxScale, nScales));
166 
168 
169  produces<reco::DiscretizedEnergyFlow>(outputLabel);
170  produces<std::pair<double, double>>(outputLabel);
171 }
172 
174  // Get the eta and phi scales for the kernel(s)
175  double kernelEtaScale = ps.getParameter<double>("kernelEtaScale");
176  const double kernelPhiScale = ps.getParameter<double>("kernelPhiScale");
177  if (kernelEtaScale <= 0.0 || kernelPhiScale <= 0.0)
178  throw cms::Exception("FFTJetBadConfig") << "invalid kernel scales" << std::endl;
179 
182  throw cms::Exception("FFTJetBadConfig") << "invalid convolver bin range" << std::endl;
183 
184  // FFT assumes that the grid extent in eta is 2*Pi. Adjust the
185  // kernel scale in eta to compensate.
186  kernelEtaScale *= (2.0 * M_PI / (energyFlow->etaMax() - energyFlow->etaMin()));
187 
188  // Build the FFT engine
189  engine = std::unique_ptr<MyFFTEngine>(new MyFFTEngine(energyFlow->nEta(), energyFlow->nPhi()));
190 
191  // 2d kernel
192  kernel2d = std::unique_ptr<fftjet::AbsFrequencyKernel>(
193  new fftjet::DiscreteGauss2d(kernelEtaScale, kernelPhiScale, energyFlow->nEta(), energyFlow->nPhi()));
194 
195  // 2d convolver
196  convolver = std::unique_ptr<fftjet::AbsConvolverBase<Real>>(new fftjet::FrequencyKernelConvolver<Real, Complex>(
198 }
199 
201 
202 // ------------ method called to produce the data ------------
206 
207  // Determine the average Et density for this event.
208  // Needs to be done here, before mixing in another grid.
209  const fftjet::Grid2d<Real>& g(*energyFlow);
210  const double densityBeforeMixing = g.sum() / pileupEtaPhiArea;
211 
212  // Mix an extra grid (if requested)
213  double densityAfterMixing = -1.0;
214  if (!externalGridFiles.empty()) {
215  mixExtraGrid();
216  densityAfterMixing = g.sum() / pileupEtaPhiArea;
217  }
218 
219  // Various useful variables
220  const unsigned nScales = filterScales->size();
221  const double* scales = &(*filterScales)[0];
222  Real* convData = const_cast<Real*>(convolvedFlow->data());
223  Real* sortData = convData + convolverMinBin * convolvedFlow->nPhi();
224  const unsigned dataLen = convolverMaxBin ? (convolverMaxBin - convolverMinBin) * convolvedFlow->nPhi()
225  : convolvedFlow->nEta() * convolvedFlow->nPhi();
226 
227  // Load flattenning factors from DB (if requested)
229  loadFlatteningFactors(iSetup);
230 
231  // Go over all scales and perform the convolutions
232  convolver->setEventData(g.data(), g.nEta(), g.nPhi());
233  for (unsigned iscale = 0; iscale < nScales; ++iscale) {
234  // Perform the convolution
235  convolver->convolveWithKernel(scales[iscale], convData, convolvedFlow->nEta(), convolvedFlow->nPhi());
236 
237  // Apply the flattening factors
238  if (!etaFlatteningFactors.empty())
240 
241  // Sort the convolved data
242  std::sort(sortData, sortData + dataLen);
243 
244  // Determine the percentile points
245  for (unsigned iper = 0; iper < nPercentiles; ++iper) {
246  // Map percentile 0 into point number 0,
247  // 1 into point number dataLen-1
248  const double q = (iper + 0.5) / nPercentiles;
249  const double dindex = q * (dataLen - 1U);
250  const unsigned ilow = static_cast<unsigned>(std::floor(dindex));
251  const double percentile =
252  fftjet::lin_interpolate_1d(ilow, ilow + 1U, sortData[ilow], sortData[ilow + 1U], dindex);
253 
254  // Store the calculated percentile
255  percentileData[iscale * nPercentiles + iper] = percentile;
256  }
257  }
258 
259  // Convert percentile data into a more convenient storable object
260  // and put it into the event record
261  iEvent.put(std::make_unique<reco::DiscretizedEnergyFlow>(
262  &percentileData[0], "FFTJetPileupProcessor", -0.5, nScales - 0.5, 0.0, nScales, nPercentiles),
263  outputLabel);
264 
265  iEvent.put(std::make_unique<std::pair<double, double>>(densityBeforeMixing, densityAfterMixing), outputLabel);
266 }
267 
269  const unsigned nFiles = externalGridFiles.size();
270  if (currentFileNum > nFiles) {
271  // This is the first time this function is called
272  currentFileNum = 0;
273  gridStream.open(externalGridFiles[currentFileNum].c_str(), std::ios_base::in | std::ios_base::binary);
274  if (!gridStream.is_open())
275  throw cms::Exception("FFTJetBadConfig") << "ERROR in FFTJetPileupProcessor::mixExtraGrid():"
276  " failed to open external grid file "
277  << externalGridFiles[currentFileNum] << std::endl;
278  }
279 
280  const fftjet::Grid2d<float>* g = nullptr;
281  const unsigned maxFail = 100U;
282  unsigned nEnergyRejected = 0;
283 
284  while (!g) {
286 
287  // If we can't read the grid, we need to switch to another file
288  for (unsigned ntries = 0; ntries < nFiles && g == nullptr; ++ntries) {
289  gridStream.close();
291  gridStream.open(externalGridFiles[currentFileNum].c_str(), std::ios_base::in | std::ios_base::binary);
292  if (!gridStream.is_open())
293  throw cms::Exception("FFTJetBadConfig") << "ERROR in FFTJetPileupProcessor::mixExtraGrid():"
294  " failed to open external grid file "
295  << externalGridFiles[currentFileNum] << std::endl;
297  }
298 
299  if (g)
300  if (g->sum() > externalGridMaxEnergy) {
301  delete g;
302  g = nullptr;
303  if (++nEnergyRejected >= maxFail)
304  throw cms::Exception("FFTJetBadConfig") << "ERROR in FFTJetPileupProcessor::mixExtraGrid():"
305  " too many grids in a row ("
306  << nEnergyRejected << ") failed the maximum energy cut" << std::endl;
307  }
308  }
309 
310  if (g) {
311  add_Grid2d_data(energyFlow.get(), *g);
312  delete g;
313  } else {
314  // Too bad, no useful file found
315  throw cms::Exception("FFTJetBadConfig") << "ERROR in FFTJetPileupProcessor::mixExtraGrid():"
316  " no valid grid records found"
317  << std::endl;
318  }
319 }
320 
321 // ------------ method called once each job just before starting event loop
323 
324 // ------------ method called once each job just after ending the event loop
326 
330  std::shared_ptr<npstat::StorableMultivariateFunctor> f = (*h)[flatteningTableCategory][flatteningTableName];
331 
332  // Fill out the table of flattening factors as a function of eta
333  const unsigned nEta = energyFlow->nEta();
334  etaFlatteningFactors.clear();
335  etaFlatteningFactors.reserve(nEta);
336  for (unsigned ieta = 0; ieta < nEta; ++ieta) {
337  const double eta = energyFlow->etaBinCenter(ieta);
338  etaFlatteningFactors.push_back((*f)(&eta, 1U));
339  }
340 }
341 
342 //define this as a plug-in
bk::beginJob
void beginJob()
Definition: Breakpoints.cc:14
electrons_cff.bool
bool
Definition: electrons_cff.py:372
FFTJetPileupProcessor::gridStream
std::ifstream gridStream
Definition: FFTJetPileupProcessor.cc:103
fftjetpileupprocessor_caloprod_cfi.loadFlatteningFactorsFromDB
loadFlatteningFactorsFromDB
Definition: fftjetpileupprocessor_caloprod_cfi.py:88
fftjetcms
Definition: AbsPileupCalculator.h:15
fftjetcms::FFTJetInterface::checkConfig
void checkConfig(const Ptr &ptr, const char *message)
Definition: FFTJetInterface.h:60
fftjetcommon_cfi.nScales
nScales
Definition: fftjetcommon_cfi.py:111
fftjetcms::FFTJetInterface::loadInputCollection
void loadInputCollection(const edm::Event &)
Definition: FFTJetInterface.cc:40
f
double f[11][100]
Definition: MuScleFitUtils.cc:78
fftjetcms::FFTJetInterface::outputLabel
const std::string outputLabel
Definition: FFTJetInterface.h:76
fftjetcommon_cfi.maxScale
maxScale
Definition: fftjetcommon_cfi.py:110
h
FWCore Framework interface EventSetupRecordImplementation h
Helper function to determine trigger accepts.
Definition: L1TUtmAlgorithmRcd.h:4
fftjetpileupprocessor_caloprod_cfi.flatteningTableRecord
flatteningTableRecord
Definition: fftjetpileupprocessor_caloprod_cfi.py:85
data-class-funcs.q
q
Definition: data-class-funcs.py:169
fftjeteflowsmoother_cfi.convolverMaxBin
convolverMaxBin
Definition: fftjeteflowsmoother_cfi.py:21
fftjetpileupprocessor_caloprod_cfi.flatteningTableName
flatteningTableName
Definition: fftjetpileupprocessor_caloprod_cfi.py:86
fftjetpileupprocessor_caloprod_cfi.externalGridMaxEnergy
externalGridMaxEnergy
Definition: fftjetpileupprocessor_caloprod_cfi.py:74
fftjetpileupprocessor_caloprod_cfi.pileupEtaPhiArea
pileupEtaPhiArea
Definition: fftjetpileupprocessor_caloprod_cfi.py:59
FFTJetPileupProcessor::nPercentiles
unsigned nPercentiles
Definition: FFTJetPileupProcessor.cc:92
FFTJetLookupTableSequenceLoader.h
fftjetpileupprocessor_caloprod_cfi.flatteningTableCategory
flatteningTableCategory
Definition: fftjetpileupprocessor_caloprod_cfi.py:87
mergeAndRegister.ntries
ntries
Definition: mergeAndRegister.py:93
FFTJetPileupProcessor::produce
void produce(edm::Event &, const edm::EventSetup &) override
Definition: FFTJetPileupProcessor.cc:203
FFTJetPileupProcessor::pileupEtaPhiArea
double pileupEtaPhiArea
Definition: FFTJetPileupProcessor.cc:99
fftjetcms::add_Grid2d_data
void add_Grid2d_data(fftjet::Grid2d< F2 > *to, const fftjet::Grid2d< F1 > &from)
Definition: gridConverters.h:51
FFTJetInterface.h
FFTJetPileupProcessor::~FFTJetPileupProcessor
~FFTJetPileupProcessor() override
Definition: FFTJetPileupProcessor.cc:200
FFTJetPileupProcessor::kernel2d
std::unique_ptr< fftjet::AbsFrequencyKernel > kernel2d
Definition: FFTJetPileupProcessor.cc:76
FFTJetPileupProcessor::loadFlatteningFactors
void loadFlatteningFactors(const edm::EventSetup &iSetup)
Definition: FFTJetPileupProcessor.cc:327
MakerMacros.h
FFTJetPileupProcessor::filterScales
std::unique_ptr< fftjet::EquidistantInLogSpace > filterScales
Definition: FFTJetPileupProcessor.cc:85
h
DEFINE_FWK_MODULE
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
FFTJetPileupProcessor::currentFileNum
unsigned currentFileNum
Definition: FFTJetPileupProcessor.cc:105
fftjeteflowsmoother_cfi.kernelEtaScale
kernelEtaScale
Definition: fftjeteflowsmoother_cfi.py:11
PVValHelper::eta
Definition: PVValidationHelpers.h:69
fftjetcms::FFTJetInterface
Definition: FFTJetInterface.h:52
edm::ESHandle
Definition: DTSurvey.h:22
fftjetcms::MyFFTEngine
fftjet::FFTWDoubleEngine MyFFTEngine
Definition: fftjetTypedefs.h:23
FFTJetPileupProcessor::externalGridMaxEnergy
double externalGridMaxEnergy
Definition: FFTJetPileupProcessor.cc:104
FFTJetPileupProcessor::percentileData
std::vector< double > percentileData
Definition: FFTJetPileupProcessor.cc:108
mitigatedMETSequence_cff.U
U
Definition: mitigatedMETSequence_cff.py:36
fftjetcms::Real
double Real
Definition: fftjetTypedefs.h:21
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
FFTJetPileupProcessor::endJob
void endJob() override
Definition: FFTJetPileupProcessor.cc:325
FFTJetPileupProcessor
Definition: FFTJetPileupProcessor.cc:52
LEDCalibrationChannels.ieta
ieta
Definition: LEDCalibrationChannels.py:63
edm::ParameterSet
Definition: ParameterSet.h:36
FFTJetPileupProcessor::flatteningTableName
std::string flatteningTableName
Definition: FFTJetPileupProcessor.cc:115
FFTJetPileupProcessor::convolver
std::unique_ptr< fftjet::AbsConvolverBase< Real > > convolver
Definition: FFTJetPileupProcessor.cc:79
FFTJetPileupProcessor::etaFlatteningFactors
std::vector< double > etaFlatteningFactors
Definition: FFTJetPileupProcessor.cc:89
FFTJetPileupProcessor::convolverMaxBin
unsigned convolverMaxBin
Definition: FFTJetPileupProcessor.cc:96
recoMuon::in
Definition: RecoMuonEnumerators.h:6
FFTJetPileupProcessor::mixExtraGrid
void mixExtraGrid()
Definition: FFTJetPileupProcessor.cc:268
StaticFFTJetRcdMapper::instance
static const Mapper & instance()
Definition: FFTJetRcdMapper.h:86
FFTJetPileupProcessor::buildKernelConvolver
void buildKernelConvolver(const edm::ParameterSet &)
Definition: FFTJetPileupProcessor.cc:173
iEvent
int iEvent
Definition: GenABIO.cc:224
fftjetcms::FFTJetInterface::energyFlow
std::unique_ptr< fftjet::Grid2d< fftjetcms::Real > > energyFlow
Definition: FFTJetInterface.h:100
fftjetcommon_cfi.minScale
minScale
Definition: fftjetcommon_cfi.py:109
M_PI
#define M_PI
Definition: BXVectorInputProducer.cc:50
HLT_2018_cff.nEta
nEta
Definition: HLT_2018_cff.py:5271
fftjetcms::fftjet_Grid2d_parser
std::unique_ptr< fftjet::Grid2d< Real > > fftjet_Grid2d_parser(const edm::ParameterSet &ps)
Definition: FFTJetParameterParser.cc:123
edm::EventSetup
Definition: EventSetup.h:57
FFTJetPileupProcessor::convolvedFlow
std::unique_ptr< fftjet::Grid2d< fftjetcms::Real > > convolvedFlow
Definition: FFTJetPileupProcessor.cc:82
fftjeteflowsmoother_cfi.convolverMinBin
convolverMinBin
Definition: fftjeteflowsmoother_cfi.py:20
FFTJetPileupProcessor::flatteningTableCategory
std::string flatteningTableCategory
Definition: FFTJetPileupProcessor.cc:116
FFTJetPileupProcessor::flatteningTableRecord
std::string flatteningTableRecord
Definition: FFTJetPileupProcessor.cc:114
FFTJetPileupProcessor::engine
std::unique_ptr< MyFFTEngine > engine
Definition: FFTJetPileupProcessor.cc:73
fftjeteflowsmoother_cfi.kernelPhiScale
kernelPhiScale
Definition: fftjeteflowsmoother_cfi.py:12
fftjetpileupprocessor_caloprod_cfi.etaFlatteningFactors
etaFlatteningFactors
Definition: fftjetpileupprocessor_caloprod_cfi.py:49
readEcalDQMStatus.read
read
Definition: readEcalDQMStatus.py:38
edm::ParameterSet::getParameter
T getParameter(std::string const &) const
std
Definition: JetResolutionObject.h:76
fftjetpileupprocessor_caloprod_cfi.externalGridFiles
externalGridFiles
Definition: fftjetpileupprocessor_caloprod_cfi.py:71
Frameworkfwd.h
Exception
Definition: hltDiff.cc:246
FFTJetParameterParser.h
cmsBatch.nFiles
nFiles
Definition: cmsBatch.py:308
fftjetcms::FFTJetInterface::discretizeEnergyFlow
void discretizeEnergyFlow()
Definition: FFTJetInterface.cc:79
DiscretizedEnergyFlow.h
cms::Exception
Definition: Exception.h:70
View.h
ParameterSet.h
FFTJetPileupProcessor::FFTJetPileupProcessor
FFTJetPileupProcessor()=delete
edm::Event
Definition: Event.h:73
FFTJetPileupProcessor::externalGridFiles
std::vector< std::string > externalGridFiles
Definition: FFTJetPileupProcessor.cc:102
fftjetpileupprocessor_caloprod_cfi.nPercentiles
nPercentiles
Definition: fftjetpileupprocessor_caloprod_cfi.py:68
FFTJetPileupProcessor::convolverMinBin
unsigned convolverMinBin
Definition: FFTJetPileupProcessor.cc:95
FFTJetPileupProcessor::loadFlatteningFactorsFromDB
bool loadFlatteningFactorsFromDB
Definition: FFTJetPileupProcessor.cc:117
gridConverters.h
FFTJetPileupProcessor::beginJob
void beginJob() override
Definition: FFTJetPileupProcessor.cc:322
g
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e g
Definition: Activities.doc:4
findQualityFiles.size
size
Write out results.
Definition: findQualityFiles.py:443