diff --git a/GenericNtuple/plugins/FlatCandProducer.cc b/GenericNtuple/plugins/FlatCandProducer.cc new file mode 100644 index 0000000000000..61de6570f5f7e --- /dev/null +++ b/GenericNtuple/plugins/FlatCandProducer.cc @@ -0,0 +1,130 @@ +#include "FWCore/Framework/interface/Frameworkfwd.h" +#include "FWCore/Framework/interface/EDProducer.h" +#include "FWCore/Framework/interface/MakerMacros.h" + +#include "FWCore/Framework/interface/Event.h" +#include "FWCore/Framework/interface/EventSetup.h" +#include "FWCore/MessageLogger/interface/MessageLogger.h" +#include "FWCore/ParameterSet/interface/ParameterSet.h" +#include "FWCore/Utilities/interface/InputTag.h" + +#include "DataFormats/Common/interface/View.h" +#include "DataFormats/Candidate/interface/LeafCandidate.h" +#include "DataFormats/Common/interface/OneToValue.h" +#include "DataFormats/PatCandidates/interface/Muon.h" +#include "DataFormats/PatCandidates/interface/Electron.h" +#include "DataFormats/PatCandidates/interface/Jet.h" + +#include "CommonTools/Utils/interface/StringObjectFunction.h" + +#include +#include +#include +#include +#include + +using namespace std; +using namespace boost::assign; + +class FlatCandProducer : public edm::EDProducer +{ +public: + FlatCandProducer(const edm::ParameterSet& pset); + void produce(edm::Event& event, const edm::EventSetup& eventSetup); + +private: + typedef std::vector doubles; + typedef std::vector strings; + + typedef std::vector Cands; + typedef edm::ValueMap CandValueMap; + typedef edm::RefProd CandRefProd; + typedef edm::Ref CandRef; + + edm::InputTag srcLabel_; + std::vector > exprs_; + std::vector vmapLabels_; + + strings varNames_; + std::vector values_; + +}; + +FlatCandProducer::FlatCandProducer(const edm::ParameterSet& pset) +{ + srcLabel_ = pset.getParameter("src"); + edm::ParameterSet vars = pset.getParameter("variables"); + + const strings strVars = vars.getParameterNamesForType(); + for ( int i=0, n=strVars.size(); i(varName); + exprs_.push_back(StringObjectFunction(varExpr)); + varNames_.push_back(varName); + } + const strings vmapNames = vars.getParameterNamesForType(); + for ( int i=0, n=vmapNames.size(); i(vmapName); + vmapLabels_.push_back(vmapLabel); + varNames_.push_back(vmapName); + } + + values_.resize(varNames_.size()); + + produces(); + for ( int i=0, n=varNames_.size(); i(varNames_[i]); + } +} + +void FlatCandProducer::produce(edm::Event& event, const edm::EventSetup& eventSetup) +{ + edm::Handle > srcHandle; + event.getByLabel(srcLabel_, srcHandle); + + const size_t nExpr = exprs_.size(); + const size_t nVmap = vmapLabels_.size(); + std::vector > > vmapHandles(nVmap); + for ( size_t i=0; i cands(new Cands); + + // Fill informations + for ( size_t i=0, n=srcHandle->size(); i > candRef(srcHandle, i); + reco::LeafCandidate cand(candRef->charge(), candRef->p4()); + cands->push_back(cand); + for ( size_t j=0; j outHandle = event.put(cands); + for ( size_t i=0, n=nExpr+nVmap; i vmap(new CandValueMap); + CandValueMap::Filler filler(*vmap); + filler.insert(outHandle, values_[i].begin(), values_[i].end()); + filler.fill(); + values_[i].clear(); + + const std::string& varName = varNames_[i]; + event.put(vmap, varName); + } +} + +DEFINE_FWK_MODULE(FlatCandProducer); + diff --git a/GenericNtuple/plugins/FlatCandToNtupleMaker.cc b/GenericNtuple/plugins/FlatCandToNtupleMaker.cc new file mode 100644 index 0000000000000..08bdb4338c28d --- /dev/null +++ b/GenericNtuple/plugins/FlatCandToNtupleMaker.cc @@ -0,0 +1,144 @@ +#include "FWCore/Framework/interface/Frameworkfwd.h" +#include "FWCore/Framework/interface/EDAnalyzer.h" +#include "FWCore/Framework/interface/MakerMacros.h" + +#include "FWCore/Framework/interface/Event.h" +#include "FWCore/Framework/interface/EventSetup.h" +#include "FWCore/Framework/interface/LuminosityBlock.h" +#include "FWCore/MessageLogger/interface/MessageLogger.h" +#include "FWCore/ParameterSet/interface/ParameterSet.h" +#include "FWCore/Utilities/interface/InputTag.h" +#include "DataFormats/Common/interface/MergeableCounter.h" + +#include "FWCore/ServiceRegistry/interface/Service.h" +#include "CommonTools/UtilAlgos/interface/TFileService.h" + +#include "DataFormats/Common/interface/View.h" +#include "DataFormats/Common/interface/ValueMap.h" +#include "DataFormats/Candidate/interface/LeafCandidate.h" + +#include "KrAFT/GenericNtuple/interface/GenericEvent.h" + +#include "TTree.h" +#include "TH1F.h" + +#include +#include +#include + +using namespace std; +using namespace edm; + +class FlatCandToNtupleMaker : public edm::EDAnalyzer +{ +public: + FlatCandToNtupleMaker(const edm::ParameterSet& pset); + ~FlatCandToNtupleMaker() {}; + + void analyze(const edm::Event& event, const edm::EventSetup& eventSetup); + +private: + typedef std::vector doubles; + typedef std::vector strings; + typedef std::vector VInputTag; + + std::vector candLabels_; + std::vector vmapLabels_; + + TTree* tree_; + std::vector candPt_, candEta_, candPhi_, candM_; + std::vector > candVars_; + +}; + +FlatCandToNtupleMaker::FlatCandToNtupleMaker(const edm::ParameterSet& pset) +{ + // Output histograms and tree + edm::Service fs; + tree_ = fs->make("event", "event"); + + edm::ParameterSet candPSets = pset.getParameter("cands"); + const strings candNames = candPSets.getParameterNamesForType(); + for ( size_t i=0, n=candNames.size(); i(candName); + candLabels_.push_back(candPSet.getParameter("src")); + + candPt_ .push_back(new doubles); + candEta_.push_back(new doubles); + candPhi_.push_back(new doubles); + candM_ .push_back(new doubles); + + tree_->Branch((candName+"_pt" ).c_str(), candPt_ .back()); + tree_->Branch((candName+"_eta").c_str(), candEta_.back()); + tree_->Branch((candName+"_phi").c_str(), candPhi_.back()); + tree_->Branch((candName+"_m" ).c_str(), candM_ .back()); + + vmapLabels_.push_back(VInputTag()); + candVars_.push_back(std::vector()); + const string candLabelName = candLabels_.back().label(); + const strings vmapNames = candPSet.getParameter("vmaps"); + for ( size_t j=0, m=vmapNames.size(); jBranch((candName+"_"+vmapName).c_str(), candVars_.back().back()); + } + } + +} + +void FlatCandToNtupleMaker::analyze(const edm::Event& event, const edm::EventSetup& eventSetup) +{ + typedef edm::View Cands; + + const size_t nCand = candLabels_.size(); + for ( size_t iCand=0; iCand < nCand; ++iCand ) + { + edm::Handle srcHandle; + event.getByLabel(candLabels_[iCand], srcHandle); + + VInputTag vmapLabels = vmapLabels_[iCand]; + const size_t nVar = vmapLabels.size(); + std::vector > > vmapHandles(nVar); + for ( size_t iVar=0; iVarsize(); i candRef(srcHandle, i); + candPt_[iCand]->push_back(candRef->pt()); + candEta_[iCand]->push_back(candRef->eta()); + candPhi_[iCand]->push_back(candRef->phi()); + candM_[iCand]->push_back(candRef->mass()); + + for ( size_t iVar=0; iVarpush_back(var); + } + } + } + + tree_->Fill(); + for ( size_t iCand=0; iCandclear(); + candEta_[iCand]->clear(); + candPhi_[iCand]->clear(); + candM_ [iCand]->clear(); + const size_t nVar = candVars_[iCand].size(); + for ( size_t iVar=0; iVarclear(); + } + } +} + +DEFINE_FWK_MODULE(FlatCandToNtupleMaker); + diff --git a/GenericNtuple/python/flatCands_cfi.py b/GenericNtuple/python/flatCands_cfi.py new file mode 100644 index 0000000000000..8a05169101018 --- /dev/null +++ b/GenericNtuple/python/flatCands_cfi.py @@ -0,0 +1,62 @@ +import FWCore.ParameterSet.Config as cms + +flatMuons = cms.EDProducer("FlatCandProducer", + src = cms.InputTag("goodMuons"), + variables = cms.PSet( + #isTight = cms.string("userInt('isTight')"), + #isLoose = cms.string("userInt('isLoose')"), + relIso = cms.string("userIso(1)"), + dxy = cms.string("dB"), + dz = cms.string("userFloat('dz')"), + ), +) + +flatElectrons = cms.EDProducer("FlatCandProducer", + src = cms.InputTag("goodElectrons"), + variables = cms.PSet( + mva = cms.string("electronID('mvaTrigV0')"), + relIso = cms.string("userIso(2)"), + scEta = cms.string("superCluster.eta"), + dxy = cms.string("dB"), + dz = cms.string("userFloat('dz')"), + #chargeID = cms.string("isGsfCtfScPixChargeConsistent ? 3 : isGsfScPixChargeConsistent ? 2 : isGsfCtfChargeConsistent ? 1 : 0"), + ), +) + +flatJets = cms.EDProducer("FlatCandProducer", + src = cms.InputTag("goodJets"), + variables = cms.PSet( + bTagCSV = cms.string("bDiscriminator('combinedSecondaryVertexBJetTags')"), + up = cms.InputTag("goodJets", "up"), + dn = cms.InputTag("goodJets", "dn"), + res = cms.InputTag("goodJets", "res"), + resUp = cms.InputTag("goodJets", "resUp"), + resDn = cms.InputTag("goodJets", "resDn"), + ), +) + +fEvent = cms.EDAnalyzer("FlatCandToNtupleMaker", + cands = cms.PSet( + muons = cms.PSet( + src = cms.InputTag("flatMuons"), + vmaps = cms.vstring( + #"isTight", "isLoose", + "relIso", "dxy", "dz", + ), + ), + electrons = cms.PSet( + src = cms.InputTag("flatElectrons"), + vmaps = cms.vstring( + "mva", "scEta", + "relIso", "dxy", "dz", + #"chargeID", + ), + ), + jets = cms.PSet( + src = cms.InputTag("flatJets"), + vmaps = cms.vstring( + "bTagCSV", "up", "dn", "res", "resUp", "resDn", + ), + ), + ), +)