diff --git a/RecoTauTag/HLTProducers/src/L2TauTagNNProducer.cc b/RecoTauTag/HLTProducers/src/L2TauTagNNProducer.cc index 4a0996aab2361..2fe15fd097279 100644 --- a/RecoTauTag/HLTProducers/src/L2TauTagNNProducer.cc +++ b/RecoTauTag/HLTProducers/src/L2TauTagNNProducer.cc @@ -731,14 +731,16 @@ void L2TauNNProducer::fillPatatracks(tensorflow::Tensor& cellGridMatrix, } std::vector L2TauNNProducer::getTauScore(const tensorflow::Tensor& cellGridMatrix) { - std::vector pred_tensor; - tensorflow::run(L2cacheData_->session, {{inputTensorName_, cellGridMatrix}}, {outputTensorName_}, &pred_tensor); const int nTau = cellGridMatrix.shape().dim_size(0); std::vector pred_vector(nTau); - for (int tau_idx = 0; tau_idx < nTau; ++tau_idx) { - pred_vector[tau_idx] = pred_tensor[0].matrix()(tau_idx, 0); + if (nTau > 0) { + // Only run the inference if there are taus to process + std::vector pred_tensor; + tensorflow::run(L2cacheData_->session, {{inputTensorName_, cellGridMatrix}}, {outputTensorName_}, &pred_tensor); + for (int tau_idx = 0; tau_idx < nTau; ++tau_idx) { + pred_vector[tau_idx] = pred_tensor[0].matrix()(tau_idx, 0); + } } - return pred_vector; } diff --git a/RecoTauTag/RecoTau/plugins/DeepTauId.cc b/RecoTauTag/RecoTau/plugins/DeepTauId.cc index 24d2f5eedd0a3..dcb264a1647e0 100644 --- a/RecoTauTag/RecoTau/plugins/DeepTauId.cc +++ b/RecoTauTag/RecoTau/plugins/DeepTauId.cc @@ -530,6 +530,7 @@ class DeepTauId : public DeepTauIdBase { {"outer_all_dropout_4/Identity"}, &pred_vector); } + return pred_vector.at(0); } @@ -547,67 +548,77 @@ class DeepTauId : public DeepTauIdBase { bool is_inner) { if (debug_level >= 2) { std::cout << ":" << std::endl; + std::cout << "number of valid cells = " << grid.num_valid_cells() << std::endl; } + + const size_t n_valid_cells = grid.num_valid_cells(); + tensorflow::Tensor predTensor; tensorflow::Tensor& convTensor = *convTensor_.at(is_inner); - eGammaTensor_[is_inner] = std::make_unique( - tensorflow::DT_FLOAT, - tensorflow::TensorShape{ - (long long int)grid.num_valid_cells(), 1, 1, dnn_inputs_v2::EgammaBlockInputs::NumberOfInputs}); - muonTensor_[is_inner] = std::make_unique( - tensorflow::DT_FLOAT, - tensorflow::TensorShape{ - (long long int)grid.num_valid_cells(), 1, 1, dnn_inputs_v2::MuonBlockInputs::NumberOfInputs}); - hadronsTensor_[is_inner] = std::make_unique( - tensorflow::DT_FLOAT, - tensorflow::TensorShape{ - (long long int)grid.num_valid_cells(), 1, 1, dnn_inputs_v2::HadronBlockInputs::NumberOfInputs}); - - eGammaTensor_[is_inner]->flat().setZero(); - muonTensor_[is_inner]->flat().setZero(); - hadronsTensor_[is_inner]->flat().setZero(); - unsigned idx = 0; - for (int eta = -grid.maxEtaIndex(); eta <= grid.maxEtaIndex(); ++eta) { - for (int phi = -grid.maxPhiIndex(); phi <= grid.maxPhiIndex(); ++phi) { - if (debug_level >= 2) { - std::cout << "processing ( eta = " << eta << ", phi = " << phi << " )" << std::endl; - } - const CellIndex cell_index{eta, phi}; - const auto cell_iter = grid.find(cell_index); - if (cell_iter != grid.end()) { + //check if at least one input is there to + //avoid calling TF with empty grid #TODO understand why the grid is empty + if (n_valid_cells > 0) { + eGammaTensor_[is_inner] = std::make_unique( + tensorflow::DT_FLOAT, + tensorflow::TensorShape{ + (long long int)grid.num_valid_cells(), 1, 1, dnn_inputs_v2::EgammaBlockInputs::NumberOfInputs}); + muonTensor_[is_inner] = std::make_unique( + tensorflow::DT_FLOAT, + tensorflow::TensorShape{ + (long long int)grid.num_valid_cells(), 1, 1, dnn_inputs_v2::MuonBlockInputs::NumberOfInputs}); + hadronsTensor_[is_inner] = std::make_unique( + tensorflow::DT_FLOAT, + tensorflow::TensorShape{ + (long long int)grid.num_valid_cells(), 1, 1, dnn_inputs_v2::HadronBlockInputs::NumberOfInputs}); + + eGammaTensor_[is_inner]->flat().setZero(); + muonTensor_[is_inner]->flat().setZero(); + hadronsTensor_[is_inner]->flat().setZero(); + + unsigned idx = 0; + for (int eta = -grid.maxEtaIndex(); eta <= grid.maxEtaIndex(); ++eta) { + for (int phi = -grid.maxPhiIndex(); phi <= grid.maxPhiIndex(); ++phi) { if (debug_level >= 2) { - std::cout << " creating inputs for ( eta = " << eta << ", phi = " << phi << " ): idx = " << idx - << std::endl; + std::cout << "processing ( eta = " << eta << ", phi = " << phi << " )" << std::endl; } - const Cell& cell = cell_iter->second; - createEgammaBlockInputs(idx, - tau, - tau_index, - tau_ref, - pv, - rho, - electrons, - pfCands, - cell, - tau_funcs, - is_inner, - *eGammaTensor_[is_inner]); - createMuonBlockInputs( - idx, tau, tau_index, tau_ref, pv, rho, muons, pfCands, cell, tau_funcs, is_inner, *muonTensor_[is_inner]); - createHadronsBlockInputs( - idx, tau, tau_index, tau_ref, pv, rho, pfCands, cell, tau_funcs, is_inner, *hadronsTensor_[is_inner]); - idx += 1; - } else { - if (debug_level >= 2) { - std::cout << " skipping creation of inputs, because ( eta = " << eta << ", phi = " << phi - << " ) is not in the grid !!" << std::endl; + const CellIndex cell_index{eta, phi}; + const auto cell_iter = grid.find(cell_index); + if (cell_iter != grid.end()) { + if (debug_level >= 2) { + std::cout << " creating inputs for ( eta = " << eta << ", phi = " << phi << " ): idx = " << idx + << std::endl; + } + const Cell& cell = cell_iter->second; + createEgammaBlockInputs(idx, + tau, + tau_index, + tau_ref, + pv, + rho, + electrons, + pfCands, + cell, + tau_funcs, + is_inner, + *eGammaTensor_[is_inner]); + createMuonBlockInputs( + idx, tau, tau_index, tau_ref, pv, rho, muons, pfCands, cell, tau_funcs, is_inner, *muonTensor_[is_inner]); + createHadronsBlockInputs( + idx, tau, tau_index, tau_ref, pv, rho, pfCands, cell, tau_funcs, is_inner, *hadronsTensor_[is_inner]); + idx += 1; + } else { + if (debug_level >= 2) { + std::cout << " skipping creation of inputs, because ( eta = " << eta << ", phi = " << phi + << " ) is not in the grid !!" << std::endl; + } } } } + // Calling TF prediction only if n_valid_cells > 0 + predTensor = getPartialPredictions(is_inner); } - const auto predTensor = getPartialPredictions(is_inner); - idx = 0; + unsigned idx = 0; for (int eta = -grid.maxEtaIndex(); eta <= grid.maxEtaIndex(); ++eta) { for (int phi = -grid.maxPhiIndex(); phi <= grid.maxPhiIndex(); ++phi) { const CellIndex cell_index{eta, phi};