-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34169 from mzarucki/nanoAOD_genFilterEff_120X
Storing Generator Filter Information in NanoAOD
- Loading branch information
Showing
7 changed files
with
327 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
#include "PhysicsTools/NanoAOD/plugins/LumiOutputBranches.h" | ||
|
||
#include <iostream> | ||
|
||
namespace { | ||
std::string makeBranchName(const std::string &baseName, const std::string &leafName) { | ||
return baseName.empty() ? leafName : (leafName.empty() ? baseName : baseName + "_" + leafName); | ||
} | ||
} // namespace | ||
|
||
void LumiOutputBranches::defineBranchesFromFirstEvent(const nanoaod::FlatTable &tab) { | ||
m_baseName = tab.name(); | ||
for (size_t i = 0; i < tab.nColumns(); i++) { | ||
const std::string &var = tab.columnName(i); | ||
switch (tab.columnType(i)) { | ||
case nanoaod::FlatTable::ColumnType::Float: | ||
m_floatBranches.emplace_back(var, tab.columnDoc(i), "F"); | ||
break; | ||
case nanoaod::FlatTable::ColumnType::Int: | ||
m_intBranches.emplace_back(var, tab.columnDoc(i), "I"); | ||
break; | ||
case nanoaod::FlatTable::ColumnType::UInt8: | ||
m_uint8Branches.emplace_back(var, tab.columnDoc(i), "b"); | ||
break; | ||
case nanoaod::FlatTable::ColumnType::Bool: | ||
m_uint8Branches.emplace_back(var, tab.columnDoc(i), "O"); | ||
break; | ||
default: | ||
throw cms::Exception("LogicError", "Unsupported type"); | ||
} | ||
} | ||
} | ||
|
||
void LumiOutputBranches::branch(TTree &tree) { | ||
if (!m_singleton) { | ||
if (m_extension == IsExtension) { | ||
m_counterBranch = tree.FindBranch(("n" + m_baseName).c_str()); | ||
if (!m_counterBranch) { | ||
throw cms::Exception("LogicError", | ||
"Trying to save an extension table for " + m_baseName + | ||
" before having saved the corresponding main table\n"); | ||
} | ||
} else { | ||
if (tree.FindBranch(("n" + m_baseName).c_str()) != nullptr) { | ||
throw cms::Exception("LogicError", "Trying to save multiple main tables for " + m_baseName + "\n"); | ||
} | ||
m_counterBranch = tree.Branch(("n" + m_baseName).c_str(), &m_counter, ("n" + m_baseName + "/i").c_str()); | ||
m_counterBranch->SetTitle(m_doc.c_str()); | ||
} | ||
} | ||
std::string varsize = m_singleton ? "" : "[n" + m_baseName + "]"; | ||
for (std::vector<NamedBranchPtr> *branches : {&m_floatBranches, &m_intBranches, &m_uint8Branches}) { | ||
for (auto &pair : *branches) { | ||
std::string branchName = makeBranchName(m_baseName, pair.name); | ||
pair.branch = | ||
tree.Branch(branchName.c_str(), (void *)nullptr, (branchName + varsize + "/" + pair.rootTypeCode).c_str()); | ||
pair.branch->SetTitle(pair.title.c_str()); | ||
} | ||
} | ||
} | ||
|
||
void LumiOutputBranches::fill(const edm::LuminosityBlockForOutput &iLumi, TTree &tree, bool extensions) { | ||
if (m_extension != DontKnowYetIfMainOrExtension) { | ||
if (extensions != m_extension) | ||
return; // do nothing, wait to be called with the proper flag | ||
} | ||
|
||
edm::Handle<nanoaod::FlatTable> handle; | ||
iLumi.getByToken(m_token, handle); | ||
const nanoaod::FlatTable &tab = *handle; | ||
m_counter = tab.size(); | ||
m_singleton = tab.singleton(); | ||
if (!m_branchesBooked) { | ||
m_extension = tab.extension() ? IsExtension : IsMain; | ||
if (extensions != m_extension) | ||
return; // do nothing, wait to be called with the proper flag | ||
defineBranchesFromFirstEvent(tab); | ||
m_doc = tab.doc(); | ||
m_branchesBooked = true; | ||
branch(tree); | ||
} | ||
if (!m_singleton && m_extension == IsExtension) { | ||
if (m_counter != *reinterpret_cast<UInt_t *>(m_counterBranch->GetAddress())) { | ||
throw cms::Exception("LogicError", | ||
"Mismatch in number of entries between extension and main table for " + tab.name()); | ||
} | ||
} | ||
for (auto &pair : m_floatBranches) | ||
fillColumn<float>(pair, tab); | ||
for (auto &pair : m_intBranches) | ||
fillColumn<int>(pair, tab); | ||
for (auto &pair : m_uint8Branches) | ||
fillColumn<uint8_t>(pair, tab); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#ifndef PhysicsTools_NanoAOD_LumiOutputBranches_h | ||
#define PhysicsTools_NanoAOD_LumiOutputBranches_h | ||
|
||
#include <string> | ||
#include <vector> | ||
#include <TTree.h> | ||
#include "FWCore/Framework/interface/LuminosityBlockForOutput.h" | ||
#include "DataFormats/NanoAOD/interface/FlatTable.h" | ||
#include "DataFormats/Provenance/interface/BranchDescription.h" | ||
#include "FWCore/Utilities/interface/EDGetToken.h" | ||
|
||
class LumiOutputBranches { | ||
public: | ||
LumiOutputBranches(const edm::BranchDescription *desc, const edm::EDGetToken &token) | ||
: m_token(token), m_extension(DontKnowYetIfMainOrExtension), m_branchesBooked(false) { | ||
if (desc->className() != "nanoaod::FlatTable") | ||
throw cms::Exception("Configuration", "NanoAODOutputModule can only write out nanoaod::FlatTable objects"); | ||
} | ||
|
||
void defineBranchesFromFirstEvent(const nanoaod::FlatTable &tab); | ||
void branch(TTree &tree); | ||
|
||
/// Fill the current table, if extensions == table.extension(). | ||
/// This parameter is used so that the fill is called first for non-extensions and then for extensions | ||
void fill(const edm::LuminosityBlockForOutput &iLumi, TTree &tree, bool extensions); | ||
|
||
private: | ||
edm::EDGetToken m_token; | ||
std::string m_baseName; | ||
bool m_singleton; | ||
enum { IsMain = 0, IsExtension = 1, DontKnowYetIfMainOrExtension = 2 } m_extension; | ||
std::string m_doc; | ||
UInt_t m_counter; | ||
struct NamedBranchPtr { | ||
std::string name, title, rootTypeCode; | ||
TBranch *branch; | ||
NamedBranchPtr(const std::string &aname, | ||
const std::string &atitle, | ||
const std::string &rootType, | ||
TBranch *branchptr = nullptr) | ||
: name(aname), title(atitle), rootTypeCode(rootType), branch(branchptr) {} | ||
}; | ||
TBranch *m_counterBranch; | ||
std::vector<NamedBranchPtr> m_floatBranches; | ||
std::vector<NamedBranchPtr> m_intBranches; | ||
std::vector<NamedBranchPtr> m_uint8Branches; | ||
bool m_branchesBooked; | ||
|
||
template <typename T> | ||
void fillColumn(NamedBranchPtr &pair, const nanoaod::FlatTable &tab) { | ||
int idx = tab.columnIndex(pair.name); | ||
if (idx == -1) | ||
throw cms::Exception("LogicError", "Missing column in input for " + m_baseName + "_" + pair.name); | ||
pair.branch->SetAddress(const_cast<T *>(&tab.columnData<T>(idx).front())); // SetAddress should take a const * ! | ||
} | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.