CMS 3D CMS Logo

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

JetTagMVATrainer Class Reference

#include <JetTagMVATrainer.h>

Inheritance diagram for JetTagMVATrainer:
edm::EDAnalyzer

List of all members.

Classes

struct  JetInfo

Public Member Functions

virtual void analyze (const edm::Event &event, const edm::EventSetup &es)
 JetTagMVATrainer (const edm::ParameterSet &params)
 ~JetTagMVATrainer ()

Protected Member Functions

bool isIgnoreFlavour (const JetInfo &jetInfo) const
bool isSignalFlavour (const JetInfo &jetInfo) const

Static Protected Member Functions

static bool isFlavour (const JetInfo &info, const std::vector< int > &list)

Protected Attributes

std::auto_ptr
< TagInfoMVACategorySelector
categorySelector
std::auto_ptr
< GenericMVAComputerCache
computerCache
edm::InputTag jetFlavour
double maxEta
double minEta
double minPt

Private Member Functions

void setup (const JetTagComputer &computer)

Private Attributes

std::vector< int > ignoreFlavours
std::string jetTagComputer
bool setupDone
std::vector< int > signalFlavours
std::vector< edm::InputTagtagInfos

Detailed Description

Definition at line 22 of file JetTagMVATrainer.h.


Constructor & Destructor Documentation

JetTagMVATrainer::JetTagMVATrainer ( const edm::ParameterSet params) [explicit]

Definition at line 42 of file JetTagMVATrainer.cc.

References categorySelector, computerCache, edm::ParameterSet::getParameter(), ignoreFlavours, signalFlavours, and python::multivaluedict::sort().

                                                                :
        jetFlavour(params.getParameter<edm::InputTag>("jetFlavourMatching")),
        minPt(params.getParameter<double>("minimumTransverseMomentum")),
        minEta(params.getParameter<double>("minimumPseudoRapidity")),
        maxEta(params.getParameter<double>("maximumPseudoRapidity")),
        setupDone(false),
        jetTagComputer(params.getParameter<std::string>("jetTagComputer")),
        tagInfos(params.getParameter< std::vector<edm::InputTag> >("tagInfos")),
        signalFlavours(params.getParameter<std::vector<int> >("signalFlavours")),
        ignoreFlavours(params.getParameter<std::vector<int> >("ignoreFlavours"))
{
        std::sort(signalFlavours.begin(), signalFlavours.end());
        std::sort(ignoreFlavours.begin(), ignoreFlavours.end());

        std::vector<std::string> calibrationLabels;
        if (params.getParameter<bool>("useCategories")) {
                categorySelector.reset(new TagInfoMVACategorySelector(params));

                calibrationLabels = categorySelector->getCategoryLabels();
        } else {
                std::string calibrationRecord =
                        params.getParameter<std::string>("calibrationRecord");

                calibrationLabels.push_back(calibrationRecord);
        }

        computerCache.reset(new GenericMVAComputerCache(calibrationLabels));
}
JetTagMVATrainer::~JetTagMVATrainer ( )

Definition at line 71 of file JetTagMVATrainer.cc.

{
}

Member Function Documentation

void JetTagMVATrainer::analyze ( const edm::Event event,
const edm::EventSetup es 
) [virtual]

Implements edm::EDAnalyzer.

Definition at line 148 of file JetTagMVATrainer.cc.

References abs, reco::TaggingVariableList::begin(), calib, categorySelector, computerCache, filterCSVwithJSON::copy, reco::JetFlavour::Leptons::electron, reco::TaggingVariableList::end(), Exception, reco::flavour(), edm::EventSetup::get(), i, getHLTprescales::index, info, edm::eventsetup::heterocontainer::insert(), isIgnoreFlavour(), isSignalFlavour(), GenericMVAComputer::iterator(), j, metsig::jet, jetFlavour, jetTagComputer, kJetEta, kJetPt, EgammaValidation_Wenu_cff::leptons, maxEta, minEta, minPt, reco::JetFlavour::Leptons::muon, GenericMVAJetTagComputer::passEventSetup(), pos, edm::ESHandle< T >::product(), setup(), setupDone, reco::JetExtendedAssociation::setValue(), reco::TaggingVariableList::size(), GenericMVAJetTagComputer::taggingVariables(), tagInfos, JetTagMVATrainer::JetInfo::tagInfos, filterCSVwithJSON::target, reco::JetFlavour::Leptons::tau, makeHLTPrescaleTable::values, and diffTreeTool::variables.

{
        // retrieve MVAComputer calibration container
        edm::ESHandle<Calibration::MVAComputerContainer> calibHandle;
        es.get<BTauGenericMVAJetTagComputerRcd>().get("trainer", calibHandle);
        const Calibration::MVAComputerContainer *calib = calibHandle.product();

        // check container for changes
        computerCache->update(calib);
        if (computerCache->isEmpty())
                return;

        // retrieve JetTagComputer
        edm::ESHandle<JetTagComputer> computerHandle;
        es.get<JetTagComputerRecord>().get(jetTagComputer, computerHandle);
        const GenericMVAJetTagComputer *computer =
                        dynamic_cast<const GenericMVAJetTagComputer*>(
                                                computerHandle.product());
        if (!computer)
                throw cms::Exception("InvalidCast")
                        << "JetTagComputer is not a MVAJetTagComputer "
                           "in JetTagMVATrainer" << std::endl;

        computer->passEventSetup(es);

        // finalize the JetTagMVALearning <-> JetTagComputer glue setup
        if (!setupDone)
                setup(*computer);

        // retrieve TagInfos
        typedef std::map<edm::RefToBase<Jet>, JetInfo, JetCompare> JetInfoMap;
        JetInfoMap jetInfos;

        std::vector< edm::Handle< edm::View<BaseTagInfo> > >
                                        tagInfoHandles(tagInfos.size());
        unsigned int nTagInfos = tagInfos.size();
        for(unsigned int i = 0; i < nTagInfos; i++) {
                edm::Handle< edm::View<BaseTagInfo> > &tagInfoHandle =
                                                        tagInfoHandles[i];
                event.getByLabel(tagInfos[i], tagInfoHandle);

                int j = 0;
                for(edm::View<BaseTagInfo>::const_iterator iter =
                        tagInfoHandle->begin();
                                iter != tagInfoHandle->end(); iter++, j++) {

                        JetInfo &jetInfo = jetInfos[iter->jet()];
                        if (jetInfo.tagInfos.empty()) {
                                jetInfo.tagInfos.resize(nTagInfos, -1);
                        }

                        jetInfo.tagInfos[i] = j;
                }
        }

        // retrieve jet flavours;
        edm::Handle<JetFlavourMatchingCollection> jetFlavourHandle;
        event.getByLabel(jetFlavour, jetFlavourHandle);

        for(JetFlavourMatchingCollection::const_iterator iter =
                jetFlavourHandle->begin();
                                iter != jetFlavourHandle->end(); iter++) {

                JetInfoMap::iterator pos =
                        jetInfos.find(edm::RefToBase<Jet>(iter->first));
                if (pos != jetInfos.end()) {
                        int flavour = iter->second.getFlavour();
                        flavour = std::abs(flavour);
                        if (flavour < 100) {
                                JetFlavour::Leptons leptons =
                                                iter->second.getLeptons();

                                pos->second.flavour = flavour;
                                pos->second.leptons[0] = leptons.electron > 0;
                                pos->second.leptons[1] = leptons.muon > 0;
                                pos->second.leptons[2] = leptons.tau > 0;
                        }
                }
        }

        // cached array containing MVAComputer value list
        std::vector<Variable::Value> values;
        values.push_back(Variable::Value(MVATrainer::kTargetId, 0));
        values.push_back(Variable::Value(kJetPt, 0));
        values.push_back(Variable::Value(kJetEta, 0));

        // now loop over the map and compute all JetTags
        for(JetInfoMap::const_iterator iter = jetInfos.begin();
            iter != jetInfos.end(); iter++) {
                edm::RefToBase<Jet> jet = iter->first;
                const JetInfo &info = iter->second;

                // simple jet filter
                if (jet->pt() < minPt ||
                    std::abs(jet->eta()) < minEta ||
                    std::abs(jet->eta()) > maxEta)
                        continue;

                // do not train with unknown jet flavours
                if (isIgnoreFlavour(info))
                        continue;

                // is it a b-jet?
                bool target = isSignalFlavour(info);

                // build TagInfos pointer for helper
                std::vector<const BaseTagInfo*> tagInfoPtrs(nTagInfos);
                for(unsigned int i = 0; i < nTagInfos; i++)  {
                        if (info.tagInfos[i] < 0)
                                continue;

                        tagInfoPtrs[i] =
                                &tagInfoHandles[i]->at(info.tagInfos[i]);
                }
                JetTagComputer::TagInfoHelper helper(tagInfoPtrs);

                TaggingVariableList variables =
                                        computer->taggingVariables(helper);

                // retrieve index of computer in case categories are used
                int index = 0;
                if (categorySelector.get()) {
                        index = categorySelector->findCategory(variables);
                        if (index < 0)
                                continue;
                }

                GenericMVAComputer *mvaComputer =
                                        computerCache->getComputer(index);
                if (!mvaComputer)
                        continue;

                // composite full array of MVAComputer values
                values.resize(3 + variables.size());
                std::vector<Variable::Value>::iterator insert = values.begin();

                (insert++)->setValue(target);
                (insert++)->setValue(jet->pt());
                (insert++)->setValue(jet->eta());
                std::copy(mvaComputer->iterator(variables.begin()),
                          mvaComputer->iterator(variables.end()), insert);

                static_cast<MVAComputer*>(mvaComputer)->eval(values);
        }
}
bool JetTagMVATrainer::isFlavour ( const JetInfo info,
const std::vector< int > &  list 
) [static, protected]

Definition at line 122 of file JetTagMVATrainer.cc.

References JetTagMVATrainer::JetInfo::flavour, i, and JetTagMVATrainer::JetInfo::leptons.

Referenced by isIgnoreFlavour(), and isSignalFlavour().

{
        if (::isFlavour(info.flavour, list))
                return true;
        else if (info.flavour < 4)
                return false;

        for(unsigned int i = 1; i <= 3; i++)
                if (info.leptons[i - 1] &&
                    ::isFlavour(info.flavour * 10 + i, list))
                        return true;

        return false;
}
bool JetTagMVATrainer::isIgnoreFlavour ( const JetInfo jetInfo) const [protected]

Definition at line 143 of file JetTagMVATrainer.cc.

References ignoreFlavours, and isFlavour().

Referenced by analyze().

bool JetTagMVATrainer::isSignalFlavour ( const JetInfo jetInfo) const [protected]

Definition at line 138 of file JetTagMVATrainer.cc.

References isFlavour(), and signalFlavours.

Referenced by analyze().

void JetTagMVATrainer::setup ( const JetTagComputer computer) [private]

Definition at line 75 of file JetTagMVATrainer.cc.

References edm::errors::Configuration, Exception, JetTagComputer::getInputLabels(), argparse::message, setupDone, and tagInfos.

Referenced by analyze().

{
        std::vector<std::string> inputLabels(computer.getInputLabels());

        if (inputLabels.empty())
                inputLabels.push_back("tagInfo");

        if (tagInfos.size() != inputLabels.size()) {
                std::string message("VInputTag size mismatch - the following "
                                    "taginfo labels are needed:\n");
                for(std::vector<std::string>::const_iterator iter =
                        inputLabels.begin(); iter != inputLabels.end(); ++iter)
                        message += "\"" + *iter + "\"\n";
                throw edm::Exception(edm::errors::Configuration) << message;
        }

        setupDone = true;
}

Member Data Documentation

Definition at line 39 of file JetTagMVATrainer.h.

Referenced by analyze(), and JetTagMVATrainer().

Definition at line 40 of file JetTagMVATrainer.h.

Referenced by analyze(), and JetTagMVATrainer().

std::vector<int> JetTagMVATrainer::ignoreFlavours [private]

Definition at line 55 of file JetTagMVATrainer.h.

Referenced by isIgnoreFlavour(), and JetTagMVATrainer().

Definition at line 38 of file JetTagMVATrainer.h.

Referenced by analyze().

std::string JetTagMVATrainer::jetTagComputer [private]

Definition at line 50 of file JetTagMVATrainer.h.

Referenced by analyze().

double JetTagMVATrainer::maxEta [protected]

Definition at line 44 of file JetTagMVATrainer.h.

Referenced by analyze().

double JetTagMVATrainer::minEta [protected]

Definition at line 43 of file JetTagMVATrainer.h.

Referenced by analyze().

double JetTagMVATrainer::minPt [protected]

Definition at line 42 of file JetTagMVATrainer.h.

Referenced by analyze().

Definition at line 49 of file JetTagMVATrainer.h.

Referenced by analyze(), and setup().

std::vector<int> JetTagMVATrainer::signalFlavours [private]

Definition at line 54 of file JetTagMVATrainer.h.

Referenced by isSignalFlavour(), and JetTagMVATrainer().

std::vector<edm::InputTag> JetTagMVATrainer::tagInfos [private]

Definition at line 52 of file JetTagMVATrainer.h.

Referenced by analyze(), and setup().