Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:02:26

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       // help that poor ROOT to copy bitsets... (workaround)
0032       BitSet &operator=(const BitSet &other) {
0033         store = other.store;
0034         bitsInLast = other.bitsInLast;
0035         return *this;
0036       }
0037       BitSet(const BitSet &other) = default;
0038       BitSet() = default;
0039       ~BitSet() = default;
0040 
0041       std::vector<unsigned char> store;
0042       unsigned int bitsInLast;
0043 
0044       COND_SERIALIZABLE;
0045     };
0046 
0047     class Matrix {
0048     public:
0049       std::vector<double> elements;
0050       unsigned int rows;
0051       unsigned int columns;
0052 
0053       COND_SERIALIZABLE;
0054     };
0055 
0056     // configuration base classes
0057 
0058     class VarProcessor {
0059     public:
0060       BitSet inputVars;
0061 
0062       virtual ~VarProcessor() {}
0063       virtual std::string getInstanceName() const;
0064       virtual std::unique_ptr<VarProcessor> clone() const;
0065 
0066       COND_SERIALIZABLE;
0067     };
0068 
0069     class Variable {
0070     public:
0071       inline Variable() {}
0072       inline Variable(const std::string &name) : name(name) {}
0073       inline ~Variable() {}
0074       Variable &operator=(const Variable &other) = default;
0075 
0076       std::string name;
0077 
0078       COND_SERIALIZABLE;
0079     };
0080 
0081     // variable processors
0082 
0083     class ProcOptional : public VarProcessor {
0084     public:
0085       std::unique_ptr<VarProcessor> clone() const override;
0086       std::vector<double> neutralPos;
0087 
0088       COND_SERIALIZABLE;
0089     };
0090 
0091     class ProcCount : public VarProcessor {
0092     public:
0093       std::unique_ptr<VarProcessor> clone() const override;
0094       COND_SERIALIZABLE;
0095     };
0096 
0097     class ProcClassed : public VarProcessor {
0098     public:
0099       std::unique_ptr<VarProcessor> clone() const override;
0100       unsigned int nClasses;
0101 
0102       COND_SERIALIZABLE;
0103     };
0104 
0105     class ProcSplitter : public VarProcessor {
0106     public:
0107       std::unique_ptr<VarProcessor> clone() const override;
0108       unsigned int nFirst;
0109 
0110       COND_SERIALIZABLE;
0111     };
0112 
0113     class ProcForeach : public VarProcessor {
0114     public:
0115       std::unique_ptr<VarProcessor> clone() const override;
0116       unsigned int nProcs;
0117 
0118       COND_SERIALIZABLE;
0119     };
0120 
0121     class ProcSort : public VarProcessor {
0122     public:
0123       std::unique_ptr<VarProcessor> clone() const override;
0124       unsigned int sortByIndex;
0125       bool descending;
0126 
0127       COND_SERIALIZABLE;
0128     };
0129 
0130     class ProcCategory : public VarProcessor {
0131     public:
0132       std::unique_ptr<VarProcessor> clone() const override;
0133       typedef std::vector<double> BinLimits;
0134 
0135       std::vector<BinLimits> variableBinLimits;
0136       std::vector<int> categoryMapping;
0137 
0138       COND_SERIALIZABLE;
0139     };
0140 
0141     class ProcNormalize : public VarProcessor {
0142     public:
0143       std::unique_ptr<VarProcessor> clone() const override;
0144       std::vector<HistogramF> distr;
0145       int categoryIdx;
0146 
0147       COND_SERIALIZABLE;
0148     };
0149 
0150     class ProcLikelihood : public VarProcessor {
0151     public:
0152       std::unique_ptr<VarProcessor> clone() const override;
0153       class SigBkg {
0154       public:
0155         HistogramF background;
0156         HistogramF signal;
0157         bool useSplines;
0158 
0159         COND_SERIALIZABLE;
0160       };
0161 
0162       std::vector<SigBkg> pdfs;
0163       std::vector<double> bias;
0164       int categoryIdx;
0165       bool logOutput;
0166       bool individual;
0167       bool neverUndefined;
0168       bool keepEmpty;
0169 
0170       COND_SERIALIZABLE;
0171     };
0172 
0173     class ProcLinear : public VarProcessor {
0174     public:
0175       std::unique_ptr<VarProcessor> clone() const override;
0176       std::vector<double> coeffs;
0177       double offset;
0178 
0179       COND_SERIALIZABLE;
0180     };
0181 
0182     class ProcMultiply : public VarProcessor {
0183     public:
0184       std::unique_ptr<VarProcessor> clone() const override;
0185       typedef std::vector<unsigned int> Config;
0186 
0187       unsigned int in;
0188       std::vector<Config> out;
0189 
0190       COND_SERIALIZABLE;
0191     };
0192 
0193     class ProcMatrix : public VarProcessor {
0194     public:
0195       std::unique_ptr<VarProcessor> clone() const override;
0196       Matrix matrix;
0197 
0198       COND_SERIALIZABLE;
0199     };
0200 
0201     class ProcExternal : public VarProcessor {
0202     public:
0203       std::unique_ptr<VarProcessor> clone() const override;
0204       std::string getInstanceName() const override;
0205 
0206       std::string method;
0207       std::vector<unsigned char> store;
0208 
0209       COND_SERIALIZABLE;
0210     };
0211 
0212     class ProcMLP : public VarProcessor {
0213     public:
0214       std::unique_ptr<VarProcessor> clone() const override;
0215       typedef std::pair<double, std::vector<double> > Neuron;
0216       typedef std::pair<std::vector<Neuron>, bool> Layer;
0217 
0218       std::vector<Layer> layers;
0219 
0220       COND_SERIALIZABLE;
0221     };
0222 
0223     // the discriminator computer
0224 
0225     class MVAComputer {
0226     public:
0227       MVAComputer();
0228       MVAComputer(const MVAComputer &orig);
0229       virtual ~MVAComputer();
0230 
0231       MVAComputer &operator=(const MVAComputer &orig);
0232 
0233       virtual std::vector<VarProcessor *> getProcessors();
0234       virtual std::vector<const VarProcessor *> getProcessors() const;
0235       void addProcessor(const VarProcessor *proc);
0236 
0237       // cacheId stuff to detect changes
0238       typedef unsigned int CacheId;
0239       inline CacheId getCacheId() const { return cacheId; }
0240       inline bool changed(CacheId old) const { return old != cacheId; }
0241 
0242       std::vector<Variable> inputSet;
0243       unsigned int output;
0244 
0245     private:
0246       std::vector<VarProcessor *> processors;
0247 
0248       CacheId cacheId COND_TRANSIENT;  // transient
0249 
0250       COND_SERIALIZABLE;
0251     };
0252 
0253     // a collection of computers identified by name
0254 
0255     class MVAComputerContainer {
0256     public:
0257       typedef std::pair<std::string, MVAComputer> Entry;
0258 
0259       MVAComputerContainer();
0260       virtual ~MVAComputerContainer() {}
0261 
0262       MVAComputer &add(const std::string &label);
0263       virtual const MVAComputer &find(const std::string &label) const;
0264       virtual bool contains(const std::string &label) const;
0265 
0266       // cacheId stuff to detect changes
0267       typedef unsigned int CacheId;
0268       inline CacheId getCacheId() const { return cacheId; }
0269       inline bool changed(CacheId old) const { return old != cacheId; }
0270 
0271     private:
0272       std::vector<Entry> entries;
0273 
0274       CacheId cacheId COND_TRANSIENT;  // transient
0275 
0276       COND_SERIALIZABLE;
0277     };
0278 
0279   }  // namespace Calibration
0280 }  // namespace PhysicsTools
0281 
0282 #endif  // CondFormats_PhysicsToolsObjects_MVAComputer_h