Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-11-29 01:32:36

0001 #ifndef CondFormats_PhysicsToolsObjects_MVAComputer_h
0002 #define CondFormats_PhysicsToolsObjects_MVAComputer_h
0003 // -*- C++ -*-
0004 //
0005 // Package:     PhysicsToolsObjects
0006 // Class  :     MVAComputer
0007 //
0008 
0009 //
0010 // Author:  Christophe Saout <christophe.saout@cern.ch>
0011 // Created:     Sat Apr 24 15:18 CEST 2007
0012 // $Id: MVAComputer.h,v 1.15 2010/01/26 19:40:03 saout Exp $
0013 //
0014 
0015 #include "CondFormats/Serialization/interface/Serializable.h"
0016 
0017 #include <memory>
0018 #include <string>
0019 #include <vector>
0020 #include <map>
0021 
0022 #include "CondFormats/PhysicsToolsObjects/interface/Histogram.h"
0023 
0024 namespace PhysicsTools {
0025   namespace Calibration {
0026 
0027     // helper classes
0028 
0029     class BitSet {
0030     public:
0031       std::vector<unsigned char> store;
0032       unsigned int bitsInLast;
0033 
0034       COND_SERIALIZABLE;
0035     };
0036 
0037     class Matrix {
0038     public:
0039       std::vector<double> elements;
0040       unsigned int rows;
0041       unsigned int columns;
0042 
0043       COND_SERIALIZABLE;
0044     };
0045 
0046     // configuration base classes
0047 
0048     class VarProcessor {
0049     public:
0050       BitSet inputVars;
0051 
0052       virtual ~VarProcessor() {}
0053       virtual std::string getInstanceName() const;
0054       virtual std::unique_ptr<VarProcessor> clone() const;
0055 
0056       COND_SERIALIZABLE;
0057     };
0058 
0059     class Variable {
0060     public:
0061       inline Variable() {}
0062       inline Variable(const std::string &name) : name(name) {}
0063       inline ~Variable() {}
0064 
0065       std::string name;
0066 
0067       COND_SERIALIZABLE;
0068     };
0069 
0070     // variable processors
0071 
0072     class ProcOptional : public VarProcessor {
0073     public:
0074       std::unique_ptr<VarProcessor> clone() const override;
0075       std::vector<double> neutralPos;
0076 
0077       COND_SERIALIZABLE;
0078     };
0079 
0080     class ProcCount : public VarProcessor {
0081     public:
0082       std::unique_ptr<VarProcessor> clone() const override;
0083       COND_SERIALIZABLE;
0084     };
0085 
0086     class ProcClassed : public VarProcessor {
0087     public:
0088       std::unique_ptr<VarProcessor> clone() const override;
0089       unsigned int nClasses;
0090 
0091       COND_SERIALIZABLE;
0092     };
0093 
0094     class ProcSplitter : public VarProcessor {
0095     public:
0096       std::unique_ptr<VarProcessor> clone() const override;
0097       unsigned int nFirst;
0098 
0099       COND_SERIALIZABLE;
0100     };
0101 
0102     class ProcForeach : public VarProcessor {
0103     public:
0104       std::unique_ptr<VarProcessor> clone() const override;
0105       unsigned int nProcs;
0106 
0107       COND_SERIALIZABLE;
0108     };
0109 
0110     class ProcSort : public VarProcessor {
0111     public:
0112       std::unique_ptr<VarProcessor> clone() const override;
0113       unsigned int sortByIndex;
0114       bool descending;
0115 
0116       COND_SERIALIZABLE;
0117     };
0118 
0119     class ProcCategory : public VarProcessor {
0120     public:
0121       std::unique_ptr<VarProcessor> clone() const override;
0122       typedef std::vector<double> BinLimits;
0123 
0124       std::vector<BinLimits> variableBinLimits;
0125       std::vector<int> categoryMapping;
0126 
0127       COND_SERIALIZABLE;
0128     };
0129 
0130     class ProcNormalize : public VarProcessor {
0131     public:
0132       std::unique_ptr<VarProcessor> clone() const override;
0133       std::vector<HistogramF> distr;
0134       int categoryIdx;
0135 
0136       COND_SERIALIZABLE;
0137     };
0138 
0139     class ProcLikelihood : public VarProcessor {
0140     public:
0141       std::unique_ptr<VarProcessor> clone() const override;
0142       class SigBkg {
0143       public:
0144         HistogramF background;
0145         HistogramF signal;
0146         bool useSplines;
0147 
0148         COND_SERIALIZABLE;
0149       };
0150 
0151       std::vector<SigBkg> pdfs;
0152       std::vector<double> bias;
0153       int categoryIdx;
0154       bool logOutput;
0155       bool individual;
0156       bool neverUndefined;
0157       bool keepEmpty;
0158 
0159       COND_SERIALIZABLE;
0160     };
0161 
0162     class ProcLinear : public VarProcessor {
0163     public:
0164       std::unique_ptr<VarProcessor> clone() const override;
0165       std::vector<double> coeffs;
0166       double offset;
0167 
0168       COND_SERIALIZABLE;
0169     };
0170 
0171     class ProcMultiply : public VarProcessor {
0172     public:
0173       std::unique_ptr<VarProcessor> clone() const override;
0174       typedef std::vector<unsigned int> Config;
0175 
0176       unsigned int in;
0177       std::vector<Config> out;
0178 
0179       COND_SERIALIZABLE;
0180     };
0181 
0182     class ProcMatrix : public VarProcessor {
0183     public:
0184       std::unique_ptr<VarProcessor> clone() const override;
0185       Matrix matrix;
0186 
0187       COND_SERIALIZABLE;
0188     };
0189 
0190     class ProcExternal : public VarProcessor {
0191     public:
0192       std::unique_ptr<VarProcessor> clone() const override;
0193       std::string getInstanceName() const override;
0194 
0195       std::string method;
0196       std::vector<unsigned char> store;
0197 
0198       COND_SERIALIZABLE;
0199     };
0200 
0201     class ProcMLP : public VarProcessor {
0202     public:
0203       std::unique_ptr<VarProcessor> clone() const override;
0204       typedef std::pair<double, std::vector<double> > Neuron;
0205       typedef std::pair<std::vector<Neuron>, bool> Layer;
0206 
0207       std::vector<Layer> layers;
0208 
0209       COND_SERIALIZABLE;
0210     };
0211 
0212     // the discriminator computer
0213 
0214     class MVAComputer {
0215     public:
0216       MVAComputer();
0217       MVAComputer(const MVAComputer &orig);
0218       virtual ~MVAComputer();
0219 
0220       MVAComputer &operator=(const MVAComputer &orig);
0221 
0222       virtual std::vector<VarProcessor *> getProcessors();
0223       virtual std::vector<const VarProcessor *> getProcessors() const;
0224       void addProcessor(const VarProcessor *proc);
0225 
0226       // cacheId stuff to detect changes
0227       typedef unsigned int CacheId;
0228       inline CacheId getCacheId() const { return cacheId; }
0229       inline bool changed(CacheId old) const { return old != cacheId; }
0230 
0231       std::vector<Variable> inputSet;
0232       unsigned int output;
0233 
0234     private:
0235       std::vector<VarProcessor *> processors;
0236 
0237       CacheId cacheId COND_TRANSIENT;  // transient
0238 
0239       COND_SERIALIZABLE;
0240     };
0241 
0242     // a collection of computers identified by name
0243 
0244     class MVAComputerContainer {
0245     public:
0246       typedef std::pair<std::string, MVAComputer> Entry;
0247 
0248       MVAComputerContainer();
0249       virtual ~MVAComputerContainer() {}
0250 
0251       MVAComputer &add(const std::string &label);
0252       virtual const MVAComputer &find(const std::string &label) const;
0253       virtual bool contains(const std::string &label) const;
0254 
0255       // cacheId stuff to detect changes
0256       typedef unsigned int CacheId;
0257       inline CacheId getCacheId() const { return cacheId; }
0258       inline bool changed(CacheId old) const { return old != cacheId; }
0259 
0260     private:
0261       std::vector<Entry> entries;
0262 
0263       CacheId cacheId COND_TRANSIENT;  // transient
0264 
0265       COND_SERIALIZABLE;
0266     };
0267 
0268   }  // namespace Calibration
0269 }  // namespace PhysicsTools
0270 
0271 #endif  // CondFormats_PhysicsToolsObjects_MVAComputer_h