CMS 3D CMS Logo

Classes | Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes

PhysicsTools::MVAComputer Class Reference

Main interface class to the generic discriminator computer framework. More...

#include <MVAComputer.h>

Inheritance diagram for PhysicsTools::MVAComputer:
GenericMVAComputer

List of all members.

Classes

struct  DerivContext
struct  EvalContext
class  InputVar
 input variable configuration object More...
class  Processor
 variable processor container More...

Public Member Functions

template<typename Iterator_t >
double deriv (Iterator_t first, Iterator_t last) const
template<typename Container_t >
double deriv (Container_t &values) const
template<typename Iterator_t >
double eval (Iterator_t first, Iterator_t last) const
 evaluate variables given by a range of iterators given by first and last
template<typename Container_t >
double eval (const Container_t &values) const
 evaluate variables in iterable container values
 MVAComputer (const char *filename)
 construct a discriminator computer from a calibration file
 MVAComputer (const Calibration::MVAComputer *calib)
 construct a discriminator computer from a const calibation object
 MVAComputer (Calibration::MVAComputer *calib, bool owned=false)
 construct a discriminator computer from a calibation object
 MVAComputer (std::istream &is)
 construct a discriminator computer from C++ input stream
 ~MVAComputer ()

Static Public Member Functions

static Calibration::MVAComputerreadCalibration (const char *filename)
 read calibration object from plain file
static Calibration::MVAComputerreadCalibration (std::istream &is)
 read calibration object from plain C++ input stream
static void writeCalibration (const char *filename, const Calibration::MVAComputer *calib)
 write calibration object to file
static void writeCalibration (std::ostream &os, const Calibration::MVAComputer *calib)
 write calibration object to pain C++ output stream

Private Member Functions

template<class T >
void evalInternal (T &ctx) const
 evaluate discriminator from flattened variable array
unsigned int getVariableId (AtomicId name) const
 map variable identifier name to the numerical position in the array
void setup (const Calibration::MVAComputer *calib)
 construct processors from calibration and setup variables

Private Attributes

std::vector< InputVarinputVariables
 vector of input variables
unsigned int nVars
 total number of variables to expect while computing discriminator
unsigned int output
 index of the variable in the "conf" array to return as result
std::auto_ptr
< Calibration::MVAComputer
owned
 in case calibration object is owned by the MVAComputer
std::vector< ProcessorvarProcessors
 vector of variable processors

Detailed Description

Main interface class to the generic discriminator computer framework.

The MVAComputer class represents an instance of the modular discriminator computer. It is constructed from a "calibration" object which contains all the required histograms, matrices and other trainina data required for computing the discriminator. The calibration data also defines the names and properties of variables that can passed to that instance of the discriminator computer. The evaluation methods then calculates the discriminator from a applicable set of input variables, i.e. vector of key-value pairs.

Definition at line 40 of file MVAComputer.h.


Constructor & Destructor Documentation

PhysicsTools::MVAComputer::MVAComputer ( const Calibration::MVAComputer calib)

construct a discriminator computer from a const calibation object

Definition at line 37 of file MVAComputer.cc.

References setup().

                                                            :
        nVars(0), output(0)
{
        setup(calib);
}
PhysicsTools::MVAComputer::MVAComputer ( Calibration::MVAComputer calib,
bool  owned = false 
)

construct a discriminator computer from a calibation object

Definition at line 43 of file MVAComputer.cc.

References setup().

                                                                  :
        nVars(0), output(0)
{
        if (owned)
                this->owned.reset(calib);
        setup(calib);
}
PhysicsTools::MVAComputer::~MVAComputer ( )

Definition at line 141 of file MVAComputer.cc.

{
}
PhysicsTools::MVAComputer::MVAComputer ( const char *  filename)

construct a discriminator computer from a calibration file

Definition at line 51 of file MVAComputer.cc.

References owned, and setup().

PhysicsTools::MVAComputer::MVAComputer ( std::istream &  is)

construct a discriminator computer from C++ input stream

Definition at line 57 of file MVAComputer.cc.

References owned, and setup().

                                       :
        nVars(0), output(0), owned(readCalibration(is))
{
        setup(owned.get());
}

Member Function Documentation

template<typename Iterator_t >
double PhysicsTools::MVAComputer::deriv ( Iterator_t  first,
Iterator_t  last 
) const
template<typename Container_t >
double PhysicsTools::MVAComputer::deriv ( Container_t &  values) const [inline]

Definition at line 67 of file MVAComputer.h.

        {
                typedef typename Container_t::iterator Iterator_t;
                return this->template deriv<Iterator_t>(
                                                values.begin(), values.end());
        }
template<typename Iterator_t >
double PhysicsTools::MVAComputer::eval ( Iterator_t  first,
Iterator_t  last 
) const

evaluate variables given by a range of iterators given by first and last

Referenced by PhysicsTools::TreeReader::fill().

template<typename Container_t >
double PhysicsTools::MVAComputer::eval ( const Container_t &  values) const [inline]

evaluate variables in iterable container values

Reimplemented in GenericMVAComputer.

Definition at line 59 of file MVAComputer.h.

        {
                typedef typename Container_t::const_iterator Iterator_t;
                return this->template eval<Iterator_t>(
                                                values.begin(), values.end());
        }
template<class T >
template void PhysicsTools::MVAComputer::evalInternal ( T ctx) const [private]

evaluate discriminator from flattened variable array

Definition at line 160 of file MVAComputer.cc.

References gather_cfg::cout, i, inputVariables, j, PhysicsTools::VarProcessor::kNext, PhysicsTools::VarProcessor::kReset, PhysicsTools::VarProcessor::kSkip, PhysicsTools::VarProcessor::kStop, python::cmstools::loop(), python::connectstrParser::o, evf::evtn::offset(), output, ntuplemaker::status, v, and varProcessors.

{
        double *output = ctx.values() + ctx.n();
        int *outConf = ctx.conf() + inputVariables.size();

#ifdef DEBUG_EVAL
        std::cout << "Input" << std::endl;
        double *v = ctx.values();
        for(int *o = ctx.conf(); o < outConf; o++) {
                std::cout << "\tVar " << (o - ctx.conf()) << std::endl;
                for(int i = o[0]; i < o[1]; i++)
                        std::cout << "\t\t" << *v++ << std::endl;
        }
#endif
        std::vector<Processor>::const_iterator iter = varProcessors.begin();
        while(iter != varProcessors.end()) {
                std::vector<Processor>::const_iterator loop = iter;
                int *loopOutConf = outConf;
                int *loopStart = 0;
                double *loopOutput = output;

                VarProcessor::LoopStatus status = VarProcessor::kNext;
                unsigned int offset = 0;
                while(status != VarProcessor::kStop) {
                        std::vector<Processor>::const_iterator next = iter + 1;
                        unsigned int nextOutput = (next != varProcessors.end())
                                                  ? next->nOutput : 0;

#ifdef DEBUG_EVAL
                        std::cout << ROOT::Reflex::Tools::Demangle(
                                typeid(*iter->processor)) << std::endl;
#endif
                        if (status != VarProcessor::kSkip)
                                ctx.eval(&*iter->processor, outConf, output,
                                         loopStart ? loopStart : loopOutConf,
                                         offset, iter->nOutput);

#ifdef DEBUG_EVAL
                        for(unsigned int i = 0; i < iter->nOutput;
                            i++, outConf++) {
                                std::cout << "\tVar " << (outConf - ctx.conf())
                                          << std::endl;
                                for(int j = outConf[0]; j < outConf[1]; j++)
                                        std::cout << "\t\t" << *output++
                                                  << std::endl;
                        }
#else
                        int orig = *outConf;
                        outConf += iter->nOutput;
                        output += *outConf - orig;
#endif

                        status = loop->processor->loop(output, outConf,
                                                       nextOutput, offset);

                        if (status == VarProcessor::kReset) {
                                outConf = loopOutConf;
                                output = loopOutput;
                                loopStart = 0;
                                offset = 0;
                                iter = loop;
                        } else {
                                if (loop == iter)
                                        loopStart = outConf;
                                iter = next;
                        }
                }
        }
}
unsigned int PhysicsTools::MVAComputer::getVariableId ( AtomicId  name) const [private]

map variable identifier name to the numerical position in the array

Definition at line 145 of file MVAComputer.cc.

References inputVariables, mergeVDriftHistosByStation::name, and pos.

{
        std::vector<InputVar>::const_iterator pos =
                std::lower_bound(inputVariables.begin(), inputVariables.end(),
                                 name);

        if (pos == inputVariables.end() || pos->var.getName() != name)
                throw cms::Exception("InvalidVariable")
                        << "Input variable " << (const char*)name
                        << " not found."  << std::endl;

        return pos->index;
}
Calibration::MVAComputer * PhysicsTools::MVAComputer::readCalibration ( const char *  filename) [static]

read calibration object from plain file

Definition at line 230 of file MVAComputer.cc.

References mergeVDriftHistosByStation::file.

Referenced by PhysicsTools::MVAComputerESSourceBase::produce().

{
        std::ifstream file(filename);
        return readCalibration(file);
}
Calibration::MVAComputer * PhysicsTools::MVAComputer::readCalibration ( std::istream &  is) [static]

read calibration object from plain C++ input stream

Definition at line 236 of file MVAComputer.cc.

References calib, Exception, errorMatrix2Lands::header, ext::basic_izstreambase< Item_t, Traits_t, Allocator_t >::rdbuf(), and STANDALONE_HEADER.

{
        if (!is.good())
                throw cms::Exception("InvalidFileState")
                        << "Stream passed to MVAComputer::readCalibration "
                           "has an invalid state." << std::endl;

        char header[sizeof STANDALONE_HEADER - 1] = { 0, };
        if (is.readsome(header, sizeof header) != sizeof header ||
            std::memcmp(header, STANDALONE_HEADER, sizeof header) != 0)
                throw cms::Exception("InvalidFileFormat")
                        << "Stream passed to MVAComputer::readCalibration "
                           "is not a valid calibration file." << std::endl;

        TClass *rootClass =
                TClass::GetClass("PhysicsTools::Calibration::MVAComputer");
        if (!rootClass)
                throw cms::Exception("DictionaryMissing")
                        << "CondFormats dictionary for "
                           "PhysicsTools::Calibration::MVAComputer missing"
                        << std::endl;

        ext::izstream izs(&is);
        std::ostringstream ss;
        ss << izs.rdbuf();
        std::string buf = ss.str();

        TBufferFile buffer(TBuffer::kRead, buf.size(), const_cast<void*>(
                        static_cast<const void*>(buf.c_str())), kFALSE);
        buffer.InitMap();

        std::auto_ptr<Calibration::MVAComputer> calib(
                                        new Calibration::MVAComputer());
        buffer.StreamObject(static_cast<void*>(calib.get()), rootClass);

        return calib.release();
}
void PhysicsTools::MVAComputer::setup ( const Calibration::MVAComputer calib) [private]

construct processors from calibration and setup variables

Definition at line 63 of file MVAComputer.cc.

References PhysicsTools::VarProcessor::ConfigCtx::begin(), calib, HDQMDatabaseProducer::config, PhysicsTools::VarProcessor::configure(), filterCSVwithJSON::copy, SurfaceDeformationFactory::create(), PhysicsTools::VarProcessor::ConfigCtx::end(), Exception, PhysicsTools::Variable::FLAG_ALL, PhysicsTools::Variable::FLAG_MULTIPLE, flags, PhysicsTools::Calibration::MVAComputer::getProcessors(), i, PhysicsTools::MVAComputer::InputVar::index, PhysicsTools::TrainMVAComputerCalibration::initFlags(), PhysicsTools::Calibration::MVAComputer::inputSet, inputVariables, j, PhysicsTools::VarProcessor::Config::mask, mergeVDriftHistosByStation::name, nVars, PhysicsTools::VarProcessor::Config::origin, PhysicsTools::Calibration::MVAComputer::output, output, pos, PhysicsTools::VarProcessor::ConfigCtx::size(), PhysicsTools::MVAComputer::InputVar::var, makeLayoutFileForGui::variables, and varProcessors.

Referenced by MVAComputer().

{
        nVars = calib->inputSet.size();
        output = calib->output;

        std::vector<Variable::Flags> flags(nVars, Variable::FLAG_ALL);
        const TrainMVAComputerCalibration *trainCalib =
                dynamic_cast<const TrainMVAComputerCalibration*>(calib);
        if (trainCalib)
                trainCalib->initFlags(flags);

        VarProcessor::ConfigCtx config(flags);
        std::vector<Calibration::VarProcessor*> processors =
                                                        calib->getProcessors();

        for(std::vector<Calibration::VarProcessor*>::const_iterator iter =
                                                        processors.begin();
            iter != processors.end(); ++iter) {
                std::string name = (*iter)->getInstanceName();
                VarProcessor *processor =
                        VarProcessor::create(name.c_str(), *iter, this);
                if (!processor)
                        throw cms::Exception("UnknownProcessor")
                                << name << " could not be instantiated."
                                << std::endl;

                VarProcessor::ConfigCtx::iterator::difference_type pos =
                                                config.end() - config.begin();
                processor->configure(config);
                unsigned int nOutput = (config.end() - config.begin()) - pos;
                if (!nOutput)
                        throw cms::Exception("InvalidProcessor")
                                << name << " rejected input variable "
                                "configuration" << std::endl;

                varProcessors.push_back(Processor(processor, nOutput));
        }

        for(VarProcessor::ConfigCtx::iterator iter = config.begin() + nVars;
            iter != config.end(); iter++) {
                VarProcessor::Config *origin = &config[iter->origin];
                if (iter->origin >= nVars)
                        iter->origin = origin->origin;

                if (iter->mask & Variable::FLAG_MULTIPLE) {
                        iter->mask = (Variable::Flags)(iter->mask &
                                                       origin->mask);
                        config[iter->origin].origin++;
                }
        }

        nVars = config.size();

        if (output >= nVars)
                        // FIXME || config[output].mask != Variable::FLAG_NONE)
                throw cms::Exception("InvalidOutput")
                        << "Output variable at index " << output
                        << " invalid." << std::endl;

        std::set<InputVar> variables;
        unsigned int i = 0;
        for(std::vector<Calibration::Variable>::const_iterator iter =
                        calib->inputSet.begin(); iter != calib->inputSet.end();
            ++iter, i++) {
                InputVar var;
                var.var = Variable(iter->name, config[i].mask);
                var.index = i;
                variables.insert(var);
        }

        inputVariables.resize(i);
        std::copy(variables.begin(), variables.end(),
                  inputVariables.begin());

        for(unsigned int j = 0; j < i; j++)
                inputVariables[j].multiplicity = config[j].origin;
}
void PhysicsTools::MVAComputer::writeCalibration ( const char *  filename,
const Calibration::MVAComputer calib 
) [static]

write calibration object to file

Definition at line 274 of file MVAComputer.cc.

References mergeVDriftHistosByStation::file.

Referenced by PhysicsTools::MVATrainerFileSave::endJob().

{
        std::ofstream file(filename);
        writeCalibration(file, calib);
}
void PhysicsTools::MVAComputer::writeCalibration ( std::ostream &  os,
const Calibration::MVAComputer calib 
) [static]

write calibration object to pain C++ output stream

Definition at line 281 of file MVAComputer.cc.

References Exception, and STANDALONE_HEADER.

{
        if (!os.good())
                throw cms::Exception("InvalidFileState")
                        << "Stream passed to MVAComputer::writeCalibration "
                           "has an invalid state." << std::endl;

        os << STANDALONE_HEADER;

        TClass *rootClass =
                TClass::GetClass("PhysicsTools::Calibration::MVAComputer");
        if (!rootClass)
                throw cms::Exception("DictionaryMissing")
                        << "CondFormats dictionary for "
                           "PhysicsTools::Calibration::MVAComputer missing"
                        << std::endl;

        TBufferFile buffer(TBuffer::kWrite);
        buffer.StreamObject(const_cast<void*>(static_cast<const void*>(calib)),
                            rootClass);

        ext::ozstream ozs(&os);
        ozs.write(buffer.Buffer(), buffer.Length());
        ozs.flush();
}

Member Data Documentation

vector of input variables

Definition at line 189 of file MVAComputer.h.

Referenced by evalInternal(), getVariableId(), and setup().

unsigned int PhysicsTools::MVAComputer::nVars [private]

total number of variables to expect while computing discriminator

Definition at line 195 of file MVAComputer.h.

Referenced by setup().

unsigned int PhysicsTools::MVAComputer::output [private]

index of the variable in the "conf" array to return as result

Definition at line 198 of file MVAComputer.h.

Referenced by evalInternal(), PhysicsTools::MVAComputer::DerivContext::output(), and setup().

in case calibration object is owned by the MVAComputer

Definition at line 201 of file MVAComputer.h.

Referenced by MVAComputer().

vector of variable processors

Definition at line 192 of file MVAComputer.h.

Referenced by evalInternal(), and setup().