17 bool simple_value =
false;
20 value_ = std::stod(cut_str, &pos);
21 simple_value = (pos == cut_str.size());
22 }
catch(std::invalid_argument&) {
23 }
catch(std::out_of_range&) {
26 static const std::string prefix =
"[&](double *x, double *p) { const int decayMode = p[0];" 27 "const double pt = p[1]; const double eta = p[2];";
28 static const int n_params = 3;
29 static const auto handler = [](
int, Bool_t,
const char*,
const char*) ->
void {};
32 auto old_handler = SetErrorHandler(handler);
33 fn_ = std::make_unique<TF1>(
"fn_", fn_str.c_str(), 0, 1, n_params);
34 SetErrorHandler(old_handler);
36 throw cms::Exception(
"TauWPThreshold: invalid formula") <<
"Invalid WP cut formula = '" << cut_str <<
"'.";
45 fn_->SetParameter(1, tau.
pt());
46 fn_->SetParameter(2, tau.
eta());
51 const tensorflow::Tensor& pred,
52 const WPMap& working_points)
const 55 output[
""] = std::make_unique<TauDiscriminator>(
TauRefProd(taus));
56 for(
const auto& wp : working_points)
57 output[wp.first] = std::make_unique<TauDiscriminator>(
TauRefProd(taus));
59 for(
size_t tau_index = 0; tau_index < taus->size(); ++tau_index) {
61 for(
size_t num_elem : num_)
62 x += pred.matrix<
float>()(tau_index, num_elem);
63 if(x != 0 && !den_.empty()) {
65 for(
size_t den_elem : den_)
66 den_val += pred.matrix<
float>()(tau_index, den_elem);
69 output[
""]->setValue(tau_index, x);
70 for(
const auto& wp : working_points) {
71 const auto&
tau = taus->at(tau_index);
72 const bool pass = x > (*wp.second)(
tau);
73 output[wp.first]->setValue(tau_index, pass);
81 tausToken_(consumes<
TauCollection>(cfg.getParameter<
edm::InputTag>(
"taus"))),
84 outputs_(outputCollection),
87 for(
const auto& output_desc :
outputs_) {
88 produces<TauDiscriminator>(output_desc.first);
90 for(
const std::string& wp_name : cut_pset.getParameterNames()) {
92 workingPoints_[output_desc.first][wp_name] = std::make_unique<Cutter>(cut_str);
93 produces<TauDiscriminator>(output_desc.first + wp_name);
109 for(
const auto& output_desc :
outputs_) {
110 auto result_map = output_desc.second.get_value(taus, pred,
workingPoints_.at(output_desc.first));
111 for(
auto&
result : result_map)
118 const auto graph_name_vector = cfg.
getParameter<std::vector<std::string>>(
"graph_file");
119 std::map<std::string, std::string> graph_names;
120 for(
const auto&
entry : graph_name_vector) {
121 const size_t sep_pos =
entry.find(
':');
123 if(sep_pos != std::string::npos) {
124 entry_name =
entry.substr(0, sep_pos);
125 graph_file =
entry.substr(sep_pos + 1);
131 if(graph_names.count(entry_name))
132 throw cms::Exception(
"DeepTauCache") <<
"Duplicated graph entries";
133 graph_names[entry_name] = graph_file;
136 return std::make_unique<DeepTauCache>(graph_names, mem_mapped);
141 for(
const auto& graph_entry : graph_names) {
142 tensorflow::SessionOptions
options;
146 const std::string& graph_file = graph_entry.second;
149 const tensorflow::Status mmap_status = memmappedEnv_.at(entry_name)->InitializeFromFile(graph_file);
150 if(!mmap_status.ok()) {
151 throw cms::Exception(
"DeepTauCache: unable to initalize memmapped environment for ")
152 << graph_file <<
". \n" << mmap_status.ToString();
155 graphs_[entry_name] = std::make_unique<tensorflow::GraphDef>();
156 const tensorflow::Status load_graph_status = ReadBinaryProto(memmappedEnv_.at(entry_name).get(),
157 tensorflow::MemmappedFileSystem::kMemmappedPackageDefaultGraphDef,
158 graphs_.at(entry_name).get());
159 if(!load_graph_status.ok())
160 throw cms::Exception(
"DeepTauCache: unable to load graph from ") << graph_file <<
". \n" 161 << mmap_status.ToString();
162 options.config.mutable_graph_options()->mutable_optimizer_options()->set_opt_level(::tensorflow::OptimizerOptions::L0);
163 options.env = memmappedEnv_.at(entry_name).get();
176 for(
auto& session_entry : sessions_)
Session * createSession(SessionOptions &sessionOptions)
T getParameter(std::string const &) const
edm::RefProd< TauCollection > TauRefProd
double eta() const final
momentum pseudorapidity
DeepTauCache(const std::map< std::string, std::string > &graph_names, bool mem_mapped)
std::unique_ptr< TF1 > fn_
edm::EDGetTokenT< TauCollection > tausToken_
GraphDef * loadGraphDef(const std::string &pbFile)
std::vector< pat::PackedCandidate > PackedCandidateCollection
std::map< std::string, CutterPtr > WPMap
double pt() const final
transverse momentum
std::vector< Vertex > VertexCollection
collection of Vertex objects
std::map< std::string, Output > OutputCollection
void produce(edm::Event &event, const edm::EventSetup &es) override
OutputCollection outputs_
std::map< std::string, std::unique_ptr< TauDiscriminator >> ResultMap
TauWPThreshold(const std::string &cut_str)
bool closeSession(Session *&session)
virtual tensorflow::Tensor getPredictions(edm::Event &event, const edm::EventSetup &es, edm::Handle< TauCollection > taus)=0
std::vector< TauType > TauCollection
DeepTauBase(const edm::ParameterSet &cfg, const OutputCollection &outputs, const DeepTauCache *cache)
Analysis-level tau class.
virtual void createOutputs(edm::Event &event, const tensorflow::Tensor &pred, edm::Handle< TauCollection > taus)
void setThreading(SessionOptions &sessionOptions, int nThreads, const std::string &singleThreadPool="no_threads")
int decayMode() const
reconstructed tau decay mode (specific to PFTau)
double operator()(const pat::Tau &tau) const
static std::unique_ptr< DeepTauCache > initializeGlobalCache(const edm::ParameterSet &cfg)
ResultMap get_value(const edm::Handle< TauCollection > &taus, const tensorflow::Tensor &pred, const WPMap &working_points) const
std::string fullPath() const
std::map< std::string, WPMap > workingPoints_