CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/src/RecoJets/FFTJetProducers/plugins/FFTJetProducer.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:    FFTJetProducers
00004 // Class:      FFTJetProducer
00005 // 
00021 //
00022 // Original Author:  Igor Volobouev
00023 //         Created:  Sun Jun 20 14:32:36 CDT 2010
00024 // $Id: FFTJetProducer.h,v 1.8 2011/10/25 00:23:52 igv Exp $
00025 //
00026 //
00027 
00028 #ifndef RecoJets_FFTJetProducers_FFTJetProducer_h
00029 #define RecoJets_FFTJetProducers_FFTJetProducer_h
00030 
00031 #include <memory>
00032 
00033 // FFTJet headers
00034 #include "fftjet/AbsRecombinationAlg.hh"
00035 #include "fftjet/AbsVectorRecombinationAlg.hh"
00036 #include "fftjet/SparseClusteringTree.hh"
00037 
00038 // framework include files
00039 #include "FWCore/Framework/interface/Frameworkfwd.h"
00040 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00041 #include "FWCore/Framework/interface/EDProducer.h"
00042 #include "FWCore/Framework/interface/Event.h"
00043 
00044 #include "DataFormats/Candidate/interface/CandidateFwd.h"
00045 
00046 // local FFTJet-related definitions
00047 #include "RecoJets/FFTJetAlgorithms/interface/fftjetTypedefs.h"
00048 #include "RecoJets/FFTJetAlgorithms/interface/AbsPileupCalculator.h"
00049 #include "RecoJets/FFTJetProducers/interface/FFTJetInterface.h"
00050 
00051 namespace fftjetcms {
00052     class DiscretizedEnergyFlow;
00053 }
00054 
00055 //
00056 // class declaration
00057 //
00058 class FFTJetProducer : public edm::EDProducer, 
00059                        public fftjetcms::FFTJetInterface
00060 {
00061 public:
00062     typedef fftjet::RecombinedJet<fftjetcms::VectorLike> RecoFFTJet;
00063     typedef fftjet::SparseClusteringTree<fftjet::Peak,long> SparseTree;
00064 
00065     // Masks for the status bits
00066     enum StatusBits
00067     {
00068         RESOLUTION = 0xff,
00069         CONSTITUENTS_RESUMMED = 0x100,
00070         PILEUP_CALCULATED = 0x200,
00071         PILEUP_SUBTRACTED_4VEC = 0x400,
00072         PILEUP_SUBTRACTED_PT = 0x800
00073     };
00074 
00075     enum Resolution
00076     {
00077         FIXED = 0,
00078         MAXIMALLY_STABLE,
00079         GLOBALLY_ADAPTIVE,
00080         LOCALLY_ADAPTIVE
00081     };
00082 
00083     explicit FFTJetProducer(const edm::ParameterSet&);
00084     virtual ~FFTJetProducer();
00085 
00086     // Parser for the resolution enum
00087     static Resolution parse_resolution(const std::string& name);
00088 
00089 protected:
00090     // Functions which should be overriden from the base
00091     virtual void beginJob();
00092     virtual void produce(edm::Event&, const edm::EventSetup&);
00093     virtual void endJob();
00094 
00095     // The following functions can be overriden by derived classes 
00096     // in order to adjust jet reconstruction algorithm behavior.
00097 
00098     // Override the following method in order to implement
00099     // your own precluster selection strategy
00100     virtual void selectPreclusters(
00101         const SparseTree& tree,
00102         const fftjet::Functor1<bool,fftjet::Peak>& peakSelector,
00103         std::vector<fftjet::Peak>* preclusters);
00104 
00105     // Override the following method (which by default does not do
00106     // anything) in order to implement your own process-dependent
00107     // assignment of membership functions to preclusters. This method
00108     // will be called once per event, just before the main algorithm.
00109     virtual void assignMembershipFunctions(
00110         std::vector<fftjet::Peak>* preclusters);
00111 
00112     // Parser for the peak selector
00113     virtual std::auto_ptr<fftjet::Functor1<bool,fftjet::Peak> >
00114     parse_peakSelector(const edm::ParameterSet&);
00115 
00116     // Parser for the default jet membership function
00117     virtual std::auto_ptr<fftjet::ScaleSpaceKernel>
00118     parse_jetMembershipFunction(const edm::ParameterSet&);
00119 
00120     // Parser for the background membership function
00121     virtual std::auto_ptr<fftjetcms::AbsBgFunctor>
00122     parse_bgMembershipFunction(const edm::ParameterSet&);
00123 
00124     // Calculator for the recombination scale
00125     virtual std::auto_ptr<fftjet::Functor1<double,fftjet::Peak> >
00126     parse_recoScaleCalcPeak(const edm::ParameterSet&);
00127 
00128     // Calculator for the recombination scale ratio
00129     virtual std::auto_ptr<fftjet::Functor1<double,fftjet::Peak> >
00130     parse_recoScaleRatioCalcPeak(const edm::ParameterSet&);
00131 
00132     // Calculator for the membership function factor
00133     virtual std::auto_ptr<fftjet::Functor1<double,fftjet::Peak> >
00134     parse_memberFactorCalcPeak(const edm::ParameterSet&);
00135 
00136     // Similar calculators for the iterative algorithm
00137     virtual std::auto_ptr<fftjet::Functor1<double,RecoFFTJet> >
00138     parse_recoScaleCalcJet(const edm::ParameterSet&);
00139 
00140     virtual std::auto_ptr<fftjet::Functor1<double,RecoFFTJet> >
00141     parse_recoScaleRatioCalcJet(const edm::ParameterSet&);
00142 
00143     virtual std::auto_ptr<fftjet::Functor1<double,RecoFFTJet> >
00144     parse_memberFactorCalcJet(const edm::ParameterSet&);
00145 
00146     // Calculator of the distance between jets which is used to make
00147     // the decision about convergence of the iterative algorithm
00148     virtual std::auto_ptr<fftjet::Functor2<double,RecoFFTJet,RecoFFTJet> >
00149     parse_jetDistanceCalc(const edm::ParameterSet&);
00150 
00151     // Pile-up density calculator
00152     virtual std::auto_ptr<fftjetcms::AbsPileupCalculator>
00153     parse_pileupDensityCalc(const edm::ParameterSet& ps);
00154 
00155     // The following function performs most of the precluster selection
00156     // work in this module. You might want to reuse it if only a slight
00157     // modification of the "selectPreclusters" method is desired.
00158     void selectTreeNodes(const SparseTree& tree,
00159                          const fftjet::Functor1<bool,fftjet::Peak>& peakSelect,
00160                          std::vector<SparseTree::NodeId>* nodes);
00161 private:
00162     typedef fftjet::AbsVectorRecombinationAlg<
00163         fftjetcms::VectorLike,fftjetcms::BgData> RecoAlg;
00164     typedef fftjet::AbsRecombinationAlg<
00165         fftjetcms::Real,fftjetcms::VectorLike,fftjetcms::BgData> GridAlg;
00166 
00167     // Explicitly disable other ways to construct this object
00168     FFTJetProducer();
00169     FFTJetProducer(const FFTJetProducer&);
00170     FFTJetProducer& operator=(const FFTJetProducer&);
00171 
00172     // Useful local utilities
00173     template<class Real>
00174     void loadSparseTreeData(const edm::Event&);
00175 
00176     void removeFakePreclusters();
00177 
00178     // The following methods do most of the work.
00179     // The following function tells us if the grid was rebuilt.
00180     static bool loadEnergyFlow(
00181         const edm::Event& iEvent, const edm::InputTag& label,
00182         std::auto_ptr<fftjet::Grid2d<fftjetcms::Real> >& flow);
00183     void buildGridAlg();
00184     void prepareRecombinationScales();
00185     bool checkConvergence(const std::vector<RecoFFTJet>& previousIterResult,
00186                           std::vector<RecoFFTJet>& thisIterResult);
00187     void determineGriddedConstituents();
00188     void determineVectorConstituents();
00189     void saveResults(edm::Event& iEvent, const edm::EventSetup&,
00190                      unsigned nPreclustersFound);
00191 
00192     template <typename Jet>
00193     void writeJets(edm::Event& iEvent, const edm::EventSetup&);
00194 
00195     template <typename Jet>
00196     void makeProduces(const std::string& alias, const std::string& tag);
00197 
00198     // The following function scans the pile-up density
00199     // and fills the pile-up grid. Can be overriden if
00200     // necessary.
00201     virtual void determinePileupDensity(
00202         const edm::Event& iEvent, const edm::InputTag& label,
00203         std::auto_ptr<fftjet::Grid2d<fftjetcms::Real> >& density);
00204 
00205     // The following function builds the pile-up estimate
00206     // for each jet
00207     void determinePileup();
00208 
00209     // The following function returns the number of iterations
00210     // performed. If this number equals to or less than "maxIterations"
00211     // then the iterations have converged. If the number larger than
00212     // "maxIterations" then the iterations failed to converge (note,
00213     // however, that only "maxIterations" iterations would still be
00214     // performed).
00215     unsigned iterateJetReconstruction();
00216 
00217     // A function to set jet status bits
00218     static void setJetStatusBit(RecoFFTJet* jet, int mask, bool value);
00219 
00220     //
00221     // ----------member data ---------------------------
00222     //
00223 
00224     // Local copy of the module configuration 
00225     const edm::ParameterSet myConfiguration;
00226 
00227     // Label for the tree produced by FFTJetPatRecoProducer
00228     const edm::InputTag treeLabel;
00229 
00230     // Are we going to use energy flow discretization grid as input
00231     // to jet reconstruction?
00232     const bool useGriddedAlgorithm;
00233 
00234     // Are we going to rebuild the energy flow discretization grid
00235     // or to reuse the grid made by FFTJetPatRecoProducer?
00236     const bool reuseExistingGrid;
00237 
00238     // Are we iterating?
00239     const unsigned maxIterations;
00240 
00241     // Parameters which affect iteration convergence
00242     const unsigned nJetsRequiredToConverge;
00243     const double convergenceDistance;
00244 
00245     // Are we building assignments of collection members to jets?
00246     const bool assignConstituents;
00247 
00248     // Are we resumming the constituents to determine jet 4-vectors?
00249     // This might make sense if FFTJet is used in the crisp, gridded
00250     // mode to determine jet areas, and vector recombination is desired.
00251     const bool resumConstituents;
00252 
00253     // Are we going to subtract the pile-up? Note that
00254     // pile-up subtraction does not modify eta and phi moments.
00255     const bool calculatePileup;
00256     const bool subtractPileup;
00257     const bool subtractPileupAs4Vec;
00258 
00259     // Label for the pile-up energy flow. Must be specified
00260     // if the pile-up is subtracted.
00261     const edm::InputTag pileupLabel;
00262 
00263     // Scale for the peak selection (if the scale is fixed)
00264     const double fixedScale;
00265 
00266     // Minimum and maximum scale for searching stable configurations
00267     const double minStableScale;
00268     const double maxStableScale;
00269 
00270     // Stability "alpha"
00271     const double stabilityAlpha;
00272 
00273     // Not sure at this point how to treat noise... For now, this is
00274     // just a single configurable number...
00275     const double noiseLevel;
00276 
00277     // Number of clusters requested (if the scale is adaptive)
00278     const unsigned nClustersRequested;
00279 
00280     // Maximum eta for the grid-based algorithm
00281     const double gridScanMaxEta;
00282 
00283     // Parameters related to the recombination algorithm
00284     const std::string recombinationAlgorithm;
00285     const bool isCrisp;
00286     const double unlikelyBgWeight;
00287     const double recombinationDataCutoff;
00288 
00289     // Resolution. The corresponding parameter value
00290     // should be one of "fixed", "maximallyStable",
00291     // "globallyAdaptive", or "locallyAdaptive".
00292     Resolution resolution;
00293 
00294     // Scales used
00295     std::auto_ptr<std::vector<double> > iniScales;
00296 
00297     // The sparse clustering tree
00298     SparseTree sparseTree;
00299 
00300     // Peak selector for the peaks already in the tree
00301     std::auto_ptr<fftjet::Functor1<bool,fftjet::Peak> > peakSelector;
00302 
00303     // Recombination algorithms and related quantities
00304     std::auto_ptr<RecoAlg> recoAlg;
00305     std::auto_ptr<GridAlg> gridAlg;
00306     std::auto_ptr<fftjet::ScaleSpaceKernel> jetMembershipFunction;
00307     std::auto_ptr<fftjetcms::AbsBgFunctor> bgMembershipFunction;
00308 
00309     // Calculator for the recombination scale
00310     std::auto_ptr<fftjet::Functor1<double,fftjet::Peak> > recoScaleCalcPeak;
00311 
00312     // Calculator for the recombination scale ratio
00313     std::auto_ptr<fftjet::Functor1<double,fftjet::Peak> >
00314     recoScaleRatioCalcPeak;
00315 
00316     // Calculator for the membership function factor
00317     std::auto_ptr<fftjet::Functor1<double,fftjet::Peak> > memberFactorCalcPeak;
00318 
00319     // Similar calculators for the iterative algorithm
00320     std::auto_ptr<fftjet::Functor1<double,RecoFFTJet> > recoScaleCalcJet;
00321     std::auto_ptr<fftjet::Functor1<double,RecoFFTJet> > recoScaleRatioCalcJet;
00322     std::auto_ptr<fftjet::Functor1<double,RecoFFTJet> > memberFactorCalcJet;
00323 
00324     // Calculator for the jet distance used to estimate convergence
00325     // of the iterative algorithm
00326     std::auto_ptr<fftjet::Functor2<double,RecoFFTJet,RecoFFTJet> >
00327     jetDistanceCalc;
00328 
00329     // Vector of selected tree nodes
00330     std::vector<SparseTree::NodeId> nodes;
00331 
00332     // Vector of selected preclusters
00333     std::vector<fftjet::Peak> preclusters;
00334 
00335     // Vector of reconstructed jets (we will refill it in every event)
00336     std::vector<RecoFFTJet> recoJets;
00337 
00338     // Cluster occupancy calculated as a function of level number
00339     std::vector<unsigned> occupancy;
00340 
00341     // The thresholds obtained by the LOCALLY_ADAPTIVE method
00342     std::vector<double> thresholds;
00343 
00344     // Minimum, maximum and used level calculated by some algorithms
00345     unsigned minLevel, maxLevel, usedLevel;
00346 
00347     // Unclustered/unused energy produced during recombination
00348     fftjetcms::VectorLike unclustered;
00349     double unused;
00350 
00351     // Quantities defined below are used in the iterative mode only
00352     std::vector<fftjet::Peak> iterPreclusters;
00353     std::vector<RecoFFTJet> iterJets;
00354     unsigned iterationsPerformed;
00355 
00356     // Vectors of constituents
00357     std::vector<std::vector<reco::CandidatePtr> > constituents;
00358 
00359     // Vector of pile-up. We will subtract it from the
00360     // 4-vectors of reconstructed jets.
00361     std::vector<fftjetcms::VectorLike> pileup;
00362 
00363     // The pile-up transverse energy density discretization grid.
00364     // Note that this is _density_, not energy. To get energy, 
00365     // multiply by cell area.
00366     std::auto_ptr<fftjet::Grid2d<fftjetcms::Real> > pileupEnergyFlow;
00367 
00368     // The functor that calculates the pile-up density
00369     std::auto_ptr<fftjetcms::AbsPileupCalculator> pileupDensityCalc;
00370 
00371     // Memory buffers related to pile-up subtraction
00372     std::vector<fftjet::AbsKernel2d*> memFcns2dVec;
00373     std::vector<double> doubleBuf;
00374     std::vector<unsigned> cellCountsVec;
00375 };
00376 
00377 #endif // RecoJets_FFTJetProducers_FFTJetProducer_h