CMS 3D CMS Logo

FFTJetEFlowSmoother.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: FFTJetProducers
4 // Class: FFTJetEFlowSmoother
5 //
13 //
14 // Original Author: Igor Volobouev
15 // Created: Thu Jun 2 18:49:49 CDT 2011
16 //
17 //
18 
19 #include <cmath>
20 #include <memory>
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 #include "fftjet/FrequencyKernelConvolver.hh"
28 #include "fftjet/FrequencySequentialConvolver.hh"
29 #include "fftjet/DiscreteGauss1d.hh"
30 #include "fftjet/DiscreteGauss2d.hh"
31 
32 // framework include files
36 
38 #include <TH3F.h>
39 
40 // parameter parser header
42 
43 // useful utilities collected in the second base
45 
46 using namespace fftjetcms;
47 
48 //
49 // class declaration
50 //
52 public:
53  explicit FFTJetEFlowSmoother(const edm::ParameterSet&);
54  ~FFTJetEFlowSmoother() override;
55 
56 protected:
57  // methods
58  void beginJob() override;
59  void produce(edm::Event&, const edm::EventSetup&) override;
60  void endJob() override;
61 
62 private:
63  FFTJetEFlowSmoother() = delete;
65  FFTJetEFlowSmoother& operator=(const FFTJetEFlowSmoother&) = delete;
66 
67  void buildKernelConvolver(const edm::ParameterSet&);
68 
69  // Storage for convolved energy flow
70  std::unique_ptr<fftjet::Grid2d<fftjetcms::Real> > convolvedFlow;
71 
72  // Filtering scales
73  std::unique_ptr<std::vector<double> > iniScales;
74 
75  // The FFT engine(s)
76  std::unique_ptr<MyFFTEngine> engine;
77  std::unique_ptr<MyFFTEngine> anotherEngine;
78 
79  // The pattern recognition kernel(s)
80  std::unique_ptr<fftjet::AbsFrequencyKernel> kernel2d;
81  std::unique_ptr<fftjet::AbsFrequencyKernel1d> etaKernel;
82  std::unique_ptr<fftjet::AbsFrequencyKernel1d> phiKernel;
83 
84  // The kernel convolver
85  std::unique_ptr<fftjet::AbsConvolverBase<Real> > convolver;
86 
87  // The scale power to use for scaling Et
88  double scalePower;
89 
90  // Overall factor to multiply Et with
92 };
93 
94 //
95 // constructors and destructor
96 //
98  : FFTJetInterface(ps),
99  scalePower(ps.getParameter<double>("scalePower")),
100  etConversionFactor(ps.getParameter<double>("etConversionFactor")) {
101  // Build the discretization grid
103  checkConfig(energyFlow, "invalid discretization grid");
104 
105  // Copy of the grid which will be used for convolutions
106  convolvedFlow = std::make_unique<fftjet::Grid2d<fftjetcms::Real> >(*energyFlow);
107 
108  // Build the initial set of pattern recognition scales
110  checkConfig(iniScales, "invalid set of scales");
111 
112  // Build the FFT engine(s), pattern recognition kernel(s),
113  // and the kernel convolver
115 
116  // Build the set of pattern recognition scales
117  produces<TH3F>(outputLabel);
118 }
119 
121  // Check the parameter named "etaDependentScaleFactors". If the vector
122  // of scales is empty we will use 2d kernel, otherwise use 1d kernels
123  const std::vector<double> etaDependentScaleFactors(ps.getParameter<std::vector<double> >("etaDependentScaleFactors"));
124 
125  // Make sure that the number of scale factors provided is correct
126  const bool use2dKernel = etaDependentScaleFactors.empty();
127  if (!use2dKernel)
128  if (etaDependentScaleFactors.size() != energyFlow->nEta())
129  throw cms::Exception("FFTJetBadConfig") << "invalid number of eta-dependent scale factors" << std::endl;
130 
131  // Get the eta and phi scales for the kernel(s)
132  double kernelEtaScale = ps.getParameter<double>("kernelEtaScale");
133  const double kernelPhiScale = ps.getParameter<double>("kernelPhiScale");
134  if (kernelEtaScale <= 0.0 || kernelPhiScale <= 0.0)
135  throw cms::Exception("FFTJetBadConfig") << "invalid kernel scale" << std::endl;
136 
137  // FFT assumes that the grid extent in eta is 2*Pi. Adjust the
138  // kernel scale in eta to compensate.
139  kernelEtaScale *= (2.0 * M_PI / (energyFlow->etaMax() - energyFlow->etaMin()));
140 
141  // Are we going to try to fix the efficiency near detector edges?
142  const bool fixEfficiency = ps.getParameter<bool>("fixEfficiency");
143 
144  // Minimum and maximum eta bin for the convolver
145  unsigned convolverMinBin = 0, convolverMaxBin = 0;
146  if (fixEfficiency || !use2dKernel) {
147  convolverMinBin = ps.getParameter<unsigned>("convolverMinBin");
148  convolverMaxBin = ps.getParameter<unsigned>("convolverMaxBin");
149  }
150 
151  if (use2dKernel) {
152  // Build the FFT engine
153  engine = std::make_unique<MyFFTEngine>(energyFlow->nEta(), energyFlow->nPhi());
154 
155  // 2d kernel
156  kernel2d = std::unique_ptr<fftjet::AbsFrequencyKernel>(
157  new fftjet::DiscreteGauss2d(kernelEtaScale, kernelPhiScale, energyFlow->nEta(), energyFlow->nPhi()));
158 
159  // 2d convolver
160  convolver = std::unique_ptr<fftjet::AbsConvolverBase<Real> >(new fftjet::FrequencyKernelConvolver<Real, Complex>(
162  } else {
163  // Need separate FFT engines for eta and phi
164  engine = std::make_unique<MyFFTEngine>(1, energyFlow->nEta());
165  anotherEngine = std::make_unique<MyFFTEngine>(1, energyFlow->nPhi());
166 
167  // 1d kernels
168  etaKernel =
169  std::unique_ptr<fftjet::AbsFrequencyKernel1d>(new fftjet::DiscreteGauss1d(kernelEtaScale, energyFlow->nEta()));
170 
171  phiKernel =
172  std::unique_ptr<fftjet::AbsFrequencyKernel1d>(new fftjet::DiscreteGauss1d(kernelPhiScale, energyFlow->nPhi()));
173 
174  // Sequential convolver
175  convolver = std::unique_ptr<fftjet::AbsConvolverBase<Real> >(
176  new fftjet::FrequencySequentialConvolver<Real, Complex>(engine.get(),
177  anotherEngine.get(),
178  etaKernel.get(),
179  phiKernel.get(),
183  fixEfficiency));
184  }
185 }
186 
188 
189 // ------------ method called to produce the data ------------
193 
194  // Various useful variables
195  const fftjet::Grid2d<Real>& g(*energyFlow);
196  const unsigned nScales = iniScales->size();
197  const double* scales = &(*iniScales)[0];
198  Real* convData = const_cast<Real*>(convolvedFlow->data());
199  const unsigned nEta = g.nEta();
200  const unsigned nPhi = g.nPhi();
201  const double bin0edge = g.phiBin0Edge();
202 
203  // We will fill the following histo
204  auto pTable = std::make_unique<TH3F>("FFTJetEFlowSmoother",
205  "FFTJetEFlowSmoother",
206  nScales + 1U,
207  -1.5,
208  nScales - 0.5,
209  nEta,
210  g.etaMin(),
211  g.etaMax(),
212  nPhi,
213  bin0edge,
214  bin0edge + 2.0 * M_PI);
215  TH3F* h = pTable.get();
216  h->SetDirectory(nullptr);
217  h->GetXaxis()->SetTitle("Scale");
218  h->GetYaxis()->SetTitle("Eta");
219  h->GetZaxis()->SetTitle("Phi");
220 
221  // Fill the original thing
223  for (unsigned ieta = 0; ieta < nEta; ++ieta)
224  for (unsigned iphi = 0; iphi < nPhi; ++iphi)
225  h->SetBinContent(1U, ieta + 1U, iphi + 1U, factor * g.binValue(ieta, iphi));
226 
227  // Go over all scales and perform the convolutions
228  convolver->setEventData(g.data(), nEta, nPhi);
229  for (unsigned iscale = 0; iscale < nScales; ++iscale) {
230  factor = etConversionFactor * pow(scales[iscale], scalePower);
231 
232  // Perform the convolution
233  convolver->convolveWithKernel(scales[iscale], convData, convolvedFlow->nEta(), convolvedFlow->nPhi());
234 
235  // Fill the output histo
236  for (unsigned ieta = 0; ieta < nEta; ++ieta) {
237  const Real* etaData = convData + ieta * nPhi;
238  for (unsigned iphi = 0; iphi < nPhi; ++iphi)
239  h->SetBinContent(iscale + 2U, ieta + 1U, iphi + 1U, factor * etaData[iphi]);
240  }
241  }
242 
243  iEvent.put(std::move(pTable), outputLabel);
244 }
245 
246 // ------------ method called once each job just before starting event loop
248 
249 // ------------ method called once each job just after ending the event loop
251 
252 //define this as a plug-in
fftjeteflowsmoother_cfi.etConversionFactor
etConversionFactor
Definition: fftjeteflowsmoother_cfi.py:55
bk::beginJob
void beginJob()
Definition: Breakpoints.cc:14
FFTJetEFlowSmoother::phiKernel
std::unique_ptr< fftjet::AbsFrequencyKernel1d > phiKernel
Definition: FFTJetEFlowSmoother.cc:82
fftjetcms
Definition: AbsPileupCalculator.h:15
FFTJetEFlowSmoother::convolvedFlow
std::unique_ptr< fftjet::Grid2d< fftjetcms::Real > > convolvedFlow
Definition: FFTJetEFlowSmoother.cc:70
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
fftjetcms::FFTJetInterface::outputLabel
const std::string outputLabel
Definition: FFTJetInterface.h:76
HLT_FULL_cff.nEta
nEta
Definition: HLT_FULL_cff.py:6586
fftjetcms::fftjet_ScaleSet_parser
std::unique_ptr< std::vector< double > > fftjet_ScaleSet_parser(const edm::ParameterSet &ps)
Definition: FFTJetParameterParser.cc:350
fftjeteflowsmoother_cfi.convolverMaxBin
convolverMaxBin
Definition: fftjeteflowsmoother_cfi.py:21
fftjetcms::FFTJetInterface::getEventScale
double getEventScale() const
Definition: FFTJetInterface.cc:38
FFTJetEFlowSmoother
Definition: FFTJetEFlowSmoother.cc:51
FFTJetEFlowSmoother::engine
std::unique_ptr< MyFFTEngine > engine
Definition: FFTJetEFlowSmoother.cc:76
FFTJetEFlowSmoother::anotherEngine
std::unique_ptr< MyFFTEngine > anotherEngine
Definition: FFTJetEFlowSmoother.cc:77
FFTJetEFlowSmoother::buildKernelConvolver
void buildKernelConvolver(const edm::ParameterSet &)
Definition: FFTJetEFlowSmoother.cc:120
FFTJetEFlowSmoother::kernel2d
std::unique_ptr< fftjet::AbsFrequencyKernel > kernel2d
Definition: FFTJetEFlowSmoother.cc:80
FFTJetInterface.h
LEDCalibrationChannels.iphi
iphi
Definition: LEDCalibrationChannels.py:64
FFTJetEFlowSmoother::scalePower
double scalePower
Definition: FFTJetEFlowSmoother.cc:88
MakerMacros.h
h
DEFINE_FWK_MODULE
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
FFTJetEFlowSmoother::~FFTJetEFlowSmoother
~FFTJetEFlowSmoother() override
Definition: FFTJetEFlowSmoother.cc:187
fftjeteflowsmoother_cfi.kernelEtaScale
kernelEtaScale
Definition: fftjeteflowsmoother_cfi.py:11
fftjetcms::FFTJetInterface
Definition: FFTJetInterface.h:52
DQMScaleToClient_cfi.factor
factor
Definition: DQMScaleToClient_cfi.py:8
FFTJetEFlowSmoother::etConversionFactor
double etConversionFactor
Definition: FFTJetEFlowSmoother.cc:91
mitigatedMETSequence_cff.U
U
Definition: mitigatedMETSequence_cff.py:36
fftjetcms::Real
double Real
Definition: fftjetTypedefs.h:21
LEDCalibrationChannels.ieta
ieta
Definition: LEDCalibrationChannels.py:63
edm::ParameterSet
Definition: ParameterSet.h:47
FFTJetEFlowSmoother::beginJob
void beginJob() override
Definition: FFTJetEFlowSmoother.cc:247
FFTJetEFlowSmoother::convolver
std::unique_ptr< fftjet::AbsConvolverBase< Real > > convolver
Definition: FFTJetEFlowSmoother.cc:85
FFTJetEFlowSmoother::endJob
void endJob() override
Definition: FFTJetEFlowSmoother.cc:250
FFTJetEFlowSmoother::etaKernel
std::unique_ptr< fftjet::AbsFrequencyKernel1d > etaKernel
Definition: FFTJetEFlowSmoother.cc:81
FFTJetEFlowSmoother::produce
void produce(edm::Event &, const edm::EventSetup &) override
Definition: FFTJetEFlowSmoother.cc:190
HLT_FULL_cff.nPhi
nPhi
Definition: HLT_FULL_cff.py:6587
iEvent
int iEvent
Definition: GenABIO.cc:224
fftjetcms::FFTJetInterface::energyFlow
std::unique_ptr< fftjet::Grid2d< fftjetcms::Real > > energyFlow
Definition: FFTJetInterface.h:100
M_PI
#define M_PI
Definition: BXVectorInputProducer.cc:49
FFTJetEFlowSmoother::FFTJetEFlowSmoother
FFTJetEFlowSmoother()=delete
fftjetcms::fftjet_Grid2d_parser
std::unique_ptr< fftjet::Grid2d< Real > > fftjet_Grid2d_parser(const edm::ParameterSet &ps)
Definition: FFTJetParameterParser.cc:125
edm::EventSetup
Definition: EventSetup.h:58
fftjeteflowsmoother_cfi.convolverMinBin
convolverMinBin
Definition: fftjeteflowsmoother_cfi.py:20
fftjeteflowsmoother_cfi.fixEfficiency
fixEfficiency
Definition: fftjeteflowsmoother_cfi.py:15
fftjeteflowsmoother_cfi.kernelPhiScale
kernelPhiScale
Definition: fftjeteflowsmoother_cfi.py:12
eostools.move
def move(src, dest)
Definition: eostools.py:511
Frameworkfwd.h
fftjetcommon_cfi.scalePower
scalePower
Definition: fftjetcommon_cfi.py:204
Exception
Definition: hltDiff.cc:245
FFTJetParameterParser.h
edm::ParameterSet::getParameter
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
FFTJetEFlowSmoother::iniScales
std::unique_ptr< std::vector< double > > iniScales
Definition: FFTJetEFlowSmoother.cc:73
fftjetcms::FFTJetInterface::discretizeEnergyFlow
void discretizeEnergyFlow()
Definition: FFTJetInterface.cc:79
funct::pow
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:29
cms::Exception
Definition: Exception.h:70
fftjeteflowsmoother_cfi.etaDependentScaleFactors
etaDependentScaleFactors
Definition: fftjeteflowsmoother_cfi.py:42
View.h
ParameterSet.h
edm::Event
Definition: Event.h:73
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