Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:11:09

0001 // @(#)root/tmva $Id$
0002 // Author: Andreas Hoecker, Joerg Stelzer, Fredrik Tegenfeldt, Helge Voss
0003 
0004 /**********************************************************************************
0005  * Project: TMVA - a Root-integrated toolkit for multivariate data analysis       *
0006  * Package: TMVA                                                                  *
0007  * Class  : Rule                                                                  *
0008  *                                                                                *
0009  * Description:                                                                   *
0010  *      A class describing a 'rule cut'                                           *
0011  *                                                                                *
0012  *                                                                                *
0013  * Authors (alphabetical):                                                        *
0014  *      Fredrik Tegenfeldt <Fredrik.Tegenfeldt@cern.ch> - Iowa State U., USA      *
0015  *                                                                                *
0016  * Copyright (c) 2005:                                                            *
0017  *      CERN, Switzerland                                                         *
0018  *      Iowa State U.                                                             *
0019  *                                                                                *
0020  * Redistribution and use in source and binary forms, with or without             *
0021  * modification, are permitted according to the terms listed in LICENSE           *
0022  * (see tmva/doc/LICENSE)                                          *
0023  **********************************************************************************/
0024 #ifndef ROOT_TMVA_RuleCut
0025 #define ROOT_TMVA_RuleCut
0026 
0027 #include "TMVA/Event.h"
0028 
0029 #include <vector>
0030 
0031 namespace TMVA {
0032 
0033    class Node;
0034    class MsgLogger;
0035 
0036    class RuleCut {
0037 
0038    public:
0039 
0040       // main constructor
0041       RuleCut( const std::vector< const TMVA::Node * > & nodes );
0042 
0043       // copy constructor
0044       RuleCut( const RuleCut & other ) : fLogger(nullptr) { Copy( other ); }
0045 
0046       // empty constructor
0047       RuleCut();
0048 
0049       // destructor
0050       virtual ~RuleCut();
0051 
0052       // evaluate an event
0053       inline Bool_t EvalEvent( const Event &eve );
0054 
0055       // get cut range for a given selector
0056       Bool_t GetCutRange(Int_t sel,Double_t &rmin, Double_t &rmax, Bool_t &dormin, Bool_t &dormax) const;
0057 
0058       // number of cuts
0059       UInt_t GetNcuts() const;
0060 
0061       // set members
0062       inline void SetNvars( UInt_t nc );
0063       void SetNeve( Double_t n )                   { fCutNeve     = n;   }
0064       void SetPurity( Double_t ssb )               { fPurity      = ssb; }
0065       void SetSelector( Int_t i, UInt_t s )        { fSelector[i] = s; }
0066       void SetCutMin( Int_t i, Double_t v )        { fCutMin[i]   = v; }
0067       void SetCutMax( Int_t i, Double_t v )        { fCutMax[i]   = v; }
0068       void SetCutDoMin( Int_t i, Bool_t v )        { fCutDoMin[i] = v; }
0069       void SetCutDoMax( Int_t i, Bool_t v )        { fCutDoMax[i] = v; }
0070 
0071       // accessors
0072       UInt_t   GetNvars()              const { return fSelector.size(); }
0073       UInt_t   GetSelector(Int_t is)   const { return fSelector[is]; }
0074       Double_t GetCutMin(Int_t is)     const { return fCutMin[is]; }
0075       Double_t GetCutMax(Int_t is)     const { return fCutMax[is]; }
0076       Char_t   GetCutDoMin(Int_t is)   const { return fCutDoMin[is]; }
0077       Char_t   GetCutDoMax(Int_t is)   const { return fCutDoMax[is]; }
0078       Double_t GetCutNeve()            const { return fCutNeve; }
0079       Double_t GetPurity()             const { return fPurity; }
0080 
0081    private:
0082       // copy
0083       inline void Copy( const RuleCut & other);
0084 
0085       // make the cuts from the array of nodes
0086       void MakeCuts( const std::vector< const TMVA::Node * > & nodes );
0087 
0088       std::vector<UInt_t>   fSelector; // array of selectors (expressions)
0089       std::vector<Double_t> fCutMin;   // array of lower limits
0090       std::vector<Double_t> fCutMax;   // array of upper limits
0091       std::vector<Char_t>   fCutDoMin; // array of usage flags for lower limits <--- stores boolean
0092       std::vector<Char_t>   fCutDoMax; // array of usage flags for upper limits <--- stores boolean
0093       Double_t              fCutNeve;  // N(events) after cut (possibly weighted)
0094       Double_t              fPurity;  // S/(S+B) on training data
0095 
0096 
0097       mutable MsgLogger*    fLogger;   //! message logger
0098       MsgLogger& Log() const { return *fLogger; }
0099    };
0100 }
0101 
0102 //_______________________________________________________________________
0103 inline void TMVA::RuleCut::Copy( const TMVA::RuleCut & other )
0104 {
0105    // copy from another
0106    if (&other != this) {
0107       for (UInt_t ns=0; ns<other.GetNvars(); ns++) {
0108          fSelector.push_back( other.GetSelector(ns) );
0109          fCutMin.push_back( other.GetCutMin(ns) );
0110          fCutMax.push_back( other.GetCutMax(ns) );
0111          fCutDoMin.push_back( other.GetCutDoMin(ns) );
0112          fCutDoMax.push_back( other.GetCutDoMax(ns) );
0113       }
0114       fCutNeve = other.GetCutNeve();
0115       fPurity = other.GetPurity();
0116    }
0117 }
0118 
0119 //_______________________________________________________________________
0120 inline Bool_t TMVA::RuleCut::EvalEvent( const Event &eve )
0121 {
0122    // evaluate event using the cut
0123 
0124    // Loop over all cuts
0125    Int_t    sel;
0126    Double_t val;
0127    Bool_t done=kFALSE;
0128    Bool_t minOK, cutOK=kFALSE;
0129    UInt_t nc=0;
0130    while (!done) {
0131       sel = fSelector[nc];
0132       val = eve.GetValue(sel);
0133       minOK = (fCutDoMin[nc] ? (val>fCutMin[nc]):kTRUE); // min cut ok
0134       cutOK = (minOK ? ((fCutDoMax[nc] ? (val<fCutMax[nc]):kTRUE)) : kFALSE); // cut ok
0135       nc++;
0136       done = ((!cutOK) || (nc==fSelector.size())); // done if
0137    }
0138    //   return ( cutOK ? 1.0: 0.0 );
0139    return cutOK;
0140 }
0141 
0142 //_______________________________________________________________________
0143 inline void TMVA::RuleCut::SetNvars( UInt_t nc )
0144 {
0145    // set the number of cuts
0146    fSelector.clear();
0147    fCutMin.clear();
0148    fCutMax.clear();
0149    fCutDoMin.clear();
0150    fCutDoMax.clear();
0151    //
0152    fSelector.resize(nc);
0153    fCutMin.resize(nc);
0154    fCutMax.resize(nc);
0155    fCutDoMin.resize(nc);
0156    fCutDoMax.resize(nc);
0157 }
0158 
0159 #endif