CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_7_hltpatch2/src/PhysicsTools/MVAComputer/src/VarProcessor.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:     MVAComputer
00004 // Class  :     VarProcessor
00005 // 
00006 
00007 // Implementation:
00008 //     Base class for variable processors. Basically only passes calls
00009 //     through to virtual methods in the actual implementation daughter class.
00010 //
00011 // Author:      Christophe Saout
00012 // Created:     Sat Apr 24 15:18 CEST 2007
00013 // $Id: VarProcessor.cc,v 1.9 2011/04/15 17:07:13 wmtan Exp $
00014 //
00015 
00016 #include "FWCore/Utilities/interface/Exception.h"
00017 
00018 #include "FWCore/PluginManager/interface/PluginManager.h"
00019 #include "FWCore/PluginManager/interface/PluginFactory.h"
00020 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00021 
00022 #include "PhysicsTools/MVAComputer/interface/VarProcessor.h"
00023 #include "PhysicsTools/MVAComputer/interface/Calibration.h"
00024 #include "PhysicsTools/MVAComputer/interface/BitSet.h"
00025 #include "PhysicsTools/MVAComputer/interface/ProcessRegistry.icc"
00026 
00027 // #define DEBUG_DERIV
00028 
00029 #ifdef DEBUG_DERIV
00030 #       include <Reflex/Tools.h>
00031 #endif
00032 
00033 EDM_REGISTER_PLUGINFACTORY(PhysicsTools::VarProcessor::PluginFactory,
00034                            "PhysicsToolsMVAComputer");
00035 
00036 namespace PhysicsTools {
00037 
00038 VarProcessor::VarProcessor(const char *name,
00039                            const Calibration::VarProcessor *calib,
00040                            const MVAComputer *computer) :
00041         computer(computer),
00042         inputVars(Calibration::convert(calib->inputVars)),
00043         nInputVars(inputVars.bits())
00044 {
00045 }
00046 
00047 VarProcessor::~VarProcessor()
00048 {
00049         inputVars = BitSet(0);
00050         nInputVars = 0;
00051 }
00052 
00053 void VarProcessor::configure(ConfigCtx &config)
00054 {
00055         ConfigCtx::size_type pos = config.size();
00056         if (pos != inputVars.size())
00057                 return;
00058 
00059         ConfIterator iter(inputVars.iter(), config);
00060         configure(iter, nInputVars);
00061 
00062         VarProcessor *loop = config.loop ? config.loop : this;
00063         ConfigCtx::Context *ctx =
00064                 loop->configureLoop(config.ctx, config.begin(),
00065                                     config.begin() + pos, config.end());
00066 
00067         if (ctx != config.ctx) {
00068                 delete config.ctx;
00069                 config.ctx = ctx;
00070         }
00071 
00072         if (config.loop && !ctx)
00073                 config.loop = 0;
00074         else if (!config.loop && ctx)
00075                 config.loop = this;
00076 }
00077 
00078 VarProcessor::ConfigCtx::ConfigCtx(std::vector<Variable::Flags> flags) :
00079         loop(0), ctx(0)
00080 {
00081         for(std::vector<Variable::Flags>::const_iterator iter = flags.begin();
00082             iter != flags.end(); ++iter)
00083                 configs.push_back(Config(*iter, 1));
00084 }
00085 
00086 VarProcessor::ConfigCtx::Context *
00087 VarProcessor::configureLoop(ConfigCtx::Context *ctx, ConfigCtx::iterator begin,
00088                             ConfigCtx::iterator cur, ConfigCtx::iterator end)
00089 {
00090         return 0;
00091 }
00092 
00093 template<>
00094 VarProcessor *ProcessRegistry<VarProcessor, Calibration::VarProcessor,
00095                               const MVAComputer>::Factory::create(
00096                 const char *name, const Calibration::VarProcessor *calib,
00097                 const MVAComputer *parent)
00098 {
00099         VarProcessor *result = ProcessRegistry::create(name, calib, parent);
00100         if (!result) {
00101                 // try to load the shared library and retry
00102                 try {
00103                         delete VarProcessor::PluginFactory::get()->create(
00104                                         std::string("VarProcessor/") + name);
00105                         result = ProcessRegistry::create(name, calib, parent);
00106                 } catch(const cms::Exception &e) {
00107                         // caller will have to deal with the null pointer
00108                         // in principle this will just give a slightly more
00109                         // descriptive error message (and will rethrow anyhow)
00110 
00111                   edm::LogError("CannotBuildMVAProc")
00112                     << "Caught exception when building processor: "
00113                     << name << " message: " << std::endl
00114                     << e.what() << std::endl;
00115                   throw e;
00116                 }
00117         }
00118         return result;
00119 }
00120 
00121 void VarProcessor::deriv(double *input, int *conf, double *output,
00122                          int *outConf, int *loop, unsigned int offset,
00123                          unsigned int in, unsigned int out_,
00124                          std::vector<double> &deriv) const
00125 {
00126         ValueIterator iter(inputVars.iter(), input, conf,
00127                            output, outConf, loop, offset);
00128 
00129         eval(iter, nInputVars);
00130 
00131         std::vector<double> matrix = this->deriv(iter, nInputVars);
00132 
00133         unsigned int size = 0;
00134         while(iter)
00135                 size += (iter++).size();
00136         unsigned int out = matrix.empty() ? 0 : (matrix.size() / size);
00137 
00138         if (matrix.size() != out * size ||
00139             (out > 1 && (int)out != outConf[out_] - outConf[0]))
00140                 throw cms::Exception("VarProcessor")
00141                         << "Derivative matrix implausible size in "
00142                         << typeid(*this).name() << "."
00143                         << std::endl;
00144 
00145 #ifdef DEBUG_DERIV
00146         if (!matrix.empty()) {
00147                 std::cout << "---------------- "
00148                           << ROOT::Reflex::Tools::Demangle(typeid(*this))
00149                           << std::endl;
00150                 for(unsigned int i = 0; i < out; i++) {
00151                         for(unsigned int j = 0; j < size; j++)
00152                                 std::cout << matrix.at(i*size+j) << "\t";
00153                         std::cout << std::endl;
00154                 }
00155                 std::cout << "----------------" << std::endl;
00156         }
00157 
00158         std::cout << "======= in = " << in << ", size = " << size
00159                   << ", out = " << out << ", matrix = " << matrix.size()
00160                   << std::endl;
00161 #endif
00162 
00163         unsigned int sz = (outConf[out_] - in) * in;
00164         unsigned int oldSz = deriv.size();
00165         if (oldSz < sz)
00166                 deriv.resize(sz);
00167 
00168         double *begin = &deriv.front() + (outConf[0] - in + offset) * in;
00169         double *end = begin + out * in;
00170         if (begin < &deriv.front() + oldSz)
00171                 std::fill(begin, end, 0.0);
00172 
00173         if (matrix.empty())
00174                 return;
00175 
00176         double *m0 = &matrix.front();
00177         BitSet::Iterator cur = inputVars.iter();
00178         for(unsigned int i = 0; i < nInputVars; i++, ++cur) {
00179 #ifdef DEBUG_DERIV
00180                 std::cout << " inputvar " << i << std::endl;
00181 #endif
00182                 int *curConf = conf + cur();
00183                 unsigned int pos = *curConf;
00184 #ifdef DEBUG_DERIV
00185                 std::cout << " -> cur = " << cur() << ", pos = "
00186                           << pos << std::endl;
00187 #endif
00188                 if (loop && curConf >= loop) {
00189                         pos += offset;
00190                         loop = 0;
00191                 }
00192 
00193                 unsigned int n = loop ? (curConf[1] - curConf[0]) : 1;
00194                 for(unsigned int j = 0; j < n; m0++, j++, pos++) {
00195 #ifdef DEBUG_DERIV
00196                         std::cout << "  multip " << j << std::endl;
00197 #endif
00198                         double *p = begin;
00199                         if (pos >= in) {
00200 #ifdef DEBUG_DERIV
00201                                 std::cout << "   deriv " << (pos - in)
00202                                           << std::endl;
00203 #endif
00204                                 const double *q = &deriv.front() +
00205                                                   (pos - in) * in;
00206                                 for(const double *m = m0; p < end;
00207                                     m += size, q -= in)
00208                                         for(unsigned int k = 0; k < in; k++)
00209                                                 *p++ += *m * *q++;
00210                         } else {
00211 #ifdef DEBUG_DERIV
00212                                 std::cout << "   in " << pos << std::endl;
00213 #endif
00214                                 for(const double *m = m0; p < end;
00215                                     m += size, p += in)
00216                                         p[pos] += *m;
00217                         }
00218                 }
00219         }
00220 
00221 #ifdef DEBUG_DERIV
00222         std::cout << "================" << std::endl;
00223         for(const double *p = &deriv.front();
00224             p != &deriv.front() + deriv.size();) {
00225                 for(unsigned int j = 0; j < in; j++)
00226                         std::cout << *p++ << "\t";
00227                 std::cout << std::endl;
00228         }
00229         std::cout << "================" << std::endl;
00230 #endif
00231 }
00232 
00233 } // namespace PhysicsTools
00234 
00235 // Force instantiation of its templated static methods.
00236 template void PhysicsTools::ProcessRegistry<PhysicsTools::VarProcessor, PhysicsTools::Calibration::VarProcessor, PhysicsTools::MVAComputer const>::unregisterProcess(char const*);
00237 template void PhysicsTools::ProcessRegistry<PhysicsTools::VarProcessor, PhysicsTools::Calibration::VarProcessor, PhysicsTools::MVAComputer const>::registerProcess(char const*, PhysicsTools::ProcessRegistry<PhysicsTools::VarProcessor, PhysicsTools::Calibration::VarProcessor, PhysicsTools::MVAComputer const> const*);