Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:25:06

0001 #ifndef RecoEgamma_ElectronIdentification_ElectronMVAEstimatorRun2_H
0002 #define RecoEgamma_ElectronIdentification_ElectronMVAEstimatorRun2_H
0003 
0004 #include "CommonTools/Utils/interface/StringCutObjectSelector.h"
0005 #include "CommonTools/Utils/interface/ThreadSafeFunctor.h"
0006 #include "CondFormats/GBRForest/interface/GBRForest.h"
0007 #include "RecoEgamma/EgammaTools/interface/AnyMVAEstimatorRun2Base.h"
0008 #include "RecoEgamma/EgammaTools/interface/MVAVariableManager.h"
0009 
0010 // Note on Python/FWLite support:
0011 //
0012 // The ElectronMVAEstimatorRun2 is not only used in the full CMSSW framework
0013 // via the AnyMVAEstimatorRun2Factory, but it is intended to also be used
0014 // standalone in Python/FWLite. However, we want to avoid building dictionaries
0015 // for the ElectronMVAEstimatorRun2 in this ElectronIdentification package,
0016 // becase algorithms and data formats should not mix in CMSSW.
0017 // That's why it has to be possible to create the dictionaries on the fly, for
0018 // example by running this line in a Python script:
0019 //
0020 // ```Python
0021 // ROOT.gInterpreter.Declare('#include "RecoEgamma/ElectronIdentification/interface/ElectronMVAEstimatorRun2.h"')
0022 // ```
0023 //
0024 // To speed up the dictionary generation and avoid errors caused by conflicting
0025 // C++ modules, we try to forwar declare as much as possible in
0026 // ElectronMVAEstimatorRun2.h and AnyMVAEstimatorRun2Base.h.
0027 
0028 namespace reco {
0029   class GsfElectron;
0030 }
0031 
0032 class ElectronMVAEstimatorRun2 : public AnyMVAEstimatorRun2Base {
0033 public:
0034   // Constructor
0035   ElectronMVAEstimatorRun2(const edm::ParameterSet& conf);
0036   // For use with FWLite/Python
0037   ElectronMVAEstimatorRun2(const std::string& mvaTag,
0038                            const std::string& mvaName,
0039                            int nCategories,
0040                            const std::string& variableDefinition,
0041                            const std::vector<std::string>& categoryCutStrings,
0042                            const std::vector<std::string>& weightFileNames,
0043                            bool debug = false);
0044 
0045   // Calculation of the MVA value
0046   float mvaValue(const reco::Candidate* candidate,
0047                  std::vector<float> const& auxVariables,
0048                  int& iCategory) const override;
0049 
0050   // for FWLite just passing rho
0051   float mvaValue(const reco::Candidate* candidate, float rho, int& iCategory) const {
0052     return mvaValue(candidate, std::vector<float>{rho}, iCategory);
0053   }
0054 
0055   int findCategory(const reco::Candidate* candidate) const override;
0056 
0057 private:
0058   void init(const std::vector<std::string>& weightFileNames);
0059 
0060   int findCategory(reco::GsfElectron const& electron) const;
0061 
0062   std::vector<ThreadSafeFunctor<StringCutObjectSelector<reco::GsfElectron>>> categoryFunctions_;
0063   std::vector<int> nVariables_;
0064 
0065   // Data members
0066   std::vector<std::unique_ptr<const GBRForest>> gbrForests_;
0067 
0068   // There might be different variables for each category, so the variables
0069   // names vector is itself a vector of length nCategories
0070   std::vector<std::vector<int>> variables_;
0071 
0072   MVAVariableManager<reco::GsfElectron> mvaVarMngr_;
0073 };
0074 
0075 #endif