Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 32 additions & 25 deletions PhysicsTools/NanoAOD/interface/SimpleFlatTableProducer.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ 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>()) {
Expand Down Expand Up @@ -55,6 +56,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 All @@ -70,7 +72,7 @@ class SimpleFlatTableProducerBase : public edm::stream::EDProducer<> {
protected:
std::string name_, doc_;
nanoaod::FlatTable::ColumnType type_;
int precision_;
int precision_;
};
class Variable : public VariableBase {
public:
Expand Down Expand Up @@ -122,11 +124,11 @@ class SimpleFlatTableProducer : public SimpleFlatTableProducerBase<T, edm::View<
for (const std::string & vname : extvarsPSet.getParameterNamesForType<edm::ParameterSet>()) {
const auto & varPSet = extvarsPSet.getParameter<edm::ParameterSet>(vname);
const std::string & type = varPSet.getParameter<std::string>("type");
if (type == "int") extvars_.push_back(new IntExtVar(vname, nanoaod::FlatTable::IntColumn, varPSet, this->consumesCollector()));
else if (type == "float") extvars_.push_back(new FloatExtVar(vname, nanoaod::FlatTable::FloatColumn, varPSet, this->consumesCollector()));
else if (type == "double") extvars_.push_back(new DoubleExtVar(vname, nanoaod::FlatTable::FloatColumn, varPSet, this->consumesCollector()));
else if (type == "uint8") extvars_.push_back(new UInt8ExtVar(vname, nanoaod::FlatTable::UInt8Column, varPSet, this->consumesCollector()));
else if (type == "bool") extvars_.push_back(new BoolExtVar(vname, nanoaod::FlatTable::BoolColumn, varPSet, this->consumesCollector()));
if (type == "int") extvars_.push_back(new IntExtVar(vname, nanoaod::FlatTable::IntColumn, varPSet, this->consumesCollector(), this->skipNonExistingSrc_));
else if (type == "float") extvars_.push_back(new FloatExtVar(vname, nanoaod::FlatTable::FloatColumn, varPSet, this->consumesCollector(), this->skipNonExistingSrc_));
else if (type == "double") extvars_.push_back(new DoubleExtVar(vname, nanoaod::FlatTable::FloatColumn, varPSet, this->consumesCollector(), this->skipNonExistingSrc_));
else if (type == "uint8") extvars_.push_back(new UInt8ExtVar(vname, nanoaod::FlatTable::UInt8Column, varPSet, this->consumesCollector(), this->skipNonExistingSrc_));
else if (type == "bool") extvars_.push_back(new BoolExtVar(vname, nanoaod::FlatTable::BoolColumn, varPSet, this->consumesCollector(), this->skipNonExistingSrc_));
else throw cms::Exception("Configuration", "unsupported type "+type+" for variable "+vname);
}
}
Expand All @@ -137,18 +139,20 @@ class SimpleFlatTableProducer : public SimpleFlatTableProducerBase<T, edm::View<
std::unique_ptr<nanoaod::FlatTable> fillTable(const edm::Event &iEvent, 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 @@ -159,7 +163,7 @@ class SimpleFlatTableProducer : public SimpleFlatTableProducerBase<T, edm::View<

protected:
bool singleton_;
const unsigned int maxLen_;
const unsigned int maxLen_;
const StringCutObjectSelector<T> cut_;

class ExtVariable : public base::VariableBase {
Expand All @@ -171,18 +175,22 @@ class SimpleFlatTableProducer : public SimpleFlatTableProducerBase<T, edm::View<
template<typename TIn, typename ValType=TIn>
class ValueMapVariable : public ExtVariable {
public:
ValueMapVariable(const std::string & aname, nanoaod::FlatTable::ColumnType atype, const edm::ParameterSet & cfg, edm::ConsumesCollector && cc) :
ExtVariable(aname, atype, cfg), token_(cc.consumes<edm::ValueMap<TIn>>(cfg.getParameter<edm::InputTag>("src"))) {}
ValueMapVariable(const std::string & aname, nanoaod::FlatTable::ColumnType atype, const edm::ParameterSet & cfg, edm::ConsumesCollector && cc, bool skipNonExistingSrc = false) :
ExtVariable(aname, atype, 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->type_, this->precision_);
}
protected:
const bool skipNonExistingSrc_;
edm::EDGetTokenT<edm::ValueMap<TIn>> token_;
};
typedef ValueMapVariable<int> IntExtVar;
Expand Down Expand Up @@ -225,4 +233,3 @@ class FirstObjectSimpleFlatTableProducer : public SimpleFlatTableProducerBase<T,
return out;
}
};

6 changes: 6 additions & 0 deletions PhysicsTools/NanoAOD/plugins/BuildFile.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
<use name="DQMServices/Core"/>
<use name="CondFormats/BTauObjects"/>
<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="RecoCTPPS/ProtonReconstruction"/>
<use name="fastjet"/>
<use name="fastjet-contrib"/>
<library file="*.cc" name="PhysicsToolsNanoAODPlugins">
Expand Down
78 changes: 78 additions & 0 deletions PhysicsTools/NanoAOD/plugins/LHCInfoProducer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// -*- 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 "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);
27 changes: 19 additions & 8 deletions PhysicsTools/NanoAOD/plugins/NanoAODOutputModule.cc
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,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 @@ -225,6 +226,8 @@ NanoAODOutputModule::writeLuminosityBlock(edm::LuminosityBlockForOutput const& i
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 @@ -283,36 +286,44 @@ NanoAODOutputModule::openFile(edm::FileBlock const&) {
m_file->SetCompressionAlgorithm(ROOT::kLZMA);
} else {
throw cms::Exception("Configuration") << "NanoAODOutputModule configured with unknown compression algorithm '" << m_compressionAlgorithm << "'\n"
<< "Allowed compression algorithms are ZLIB and LZMA\n";
<< "Allowed compression algorithms are ZLIB and LZMA\n";
}
/* Setup file structure here */
m_tables.clear();
m_triggers.clear();
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" )
m_tables.emplace_back(keep.first, keep.second);
m_tables.emplace_back(keep.first, keep.second);
else if(keep.first->className() == "edm::TriggerResults" )
{
m_triggers.emplace_back(keep.first, keep.second);
}
{
m_triggers.emplace_back(keep.first, keep.second);
}
else if(keep.first->className() == "std::basic_string<char,std::char_traits<char> >" && keep.first->productInstanceName()=="genModel") { // friendlyClassName == "String"
m_evstrings.emplace_back(keep.first, keep.second, true); // update only at lumiBlock transitions
m_evstrings.emplace_back(keep.first, keep.second, true); // update only at lumiBlock transitions
}
else throw cms::Exception("Configuration", "NanoAODOutputModule cannot handle class " + keep.first->className());
}

for (const auto & keep : keeps[edm::InRun]) {
if(keep.first->className() == "nanoaod::MergeableCounterTable" )
m_runTables.push_back(SummaryTableOutputBranches(keep.first, keep.second));
m_runTables.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);
m_nanoMetadata.emplace_back(keep.first->productInstanceName(), keep.second);
else throw cms::Exception("Configuration", "NanoAODOutputModule cannot handle class " + keep.first->className() + " in Run branch");
}

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");
}

// create the trees
m_tree.reset(new TTree("Events","Events"));
Expand Down
Loading