17 using GraphPtr = std::shared_ptr<tensorflow::GraphDef>;
20 for (
const auto& graph_entry : graph_names) {
29 if (!mmap_status.ok()) {
30 throw cms::Exception(
"DeepTauCache: unable to initalize memmapped environment for ")
32 << mmap_status.ToString();
35 graphs_[entry_name] = std::make_unique<tensorflow::GraphDef>();
38 tensorflow::MemmappedFileSystem::kMemmappedPackageDefaultGraphDef,
40 if (!load_graph_status.ok())
42 << load_graph_status.ToString();
44 options.getSessionOptions().config.mutable_graph_options()->mutable_optimizer_options()->set_opt_level(
45 ::tensorflow::OptimizerOptions::L0);
68 std::map<std::string, tensorflow::Session*>
sessions_;
69 std::map<std::string, std::unique_ptr<tensorflow::MemmappedEnv>>
memmappedEnv_;
83 desc.add<std::vector<std::string>>(
"graph_file",
84 {
"RecoTauTag/TrainingFiles/data/DeepTauId/deepTau_2017v2p6_e6.pb"});
85 descriptions.
add(
"DeepTau",
desc);
95 tensorflow::DT_FLOAT, tensorflow::TensorShape{1, TauBlockInputs::NumberOfInputs});
99 tensorflow::TensorShape{1,
100 static_cast<int>(TauBlockInputs::NumberOfInputs) -
101 static_cast<int>(TauBlockInputs::varsToDrop.
size())});
104 for (
size_t n = 0;
n < 2; ++
n) {
105 const bool is_inner =
n == 0;
106 const auto n_cells = is_inner ? number_of_inner_cell : number_of_outer_cell;
107 eGammaTensor_[is_inner] = std::make_unique<tensorflow::Tensor>(
108 tensorflow::DT_FLOAT, tensorflow::TensorShape{1, 1, 1, EgammaBlockInputs::NumberOfInputs});
109 muonTensor_[is_inner] = std::make_unique<tensorflow::Tensor>(
110 tensorflow::DT_FLOAT, tensorflow::TensorShape{1, 1, 1, MuonBlockInputs::NumberOfInputs});
112 tensorflow::DT_FLOAT, tensorflow::TensorShape{1, 1, 1, HadronBlockInputs::NumberOfInputs});
113 convTensor_[is_inner] = std::make_unique<tensorflow::Tensor>(
114 tensorflow::DT_FLOAT, tensorflow::TensorShape{1, n_cells, n_cells, number_of_conv_features});
116 tensorflow::DT_FLOAT, tensorflow::TensorShape{1, 1, 1, number_of_conv_features});
145 const auto graph_name_vector =
cfg.getParameter<std::vector<std::string>>(
"graph_file");
146 std::map<std::string, std::string> graph_names;
147 for (
const auto&
entry : graph_name_vector) {
148 const size_t sep_pos =
entry.find(
':');
150 if (sep_pos != std::string::npos) {
151 entry_name =
entry.substr(0, sep_pos);
158 if (graph_names.count(entry_name))
159 throw cms::Exception(
"DeepTauCache") <<
"Duplicated graph entries";
163 return std::make_unique<deep_tau::DeepTauCache>(graph_names,
mem_mapped);
172 const CellGrid*
grid =
nullptr)
const {
174 std::cout <<
"<checkInputs>: block_name = " << block_name << std::endl;
175 if (block_name ==
"input_tau") {
176 for (
int input_index = 0; input_index < n_inputs; ++input_index) {
177 float input =
inputs.matrix<
float>()(0, input_index);
180 <<
"in the " << block_name
181 <<
", input is not finite, i.e. infinite or NaN, for input_index = " << input_index;
191 if (block_name.find(
"input_inner") != std::string::npos) {
194 }
else if (block_name.find(
"input_outer") != std::string::npos) {
199 int eta_phi_index = 0;
200 for (
int eta = -n_eta;
eta <= n_eta; ++
eta) {
201 for (
int phi = -n_phi;
phi <= n_phi; ++
phi) {
202 const CellIndex cell_index{
eta,
phi};
203 const auto cell_iter =
grid->find(cell_index);
204 if (cell_iter !=
grid->end()) {
205 for (
int input_index = 0; input_index < n_inputs; ++input_index) {
209 <<
"in the " << block_name <<
", input is not finite, i.e. infinite or NaN, for eta = " <<
eta 210 <<
", phi = " <<
phi <<
", input_index = " << input_index;
213 std::cout << block_name <<
"[eta = " <<
eta <<
"][phi = " <<
phi <<
"][var = " << input_index
228 const CellGrid*
grid =
nullptr) {
230 std::cout <<
"<saveInputs>: block_name = " << block_name << std::endl;
233 (*json_file_) <<
", ";
234 (*json_file_) <<
"\"" << block_name <<
"\": [";
235 if (block_name ==
"input_tau") {
236 for (
int input_index = 0; input_index < n_inputs; ++input_index) {
237 float input =
inputs.matrix<
float>()(0, input_index);
238 if (input_index != 0)
239 (*json_file_) <<
", ";
240 (*json_file_) <<
input;
245 if (block_name.find(
"input_inner") != std::string::npos) {
248 }
else if (block_name.find(
"input_outer") != std::string::npos) {
253 int eta_phi_index = 0;
254 for (
int eta = -n_eta;
eta <= n_eta; ++
eta) {
256 (*json_file_) <<
", ";
257 (*json_file_) <<
"[";
258 for (
int phi = -n_phi;
phi <= n_phi; ++
phi) {
260 (*json_file_) <<
", ";
261 (*json_file_) <<
"[";
262 const CellIndex cell_index{
eta,
phi};
263 const auto cell_iter =
grid->find(cell_index);
264 for (
int input_index = 0; input_index < n_inputs; ++input_index) {
266 if (cell_iter !=
grid->end()) {
269 if (input_index != 0)
270 (*json_file_) <<
", ";
271 (*json_file_) <<
input;
273 if (cell_iter !=
grid->end()) {
276 (*json_file_) <<
"]";
278 (*json_file_) <<
"]";
281 (*json_file_) <<
"]";
288 const std::vector<pat::Electron> electron_collection_default;
289 const std::vector<pat::Muon> muon_collection_default;
293 pfTauTransverseImpactParameters_default;
295 const std::vector<pat::Electron>* electron_collection;
296 const std::vector<pat::Muon>* muon_collection;
309 electron_collection = &electron_collection_default;
310 muon_collection = &muon_collection_default;
340 auto const& eventnr =
event.id().event();
344 for (
size_t tau_index = 0; tau_index <
taus->size(); ++tau_index) {
347 std::vector<tensorflow::Tensor> pred_vector;
349 bool passesPrediscriminants;
351 passesPrediscriminants = tauIDs.passPrediscriminants<std::vector<TauDiscInfo<reco::PFTauDiscriminator>>>(
354 passesPrediscriminants = tauIDs.passPrediscriminants<std::vector<TauDiscInfo<pat::PATTauDiscriminator>>>(
358 if (passesPrediscriminants) {
361 getPredictionsV2<reco::PFCandidate, reco::PFTau>(
taus->at(tau_index),
373 getPredictionsV2<pat::PackedCandidate, pat::Tau>(
taus->at(tau_index),
389 const float pred = pred_vector[0].flat<
float>()(
k);
390 if (!(pred >= 0 && pred <= 1))
392 <<
"invalid prediction = " << pred <<
" for tau_index = " << tau_index <<
", pred_index = " <<
k;
393 predictions.matrix<
float>()(tau_index,
k) = pred;
402 predictions.matrix<
float>()(tau_index,
k) = (
k == 2) ? -1.
f : 2.
f;
409 template <
typename Cand
idateCastType,
typename TauCastType>
411 const size_t tau_index,
413 const std::vector<pat::Electron>*
electrons,
414 const std::vector<pat::Muon>*
muons,
419 std::vector<tensorflow::Tensor>& pred_vector,
423 std::cout <<
"<DeepTauId::getPredictionsV2 (moduleLabel = " << moduleDescription().moduleLabel()
424 <<
")>:" << std::endl;
425 std::cout <<
" tau: pT = " <<
tau.pt() <<
", eta = " <<
tau.eta() <<
", phi = " <<
tau.phi()
426 <<
", eventnr = " << eventnr << std::endl;
431 fillGrids(dynamic_cast<const TauCastType&>(
tau), *
muons, inner_grid, outer_grid);
432 fillGrids(dynamic_cast<const TauCastType&>(
tau), pfCands, inner_grid, outer_grid);
435 createTauBlockInputs<CandidateCastType>(
438 createConvFeatures<CandidateCastType>(
dynamic_cast<const TauCastType&
>(
tau),
452 createConvFeatures<CandidateCastType>(
dynamic_cast<const TauCastType&
>(
tau),
469 json_file_ =
new std::ofstream(json_file_name.data());
471 (*json_file_) <<
"{";
479 (*json_file_) <<
"}";
488 {
"main_output/Softmax"},
513 std::vector<tensorflow::Tensor> pred_vector;
521 {
"inner_all_dropout_4/Identity"},
530 {
"outer_all_dropout_4/Identity"},
533 return pred_vector.at(0);
536 template <
typename Cand
idateCastType,
typename TauCastType>
538 const size_t tau_index,
542 const std::vector<pat::Electron>*
electrons,
543 const std::vector<pat::Muon>*
muons,
545 const CellGrid&
grid,
549 std::cout <<
"<DeepTauId::createConvFeatures (is_inner = " << is_inner <<
")>:" << std::endl;
551 tensorflow::Tensor& convTensor = *
convTensor_.at(is_inner);
552 eGammaTensor_[is_inner] = std::make_unique<tensorflow::Tensor>(
553 tensorflow::DT_FLOAT,
554 tensorflow::TensorShape{
555 (
long long int)
grid.num_valid_cells(), 1, 1, dnn_inputs_v2::EgammaBlockInputs::NumberOfInputs});
556 muonTensor_[is_inner] = std::make_unique<tensorflow::Tensor>(
557 tensorflow::DT_FLOAT,
558 tensorflow::TensorShape{
559 (
long long int)
grid.num_valid_cells(), 1, 1, dnn_inputs_v2::MuonBlockInputs::NumberOfInputs});
561 tensorflow::DT_FLOAT,
562 tensorflow::TensorShape{
563 (
long long int)
grid.num_valid_cells(), 1, 1, dnn_inputs_v2::HadronBlockInputs::NumberOfInputs});
573 std::cout <<
"processing ( eta = " <<
eta <<
", phi = " <<
phi <<
" )" << std::endl;
575 const CellIndex cell_index{
eta,
phi};
576 const auto cell_iter =
grid.find(cell_index);
577 if (cell_iter !=
grid.end()) {
579 std::cout <<
" creating inputs for ( eta = " <<
eta <<
", phi = " <<
phi <<
" ): idx = " <<
idx 582 const Cell& cell = cell_iter->second;
583 createEgammaBlockInputs<CandidateCastType>(
idx,
595 createMuonBlockInputs<CandidateCastType>(
596 idx,
tau, tau_index, tau_ref,
pv,
rho,
muons, pfCands, cell, tau_funcs, is_inner, *
muonTensor_[is_inner]);
597 createHadronsBlockInputs<CandidateCastType>(
598 idx,
tau, tau_index, tau_ref,
pv,
rho, pfCands, cell, tau_funcs, is_inner, *
hadronsTensor_[is_inner]);
602 std::cout <<
" skipping creation of inputs, because ( eta = " <<
eta <<
", phi = " <<
phi 603 <<
" ) is not in the grid !!" << std::endl;
613 const CellIndex cell_index{
eta,
phi};
614 const int eta_index =
grid.getEtaTensorIndex(cell_index);
615 const int phi_index =
grid.getPhiTensorIndex(cell_index);
617 const auto cell_iter =
grid.find(cell_index);
618 if (cell_iter !=
grid.end()) {
633 for (
int n = 0;
n < dnn_inputs_v2::number_of_conv_features; ++
n) {
634 convTensor.tensor<
float, 4>()(0, eta_index, phi_index,
n) =
features.tensor<
float, 4>()(batch_idx, 0, 0,
n);
void createConvFeatures(const TauCastType &tau, const size_t tau_index, const edm::RefToBase< reco::BaseTau > tau_ref, const reco::Vertex &pv, double rho, const std::vector< pat::Electron > *electrons, const std::vector< pat::Muon > *muons, const edm::View< reco::Candidate > &pfCands, const CellGrid &grid, TauFunc tau_funcs, bool is_inner)
edm::EDGetTokenT< std::vector< pat::Electron > > electrons_token_
std::vector< TauDiscInfo< pat::PATTauDiscriminator > > patPrediscriminants_
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
tensorflow::Tensor getPredictions(edm::Event &event, edm::Handle< TauCollection > taus)
std::array< std::unique_ptr< tensorflow::Tensor >, 2 > hadronsTensor_
static std::unique_ptr< deep_tau::DeepTauCache > initializeGlobalCache(const edm::ParameterSet &cfg)
edm::EDGetTokenT< double > rho_token_
DeepTauCache(const std::map< std::string, std::string > &graph_names, bool mem_mapped)
std::string fullPath() const
constexpr bool isNotFinite(T x)
std::array< std::unique_ptr< tensorflow::Tensor >, 2 > zeroOutputTensor_
std::map< std::string, tensorflow::Session * > sessions_
GraphDef * loadGraphDef(const std::string &pbFile)
std::map< BasicDiscriminator, size_t > basicDiscrIndexMap_
const std::vector< BasicDiscriminator > requiredBasicDiscriminators_
unsigned long long EventNumber_t
void setCellConvFeatures(tensorflow::Tensor &convTensor, const tensorflow::Tensor &features, unsigned batch_idx, int eta_index, int phi_index)
pfTauTransverseImpactParameters
void getPredictionsV2(TauCollection::const_reference &tau, const size_t tau_index, const edm::RefToBase< reco::BaseTau > tau_ref, const std::vector< pat::Electron > *electrons, const std::vector< pat::Muon > *muons, const edm::View< reco::Candidate > &pfCands, const reco::Vertex &pv, double rho, const edm::EventNumber_t &eventnr, std::vector< tensorflow::Tensor > &pred_vector, TauFunc tau_funcs)
muons
the two sets of parameters below are mutually exclusive, depending if RECO or ALCARECO is used the us...
static std::string to_string(const XMLCh *ch)
static std::string const input
std::array< std::unique_ptr< tensorflow::Tensor >, 2 > convTensor_
uint8_t andPrediscriminants_
edm::EDGetTokenT< CandidateCollection > pfcandToken_
DeepTauId(const edm::ParameterSet &cfg, const deep_tau::DeepTauCache *cache)
edm::EDGetTokenT< std::vector< pat::Muon > > muons_token_
const deep_tau::DeepTauCache * cache_
static void fillDescriptionsHelper(edm::ParameterSetDescription &desc)
std::shared_ptr< tensorflow::GraphDef > GraphPtr
const tensorflow::GraphDef & getGraph(const std::string &name="") const
std::vector< float > features(const reco::PreId &ecal, const reco::PreId &hcal, double rho, const reco::BeamSpot &spot, noZS::EcalClusterLazyTools &ecalTools)
edm::EDGetTokenT< reco::TauDiscriminatorContainer > basicTauDiscriminators_inputToken_
edm::EDGetTokenT< TauCollection > tausToken_
std::map< BasicDiscriminator, size_t > basicDiscrdR03IndexMap_
void run(Session *session, const NamedTensorList &inputs, const std::vector< std::string > &outputNames, std::vector< Tensor > *outputs, const thread::ThreadPoolOptions &threadPoolOptions)
void saveInputs(const tensorflow::Tensor &inputs, const std::string &block_name, int n_inputs, const CellGrid *grid=nullptr)
bool closeSession(Session *&session)
const std::vector< BasicDiscriminator > requiredBasicDiscriminatorsdR03_
basicTauDiscriminatorsdR03
#define DEFINE_FWK_MODULE(type)
static void globalEndJob(const deep_tau::DeepTauCache *cache_)
edm::EDGetTokenT< reco::TauDiscriminatorContainer > basicTauDiscriminatorsdR03_inputToken_
void checkInputs(const tensorflow::Tensor &inputs, const std::string &block_name, int n_inputs, const CellGrid *grid=nullptr) const
tensorflow::Session & getSession(const std::string &name="") const
const std::map< BasicDiscriminator, size_t > matchDiscriminatorIndices(edm::Event const &event, edm::EDGetTokenT< reco::TauDiscriminatorContainer > discriminatorContainerToken, std::vector< BasicDiscriminator > requiredDiscr)
void createOutputs(edm::Event &event, const PredType &pred, edm::Handle< TauCollection > taus)
Session * createSession()
tensorflow::Tensor getPartialPredictions(bool is_inner)
std::ofstream * json_file_
const bool disable_CellIndex_workaround_
void loadPrediscriminants(edm::Event const &event, edm::Handle< TauCollection > const &taus)
void add(std::string const &label, ParameterSetDescription const &psetDescription)
void fillGrids(const TauCastType &tau, const Collection &objects, CellGrid &inner_grid, CellGrid &outer_grid)
std::array< std::unique_ptr< tensorflow::Tensor >, 2 > eGammaTensor_
std::map< std::string, GraphPtr > graphs_
const unsigned sub_version_
DeepTauIdWrapper(const edm::ParameterSet &cfg)
edm::EDGetTokenT< reco::VertexCollection > vtxToken_
std::vector< TauDiscInfo< reco::PFTauDiscriminator > > recoPrediscriminants_
std::map< std::string, std::unique_ptr< tensorflow::MemmappedEnv > > memmappedEnv_
std::array< std::unique_ptr< tensorflow::Tensor >, 2 > muonTensor_
void produce(edm::Event &event, const edm::EventSetup &es) override
T const & const_reference
constexpr int NumberOfOutputs
edm::EDGetTokenT< edm::AssociationVector< reco::PFTauRefProd, std::vector< reco::PFTauTransverseImpactParameterRef > > > pfTauTransverseImpactParameters_token_
std::unique_ptr< tensorflow::Tensor > tauBlockTensor_