Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-05 08:14:07

0001 // Class: ReadFisher
0002 // Automatically generated by MethodBase::MakeClass
0003 //
0004 
0005 /* configuration options =====================================================
0006 
0007 #GEN -*-*-*-*-*-*-*-*-*-*-*- general info -*-*-*-*-*-*-*-*-*-*-*-
0008 
0009 Method         : Fisher::Fisher
0010 TMVA Release   : 4.2.0         [262656]
0011 ROOT Release   : 5.34/38       [336422]
0012 Creator        : vassalli
0013 Date           : Wed Jan 23 17:51:47 2019
0014 Host           : Linux cvmfswrite02.sdcc.bnl.gov 3.10.0-693.11.6.el7.x86_64 #1 SMP Wed Jan 3 18:09:42 CST 2018 x86_64 x86_64 x86_64 GNU/Linux
0015 Dir            : /direct/phenix+u/vassalli/sphenix/single/Training
0016 Training events: 2407
0017 Analysis type  : [Classification]
0018 
0019 
0020 #OPT -*-*-*-*-*-*-*-*-*-*-*-*- options -*-*-*-*-*-*-*-*-*-*-*-*-
0021 
0022 # Set by User:
0023 # Default:
0024 V: "False" [Verbose output (short form of "VerbosityLevel" below - overrides the latter one)]
0025 VerbosityLevel: "Default" [Verbosity level]
0026 VarTransform: "None" [List of variable transformations performed before training, e.g., "D_Background,P_Signal,G,N_AllClasses" for: "Decorrelation, PCA-transformation, Gaussianisation, Normalisation, each for the given class of events ('AllClasses' denotes all events of all classes, if no class indication is given, 'All' is assumed)"]
0027 H: "False" [Print method-specific help message]
0028 CreateMVAPdfs: "False" [Create PDFs for classifier outputs (signal and background)]
0029 IgnoreNegWeightsInTraining: "False" [Events with negative weights are ignored in the training (but are included for testing and performance evaluation)]
0030 Method: "Fisher" [Discrimination method]
0031 ##
0032 
0033 
0034 #VAR -*-*-*-*-*-*-*-*-*-*-*-* variables *-*-*-*-*-*-*-*-*-*-*-*-
0035 
0036 NVar 10
0037 track_deta                    track_deta                    track_deta                    track_deta                                                      'F'    [1.05425715446e-06,1.83402311802]
0038 track_dlayer                  track_dlayer                  track_dlayer                  track_dlayer                                                    'I'    [0,14]
0039 track_layer                   track_layer                   track_layer                   track_layer                                                     'I'    [0,23]
0040 track_pT                      track_pT                      track_pT                      track_pT                                                        'F'    [0.209833949804,34.1584281921]
0041 approach_dist                 approach_dist                 approach_dist                 approach_dist                                                   'F'    [1.69032391568e-05,12.8133029938]
0042 vtx_radius                    vtx_radius                    vtx_radius                    vtx_radius                                                      'F'    [0.00339345191605,20.9999389648]
0043 vtxTrack_dist                 vtxTrack_dist                 vtxTrack_dist                 vtxTrack_dist                                                   'F'    [0.0258899498731,10.0588207245]
0044 photon_m                      photon_m                      photon_m                      photon_m                                                        'F'    [1.044480443,713.936157227]
0045 photon_pT                     photon_pT                     photon_pT                     photon_pT                                                       'F'    [0.061235960573,5008.76708984]
0046 cluster_prob                  cluster_prob                  cluster_prob                  cluster_prob                                                    'F'    [0,0.999874174595]
0047 NSpec 1
0048 vtx_chi2                      vtx_chi2                      vtx_chi2                      F                                                               'F'    [0,3.33078734987e-36]
0049 
0050 
0051 ============================================================================ */
0052 
0053 #include <vector>
0054 #include <cmath>
0055 #include <string>
0056 #include <iostream>
0057 
0058 #ifndef IClassifierReader__def
0059 #define IClassifierReader__def
0060 
0061 class IClassifierReader {
0062 
0063  public:
0064 
0065    // constructor
0066    IClassifierReader() : fStatusIsClean( true ) {}
0067    virtual ~IClassifierReader() {}
0068 
0069    // return classifier response
0070    virtual double GetMvaValue( const std::vector<double>& inputValues ) const = 0;
0071 
0072    // returns classifier status
0073    bool IsStatusClean() const { return fStatusIsClean; }
0074 
0075  protected:
0076 
0077    bool fStatusIsClean;
0078 };
0079 
0080 #endif
0081 
0082 class ReadFisher : public IClassifierReader {
0083 
0084  public:
0085 
0086    // constructor
0087    ReadFisher( std::vector<std::string>& theInputVars ) 
0088       : IClassifierReader(),
0089         fClassName( "ReadFisher" ),
0090         fNvars( 10 ),
0091         fIsNormalised( false )
0092    {      
0093       // the training input variables
0094       const char* inputVars[] = { "track_deta", "track_dlayer", "track_layer", "track_pT", "approach_dist", "vtx_radius", "vtxTrack_dist", "photon_m", "photon_pT", "cluster_prob" };
0095 
0096       // sanity checks
0097       if (theInputVars.size() <= 0) {
0098          std::cout << "Problem in class \"" << fClassName << "\": empty input vector" << std::endl;
0099          fStatusIsClean = false;
0100       }
0101 
0102       if (theInputVars.size() != fNvars) {
0103          std::cout << "Problem in class \"" << fClassName << "\": mismatch in number of input values: "
0104                    << theInputVars.size() << " != " << fNvars << std::endl;
0105          fStatusIsClean = false;
0106       }
0107 
0108       // validate input variables
0109       for (size_t ivar = 0; ivar < theInputVars.size(); ivar++) {
0110          if (theInputVars[ivar] != inputVars[ivar]) {
0111             std::cout << "Problem in class \"" << fClassName << "\": mismatch in input variable names" << std::endl
0112                       << " for variable [" << ivar << "]: " << theInputVars[ivar].c_str() << " != " << inputVars[ivar] << std::endl;
0113             fStatusIsClean = false;
0114          }
0115       }
0116 
0117       // initialize min and max vectors (for normalisation)
0118       fVmin[0] = 0;
0119       fVmax[0] = 0;
0120       fVmin[1] = 0;
0121       fVmax[1] = 0;
0122       fVmin[2] = 0;
0123       fVmax[2] = 0;
0124       fVmin[3] = 0;
0125       fVmax[3] = 0;
0126       fVmin[4] = 0;
0127       fVmax[4] = 0;
0128       fVmin[5] = 0;
0129       fVmax[5] = 0;
0130       fVmin[6] = 0;
0131       fVmax[6] = 0;
0132       fVmin[7] = 0;
0133       fVmax[7] = 0;
0134       fVmin[8] = 0;
0135       fVmax[8] = 0;
0136       fVmin[9] = 0;
0137       fVmax[9] = 0;
0138 
0139       // initialize input variable types
0140       fType[0] = 'F';
0141       fType[1] = 'I';
0142       fType[2] = 'I';
0143       fType[3] = 'F';
0144       fType[4] = 'F';
0145       fType[5] = 'F';
0146       fType[6] = 'F';
0147       fType[7] = 'F';
0148       fType[8] = 'F';
0149       fType[9] = 'F';
0150 
0151       // initialize constants
0152       Initialize();
0153 
0154    }
0155 
0156    // destructor
0157    virtual ~ReadFisher() {
0158       Clear(); // method-specific
0159    }
0160 
0161    // the classifier response
0162    // "inputValues" is a vector of input values in the same order as the 
0163    // variables given to the constructor
0164    double GetMvaValue( const std::vector<double>& inputValues ) const;
0165 
0166  private:
0167 
0168    // method-specific destructor
0169    void Clear();
0170 
0171    // common member variables
0172    const char* fClassName;
0173 
0174    const size_t fNvars;
0175    size_t GetNvar()           const { return fNvars; }
0176    char   GetType( int ivar ) const { return fType[ivar]; }
0177 
0178    // normalisation of input variables
0179    const bool fIsNormalised;
0180    bool IsNormalised() const { return fIsNormalised; }
0181    double fVmin[10];
0182    double fVmax[10];
0183    double NormVariable( double x, double xmin, double xmax ) const {
0184       // normalise to output range: [-1, 1]
0185       return 2*(x - xmin)/(xmax - xmin) - 1.0;
0186    }
0187 
0188    // type of input variable: 'F' or 'I'
0189    char   fType[10];
0190 
0191    // initialize internal variables
0192    void Initialize();
0193    double GetMvaValue__( const std::vector<double>& inputValues ) const;
0194 
0195    // private members (method specific)
0196    double              fFisher0;
0197    std::vector<double> fFisherCoefficients;
0198 };
0199 
0200 inline void ReadFisher::Initialize() 
0201 {
0202    fFisher0 = -0.0844739600992;
0203    fFisherCoefficients.push_back( -1.43051579213 );
0204    fFisherCoefficients.push_back( -0.269539620647 );
0205    fFisherCoefficients.push_back( 0.402262492209 );
0206    fFisherCoefficients.push_back( 0.114840085395 );
0207    fFisherCoefficients.push_back( -0.0512308893762 );
0208    fFisherCoefficients.push_back( 0.0867003372772 );
0209    fFisherCoefficients.push_back( -0.312953668051 );
0210    fFisherCoefficients.push_back( -0.00686129058321 );
0211    fFisherCoefficients.push_back( 0.000764347437541 );
0212    fFisherCoefficients.push_back( -0.614808771127 );
0213 
0214    // sanity check
0215    if (fFisherCoefficients.size() != fNvars) {
0216       std::cout << "Problem in class \"" << fClassName << "\"::Initialize: mismatch in number of input values"
0217                 << fFisherCoefficients.size() << " != " << fNvars << std::endl;
0218       fStatusIsClean = false;
0219    }         
0220 }
0221 
0222 inline double ReadFisher::GetMvaValue__( const std::vector<double>& inputValues ) const
0223 {
0224    double retval = fFisher0;
0225    for (size_t ivar = 0; ivar < fNvars; ivar++) {
0226       retval += fFisherCoefficients[ivar]*inputValues[ivar];
0227    }
0228 
0229    return retval;
0230 }
0231 
0232 // Clean up
0233 inline void ReadFisher::Clear() 
0234 {
0235    // clear coefficients
0236    fFisherCoefficients.clear(); 
0237 }
0238    inline double ReadFisher::GetMvaValue( const std::vector<double>& inputValues ) const
0239    {
0240       // classifier response value
0241       double retval = 0;
0242 
0243       // classifier response, sanity check first
0244       if (!IsStatusClean()) {
0245          std::cout << "Problem in class \"" << fClassName << "\": cannot return classifier response"
0246                    << " because status is dirty" << std::endl;
0247          retval = 0;
0248       }
0249       else {
0250          if (IsNormalised()) {
0251             // normalise variables
0252             std::vector<double> iV;
0253             iV.reserve(inputValues.size());
0254             int ivar = 0;
0255             for (std::vector<double>::const_iterator varIt = inputValues.begin();
0256                  varIt != inputValues.end(); varIt++, ivar++) {
0257                iV.push_back(NormVariable( *varIt, fVmin[ivar], fVmax[ivar] ));
0258             }
0259             retval = GetMvaValue__( iV );
0260          }
0261          else {
0262             retval = GetMvaValue__( inputValues );
0263          }
0264       }
0265 
0266       return retval;
0267    }