18 bool simple_value =
false;
21 value_ = std::stod(cut_str, &pos);
22 simple_value = (pos == cut_str.size());
23 }
catch (std::invalid_argument&) {
24 }
catch (std::out_of_range&) {
28 "[&](double *x, double *p) { const int decayMode = p[0];"
29 "const double pt = p[1]; const double eta = p[2];";
30 static const int n_params = 3;
31 static const auto handler = [](int, Bool_t,
const char*,
const char*) ->
void {};
34 if (cut_str.find(
"return") == std::string::npos)
35 fn_str +=
" return " + cut_str +
";}";
37 fn_str += cut_str +
"}";
38 auto old_handler = SetErrorHandler(handler);
39 fn_ = std::make_unique<TF1>(
"fn_", fn_str.c_str(), 0, 1, n_params);
40 SetErrorHandler(old_handler);
42 throw cms::Exception(
"TauWPThreshold: invalid formula") <<
"Invalid WP cut formula = '" << cut_str <<
"'.";
52 fn_->SetParameter(0, dynamic_cast<const reco::PFTau&>(tau).decayMode());
54 fn_->SetParameter(0, dynamic_cast<const pat::Tau&>(tau).decayMode());
55 fn_->SetParameter(1, tau.
pt());
56 fn_->SetParameter(2, tau.
eta());
61 const tensorflow::Tensor& pred,
64 std::vector<reco::SingleTauDiscriminatorContainer> outputbuffer(taus->size());
66 for (
size_t tau_index = 0; tau_index < taus->size(); ++tau_index) {
68 for (
size_t num_elem :
num_)
69 x += pred.matrix<
float>()(tau_index, num_elem);
70 if (x != 0 && !
den_.empty()) {
72 for (
size_t den_elem :
den_)
73 den_val += pred.matrix<
float>()(tau_index, den_elem);
76 outputbuffer[tau_index].rawValues.push_back(x);
78 for (
const auto& wp : *working_points) {
79 const bool pass = x > (*wp)(taus->at(tau_index),
is_online);
80 outputbuffer[tau_index].workingPoints.push_back(pass);
84 std::unique_ptr<TauDiscriminator>
output = std::make_unique<TauDiscriminator>();
86 filler.insert(taus, outputbuffer.begin(), outputbuffer.end());
97 is_online_(cfg.getParameter<bool>(
"is_online")),
100 for (
const auto& output_desc :
outputs_) {
101 produces<TauDiscriminator>(output_desc.first);
102 const auto& cut_list = cfg.
getParameter<std::vector<std::string>>(output_desc.first +
"WP");
104 workingPoints_[output_desc.first].push_back(std::make_unique<Cutter>(cut_str));
115 transform(pdBoolOperator.begin(), pdBoolOperator.end(), pdBoolOperator.begin(), ::tolower);
117 if (pdBoolOperator ==
"and") {
119 }
else if (pdBoolOperator ==
"or") {
123 <<
"PrediscriminantBooleanOperator defined incorrectly, options are: AND,OR";
127 std::vector<std::string> prediscriminantsNames =
130 for (
auto const& iDisc : prediscriminantsNames) {
138 thisDiscriminator.
cut =
cut;
139 thisDiscriminator.
disc_token = consumes<reco::PFTauDiscriminator>(
label);
144 thisDiscriminator.
cut =
cut;
145 thisDiscriminator.
disc_token = consumes<pat::PATTauDiscriminator>(
label);
157 size_t nPrediscriminants =
159 for (
size_t iDisc = 0; iDisc < nPrediscriminants; ++iDisc) {
171 if (tauProductID != discKeyId) {
173 <<
"The tau collection has product ID: " << tauProductID
174 <<
" but the pre-discriminator is keyed with product ID: " << discKeyId << std::endl;
183 for (
const auto& output_desc :
outputs_) {
188 auto result = output_desc.second.get_value(taus, pred, working_points,
is_online_);
194 const auto graph_name_vector = cfg.
getParameter<std::vector<std::string>>(
"graph_file");
195 std::map<std::string, std::string> graph_names;
196 for (
const auto&
entry : graph_name_vector) {
197 const size_t sep_pos =
entry.find(
':');
199 if (sep_pos != std::string::npos) {
200 entry_name =
entry.substr(0, sep_pos);
201 graph_file =
entry.substr(sep_pos + 1);
207 if (graph_names.count(entry_name))
208 throw cms::Exception(
"DeepTauCache") <<
"Duplicated graph entries";
212 return std::make_unique<DeepTauCache>(graph_names,
mem_mapped);
216 for (
const auto& graph_entry : graph_names) {
217 tensorflow::SessionOptions
options;
223 memmappedEnv_[entry_name] = std::make_unique<tensorflow::MemmappedEnv>(tensorflow::Env::Default());
225 if (!mmap_status.ok()) {
226 throw cms::Exception(
"DeepTauCache: unable to initalize memmapped environment for ")
227 << graph_file <<
". \n"
228 << mmap_status.ToString();
231 graphs_[entry_name] = std::make_unique<tensorflow::GraphDef>();
234 tensorflow::MemmappedFileSystem::kMemmappedPackageDefaultGraphDef,
236 if (!load_graph_status.ok())
237 throw cms::Exception(
"DeepTauCache: unable to load graph from ") << graph_file <<
". \n"
238 << load_graph_status.ToString();
240 options.config.mutable_graph_options()->mutable_optimizer_options()->set_opt_level(
241 ::tensorflow::OptimizerOptions::L0);
Session * createSession(SessionOptions &sessionOptions)
double pt() const final
transverse momentum
DeepTauCache(const std::map< std::string, std::string > &graph_names, bool mem_mapped)
std::unique_ptr< TF1 > fn_
std::map< std::string, tensorflow::Session * > sessions_
edm::EDGetTokenT< TauCollection > tausToken_
dictionary working_points
GraphDef * loadGraphDef(const std::string &pbFile)
double operator()(const reco::BaseTau &tau, bool isPFTau) const
const DeepTauCache * cache_
std::vector< size_t > num_
std::vector< Vertex > VertexCollection
std::vector< size_t > den_
std::map< std::string, Output > OutputCollection
std::vector< CutterPtr > WPList
void produce(edm::Event &event, const edm::EventSetup &es) override
std::unique_ptr< TauDiscriminator > get_value(const edm::Handle< TauCollection > &taus, const tensorflow::Tensor &pred, const WPList *working_points, bool is_online) const
OutputCollection outputs_
std::vector< std::string > getParameterNamesForType(bool trackiness=true) const
void setThreading(SessionOptions &sessionOptions, int nThreads=1)
edm::EDGetTokenT< ConsumeType > disc_token
TauWPThreshold(const std::string &cut_str)
bool closeSession(Session *&session)
DeepTauBase(const edm::ParameterSet &cfg, const OutputCollection &outputs, const DeepTauCache *cache)
std::map< std::string, WPList > workingPoints_
virtual tensorflow::Tensor getPredictions(edm::Event &event, edm::Handle< TauCollection > taus)=0
virtual void createOutputs(edm::Event &event, const tensorflow::Tensor &pred, edm::Handle< TauCollection > taus)
uint8_t andPrediscriminants_
T getParameter(std::string const &) const
static std::unique_ptr< DeepTauCache > initializeGlobalCache(const edm::ParameterSet &cfg)
std::map< std::string, GraphPtr > graphs_
std::string fullPath() const
std::map< std::string, std::unique_ptr< tensorflow::MemmappedEnv > > memmappedEnv_
std::vector< TauDiscInfo< pat::PATTauDiscriminator > > patPrediscriminants_
edm::EDGetTokenT< CandidateCollection > pfcandToken_
std::vector< TauDiscInfo< reco::PFTauDiscriminator > > recoPrediscriminants_
double eta() const final
momentum pseudorapidity
edm::EDGetTokenT< reco::VertexCollection > vtxToken_