Skip to content

Commit

Permalink
Merge pull request #31531 from jwill24/pps_nanoAOD
Browse files Browse the repository at this point in the history
PPS nanoAOD
  • Loading branch information
cmsbuild committed Dec 16, 2020
2 parents 827ebb7 + 9be3994 commit 04f7b97
Show file tree
Hide file tree
Showing 10 changed files with 767 additions and 27 deletions.
71 changes: 46 additions & 25 deletions PhysicsTools/NanoAOD/interface/SimpleFlatTableProducer.h
Expand Up @@ -18,7 +18,10 @@ class SimpleFlatTableProducerBase : public edm::stream::EDProducer<> {
: name_(params.getParameter<std::string>("name")),
doc_(params.existsAs<std::string>("doc") ? params.getParameter<std::string>("doc") : ""),
extension_(params.existsAs<bool>("extension") ? params.getParameter<bool>("extension") : false),
src_(consumes<TProd>(params.getParameter<edm::InputTag>("src"))) {
skipNonExistingSrc_(
params.existsAs<bool>("skipNonExistingSrc") ? params.getParameter<bool>("skipNonExistingSrc") : false),
src_(skipNonExistingSrc_ ? mayConsume<TProd>(params.getParameter<edm::InputTag>("src"))
: consumes<TProd>(params.getParameter<edm::InputTag>("src"))) {
edm::ParameterSet const &varsPSet = params.getParameter<edm::ParameterSet>("variables");
for (const std::string &vname : varsPSet.getParameterNamesForType<edm::ParameterSet>()) {
const auto &varPSet = varsPSet.getParameter<edm::ParameterSet>(vname);
Expand Down Expand Up @@ -58,6 +61,7 @@ class SimpleFlatTableProducerBase : public edm::stream::EDProducer<> {
const std::string name_;
const std::string doc_;
const bool extension_;
const bool skipNonExistingSrc_;
const edm::EDGetTokenT<TProd> src_;

class VariableBase {
Expand Down Expand Up @@ -135,15 +139,20 @@ class SimpleFlatTableProducer : public SimpleFlatTableProducerBase<T, edm::View<
const auto &varPSet = extvarsPSet.getParameter<edm::ParameterSet>(vname);
const std::string &type = varPSet.getParameter<std::string>("type");
if (type == "int")
extvars_.push_back(std::make_unique<IntExtVar>(vname, varPSet, this->consumesCollector()));
extvars_.push_back(
std::make_unique<IntExtVar>(vname, varPSet, this->consumesCollector(), this->skipNonExistingSrc_));
else if (type == "float")
extvars_.push_back(std::make_unique<FloatExtVar>(vname, varPSet, this->consumesCollector()));
extvars_.push_back(
std::make_unique<FloatExtVar>(vname, varPSet, this->consumesCollector(), this->skipNonExistingSrc_));
else if (type == "double")
extvars_.push_back(std::make_unique<DoubleExtVar>(vname, varPSet, this->consumesCollector()));
extvars_.push_back(
std::make_unique<DoubleExtVar>(vname, varPSet, this->consumesCollector(), this->skipNonExistingSrc_));
else if (type == "uint8")
extvars_.push_back(std::make_unique<UInt8ExtVar>(vname, varPSet, this->consumesCollector()));
extvars_.push_back(
std::make_unique<UInt8ExtVar>(vname, varPSet, this->consumesCollector(), this->skipNonExistingSrc_));
else if (type == "bool")
extvars_.push_back(std::make_unique<BoolExtVar>(vname, varPSet, this->consumesCollector()));
extvars_.push_back(
std::make_unique<BoolExtVar>(vname, varPSet, this->consumesCollector(), this->skipNonExistingSrc_));
else
throw cms::Exception("Configuration", "unsupported type " + type + " for variable " + vname);
}
Expand All @@ -156,21 +165,23 @@ class SimpleFlatTableProducer : public SimpleFlatTableProducerBase<T, edm::View<
const edm::Handle<edm::View<T>> &prod) const override {
std::vector<const T *> selobjs;
std::vector<edm::Ptr<T>> selptrs; // for external variables
if (singleton_) {
assert(prod->size() == 1);
selobjs.push_back(&(*prod)[0]);
if (!extvars_.empty())
selptrs.emplace_back(prod->ptrAt(0));
} else {
for (unsigned int i = 0, n = prod->size(); i < n; ++i) {
const auto &obj = (*prod)[i];
if (cut_(obj)) {
selobjs.push_back(&obj);
if (!extvars_.empty())
selptrs.emplace_back(prod->ptrAt(i));
if (prod.isValid() || !(this->skipNonExistingSrc_)) {
if (singleton_) {
assert(prod->size() == 1);
selobjs.push_back(&(*prod)[0]);
if (!extvars_.empty())
selptrs.emplace_back(prod->ptrAt(0));
} else {
for (unsigned int i = 0, n = prod->size(); i < n; ++i) {
const auto &obj = (*prod)[i];
if (cut_(obj)) {
selobjs.push_back(&obj);
if (!extvars_.empty())
selptrs.emplace_back(prod->ptrAt(i));
}
if (selobjs.size() >= maxLen_)
break;
}
if (selobjs.size() >= maxLen_)
break;
}
}
auto out = std::make_unique<nanoaod::FlatTable>(selobjs.size(), this->name_, singleton_, this->extension_);
Expand All @@ -194,19 +205,29 @@ class SimpleFlatTableProducer : public SimpleFlatTableProducerBase<T, edm::View<
template <typename TIn, typename ValType = TIn>
class ValueMapVariable : public ExtVariable {
public:
ValueMapVariable(const std::string &aname, const edm::ParameterSet &cfg, edm::ConsumesCollector &&cc)
: ExtVariable(aname, cfg), token_(cc.consumes<edm::ValueMap<TIn>>(cfg.getParameter<edm::InputTag>("src"))) {}
ValueMapVariable(const std::string &aname,
const edm::ParameterSet &cfg,
edm::ConsumesCollector &&cc,
bool skipNonExistingSrc = false)
: ExtVariable(aname, cfg),
skipNonExistingSrc_(skipNonExistingSrc),
token_(skipNonExistingSrc_ ? cc.mayConsume<edm::ValueMap<TIn>>(cfg.getParameter<edm::InputTag>("src"))
: cc.consumes<edm::ValueMap<TIn>>(cfg.getParameter<edm::InputTag>("src"))) {}
void fill(const edm::Event &iEvent, std::vector<edm::Ptr<T>> selptrs, nanoaod::FlatTable &out) const override {
edm::Handle<edm::ValueMap<TIn>> vmap;
iEvent.getByToken(token_, vmap);
std::vector<ValType> vals(selptrs.size());
for (unsigned int i = 0, n = vals.size(); i < n; ++i) {
vals[i] = (*vmap)[selptrs[i]];
std::vector<ValType> vals;
if (vmap.isValid() || !skipNonExistingSrc_) {
vals.resize(selptrs.size());
for (unsigned int i = 0, n = vals.size(); i < n; ++i) {
vals[i] = (*vmap)[selptrs[i]];
}
}
out.template addColumn<ValType>(this->name_, vals, this->doc_, this->precision_);
}

protected:
const bool skipNonExistingSrc_;
edm::EDGetTokenT<edm::ValueMap<TIn>> token_;
};
typedef ValueMapVariable<int> IntExtVar;
Expand Down
6 changes: 6 additions & 0 deletions PhysicsTools/NanoAOD/plugins/BuildFile.xml
Expand Up @@ -16,6 +16,12 @@
<use name="CondFormats/BTauObjects"/>
<use name="CondFormats/L1TObjects"/>
<use name="CondTools/BTau"/>
<use name="DataFormats/CTPPSDetId"/>
<use name="DataFormats/CTPPSReco"/>
<use name="DataFormats/ProtonReco"/>
<use name="CondFormats/RunInfo"/>
<use name="CondFormats/DataRecord"/>
<use name="RecoPPS/ProtonReconstruction"/>
<use name="fastjet"/>
<use name="fastjet-contrib"/>
<library file="*.cc" name="PhysicsToolsNanoAODPlugins">
Expand Down
80 changes: 80 additions & 0 deletions PhysicsTools/NanoAOD/plugins/LHCInfoProducer.cc
@@ -0,0 +1,80 @@
// -*- C++ -*-
//
// Package: PhysicsTools/NanoAOD
// Class: LHCInfoProducer
//
/**\class LHCInfoProducer LHCInfoProducer.cc PhysicsTools/NanoAOD/plugins/LHCInfoProducer.cc
Description: [one line class summary]
Implementation:
[Notes on implementation]
*/
//
// Original Author: Justin Williams
// Created: 05 Jul 2019 14:06:12 GMT
//
//

// System include files
#include <memory>
#include <map>
#include <string>
#include <vector>
#include <iostream>

// user include files
#include "FWCore/Framework/interface/Frameworkfwd.h"
#include "FWCore/Framework/interface/global/EDProducer.h"

#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/MakerMacros.h"

#include "FWCore/Framework/interface/ESHandle.h"
#include "FWCore/Framework/interface/ESProducer.h"
#include "FWCore/Framework/interface/EventSetup.h"
#include "FWCore/Framework/interface/EventSetupRecordIntervalFinder.h"
#include "FWCore/Framework/interface/SourceFactory.h"

#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/Utilities/interface/StreamID.h"

#include "CommonTools/Egamma/interface/EffectiveAreas.h"

#include "DataFormats/NanoAOD/interface/FlatTable.h"
#include "DataFormats/Common/interface/ValueMap.h"
#include "DataFormats/NanoAOD/interface/MergeableCounterTable.h"

#include "FWCore/Utilities/interface/transform.h"

#include "CondFormats/RunInfo/interface/LHCInfo.h"
#include "CondFormats/DataRecord/interface/LHCInfoRcd.h"

class LHCInfoProducer : public edm::global::EDProducer<edm::BeginLuminosityBlockProducer> {
public:
LHCInfoProducer(edm::ParameterSet const&) {
produces<nanoaod::MergeableCounterTable, edm::Transition::BeginLuminosityBlock>();
}
~LHCInfoProducer() override {}

// ------------ method called to produce the data ------------
void produce(edm::StreamID id, edm::Event& iEvent, const edm::EventSetup& iSetup) const override {}

void globalBeginLuminosityBlockProduce(edm::LuminosityBlock& iLumi, edm::EventSetup const& iSetup) const override {
edm::ESHandle<LHCInfo> lhcInfo;
iSetup.get<LHCInfoRcd>().get(lhcInfo);
const LHCInfo* info = lhcInfo.product();
auto out = std::make_unique<nanoaod::MergeableCounterTable>();
out->addFloat("crossingAngle", "LHC crossing angle", info->crossingAngle());
out->addFloat("betaStar", "LHC beta star", info->betaStar());
out->addFloat("energy", "LHC beam energy", info->energy());
iLumi.put(std::move(out));
}

// ------------ method fills 'descriptions' with the allowed parameters for the module ------------
static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
edm::ParameterSetDescription desc;
desc.setUnknown();
descriptions.addDefault(desc);
}
};

DEFINE_FWK_MODULE(LHCInfoProducer);
17 changes: 17 additions & 0 deletions PhysicsTools/NanoAOD/plugins/NanoAODOutputModule.cc
Expand Up @@ -125,6 +125,7 @@ class NanoAODOutputModule : public edm::one::OutputModule<> {
std::vector<EventStringOutputBranches> m_evstrings;

std::vector<SummaryTableOutputBranches> m_runTables;
std::vector<SummaryTableOutputBranches> m_lumiTables;

std::vector<std::pair<std::string, edm::EDGetToken>> m_nanoMetadata;
};
Expand Down Expand Up @@ -224,6 +225,10 @@ void NanoAODOutputModule::writeLuminosityBlock(edm::LuminosityBlockForOutput con
jr->reportLumiSection(m_jrToken, iLumi.id().run(), iLumi.id().value());

m_commonLumiBranches.fill(iLumi.id());

for (auto& t : m_lumiTables)
t.fill(iLumi, *m_lumiTree);

m_lumiTree->Fill();

m_processHistoryRegistry.registerProcessHistory(iLumi.processHistory());
Expand Down Expand Up @@ -287,6 +292,7 @@ void NanoAODOutputModule::openFile(edm::FileBlock const&) {
m_triggers_areSorted = false;
m_evstrings.clear();
m_runTables.clear();
m_lumiTables.clear();
const auto& keeps = keptProducts();
for (const auto& keep : keeps[edm::InEvent]) {
if (keep.first->className() == "nanoaod::FlatTable")
Expand All @@ -300,6 +306,17 @@ void NanoAODOutputModule::openFile(edm::FileBlock const&) {
throw cms::Exception("Configuration", "NanoAODOutputModule cannot handle class " + keep.first->className());
}

for (const auto& keep : keeps[edm::InLumi]) {
if (keep.first->className() == "nanoaod::MergeableCounterTable")
m_lumiTables.push_back(SummaryTableOutputBranches(keep.first, keep.second));
else if (keep.first->className() == "nanoaod::UniqueString" && keep.first->moduleLabel() == "nanoMetadata")
m_nanoMetadata.emplace_back(keep.first->productInstanceName(), keep.second);
else
throw cms::Exception(
"Configuration",
"NanoAODOutputModule cannot handle class " + keep.first->className() + " in LuminosityBlock branch");
}

for (const auto& keep : keeps[edm::InRun]) {
if (keep.first->className() == "nanoaod::MergeableCounterTable")
m_runTables.push_back(SummaryTableOutputBranches(keep.first, keep.second));
Expand Down

0 comments on commit 04f7b97

Please sign in to comment.