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);
80 tausToken_(consumes<
TauCollection>(cfg.getParameter<
edm::InputTag>(
"taus"))),
81 outputs_(outputCollection),
84 for(
const auto& output_desc :
outputs_) {
85 produces<TauDiscriminator>(output_desc.first);
87 for(
const std::string& wp_name : cut_pset.getParameterNames()) {
89 workingPoints_[output_desc.first][wp_name] = std::make_unique<Cutter>(cut_str);
90 produces<TauDiscriminator>(output_desc.first + wp_name);
106 for(
const auto& output_desc :
outputs_) {
107 auto result_map = output_desc.second.get_value(taus, pred,
workingPoints_.at(output_desc.first));
108 for(
auto&
result : result_map)
117 return std::make_unique<DeepTauCache>(graph_name, mem_mapped);
122 tensorflow::SessionOptions
options;
127 const tensorflow::Status mmap_status = memmappedEnv_.get()->InitializeFromFile(graph_name);
128 if(!mmap_status.ok())
129 throw cms::Exception(
"DeepTauCache: unable to initalize memmapped environment for ") << graph_name <<
". \n" 130 << mmap_status.ToString();
132 graph_ = std::make_unique<tensorflow::GraphDef>();
134 tensorflow::MemmappedFileSystem::kMemmappedPackageDefaultGraphDef,
136 if(!load_graph_status.ok())
137 throw cms::Exception(
"DeepTauCache: unable to load graph_ from ") << graph_name <<
". \n" 138 << mmap_status.ToString();
139 options.config.mutable_graph_options()->mutable_optimizer_options()->set_opt_level(::tensorflow::OptimizerOptions::L0);
140 options.env = memmappedEnv_.get();
Session * createSession(SessionOptions &sessionOptions)
T getParameter(std::string const &) const
edm::RefProd< TauCollection > TauRefProd
double eta() const final
momentum pseudorapidity
std::unique_ptr< TF1 > fn_
edm::EDGetTokenT< TauCollection > tausToken_
GraphDef * loadGraphDef(const std::string &pbFile)
std::map< std::string, CutterPtr > WPMap
double pt() const final
transverse momentum
DeepTauCache(const std::string &graph_name, bool mem_mapped)
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_