22 "max_cl_energy",
"max_cl_et",
"max_cl_eta",
"max_cl_phi",
"max_cl_ieta",
"max_cl_iphi",
23 "max_cl_iz",
"max_cl_seed_dEta",
"max_cl_seed_dPhi",
"max_cl_seed_dEnergy",
"max_cl_seed_dEt",
"max_cl_nxtals",
24 "min_cl_energy",
"min_cl_et",
"min_cl_eta",
"min_cl_phi",
"min_cl_ieta",
"min_cl_iphi",
25 "min_cl_iz",
"min_cl_seed_dEta",
"min_cl_seed_dPhi",
"min_cl_seed_dEnergy",
"min_cl_seed_dEt",
"min_cl_nxtals",
26 "avg_cl_energy",
"avg_cl_et",
"avg_cl_eta",
"avg_cl_phi",
"avg_cl_ieta",
"avg_cl_iphi",
27 "avg_cl_iz",
"avg_cl_seed_dEta",
"avg_cl_seed_dPhi",
"avg_cl_seed_dEnergy",
"avg_cl_seed_dEt",
"avg_cl_nxtals"};
38 throw cms::Exception(
"WrongConfiguration") <<
"Mismatch between number of input features for Clusters and " 39 <<
"parameters in the scaler file.";
44 throw cms::Exception(
"WrongConfiguration") <<
"Mismatch between number of input features for Clusters and " 45 <<
"parameters in the scaler file.";
49 throw cms::Exception(
"WrongConfiguration") <<
"Mismatch between number of input features for Clusters and " 50 <<
"parameters in the scaler file.";
61 LogDebug(
"DeepSCGraphEvaluation") <<
"Loading graph";
64 LogDebug(
"DeepSCGraphEvaluation") <<
"Starting TF sessions";
66 LogDebug(
"DeepSCGraphEvaluation") <<
"TF ready";
70 std::string file,
const std::vector<std::string>& availableInputs)
const {
74 if (inputfile.fail()) {
75 throw cms::Exception(
"MissingFile") <<
"Input features config file not found: " <<
file;
81 while (inputfile >> varName >> type_str >>
par1 >>
par2) {
82 if (type_str ==
"MeanRms")
84 else if (type_str ==
"MinMax")
90 auto match =
std::find(availableInputs.begin(), availableInputs.end(), varName);
91 if (match == std::end(availableInputs)) {
92 throw cms::Exception(
"MissingInput") <<
"Requested input (" << varName <<
") not available between DNN inputs";
94 LogDebug(
"DeepSCGraphEvalutation") <<
"Registered input feature: " << varName <<
", scaler=" << type_str;
102 std::vector<float>
inputs;
121 LogDebug(
"DeepSCGraphEvaluation") <<
"Starting evaluation";
124 std::vector<std::vector<float>> outputs_clustering;
128 size_t nInputs =
inputs.clustersX.size();
130 while (nInputs > 0) {
142 tensorflow::Tensor clsX_{tensorflow::DT_FLOAT,
144 tensorflow::Tensor windX_{tensorflow::DT_FLOAT, {
static_cast<long int>(nItems),
cfg_.
nWindowFeatures}};
145 tensorflow::Tensor hitsX_{tensorflow::DT_FLOAT,
147 tensorflow::Tensor isSeedX_{tensorflow::DT_FLOAT, {
static_cast<long int>(nItems),
cfg_.
maxNClusters, 1}};
148 tensorflow::Tensor nClsSize_{tensorflow::DT_FLOAT, {
static_cast<long int>(nItems)}};
151 for (
size_t b = 0;
b < nItems;
b++) {
157 if (
k < cls_data.size()) {
160 clsX_.tensor<
float, 3>()(
b,
k, z) = 0.;
167 for (
size_t b = 0;
b < nItems;
b++) {
171 windX_.matrix<
float>()(
b,
k) =
float(wind_features[
k]);
176 for (
size_t b = 0;
b < nItems;
b++) {
178 size_t ncls_in_window = hits_data.size();
182 size_t nhits_in_cluster;
183 if (
k < ncls_in_window)
184 nhits_in_cluster = hits_data[
k].size();
186 nhits_in_cluster = 0;
191 bool ok =
j < nhits_in_cluster;
197 hitsX_.tensor<
float, 4>()(
b,
k,
j, z) = 0.;
204 for (
size_t b = 0;
b < nItems;
b++) {
208 if (
k < isSeed_data.size()) {
209 isSeedX_.tensor<
float, 3>()(
b,
k, 0) =
float(isSeed_data[
k]);
211 isSeedX_.tensor<
float, 3>()(
b,
k, 0) = 0.;
216 for (
size_t b = 0;
b < nItems;
b++) {
220 std::vector<std::pair<std::string, tensorflow::Tensor>> feed_dict = {
221 {
"input_1", clsX_}, {
"input_2", windX_}, {
"input_3", hitsX_}, {
"input_4", isSeedX_}, {
"input_5", nClsSize_}};
224 std::vector<tensorflow::Tensor> outputs_tf;
227 LogDebug(
"DeepSCGraphEvaluation") <<
"Run model";
230 const auto& y_cl = outputs_tf[0].tensor<
float, 3>();
232 for (
size_t b = 0;
b < nItems;
b++) {
234 std::vector<float> cl_output(ncls);
235 for (
size_t iC = 0; iC < ncls; iC++) {
237 float y = y_cl(
b, iC, 0);
239 cl_output[iC] = 1 / (1 +
std::exp(-y));
245 outputs_clustering.push_back(cl_output);
249 return outputs_clustering;
std::vector< float > getScaledInputs(const DeepSCInputs::FeaturesMap &variables, const DeepSCInputs::InputConfigs &config) const
const DeepSCConfiguration cfg_
std::string fullPath() const
GraphDef * loadGraphDef(const std::string &pbFile)
std::string configFileWindowFeatures
DeepSCGraphEvaluation(const DeepSCConfiguration &)
DeepSCInputs::InputConfigs inputFeaturesClusters
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
static const std::vector< std::string > availableClusterInputs
static const std::vector< std::string > availableWindowInputs
DeepSCInputs::InputConfigs inputFeaturesWindows
std::string configFileHitsFeatures
void initTensorFlowGraphAndSession()
std::vector< float > features(const reco::PreId &ecal, const reco::PreId &hcal, double rho, const reco::BeamSpot &spot, noZS::EcalClusterLazyTools &ecalTools)
DeepSCInputs::InputConfigs readInputFeaturesConfig(std::string file, const std::vector< std::string > &availableInputs) const
void run(Session *session, const NamedTensorList &inputs, const std::vector< std::string > &outputNames, std::vector< Tensor > *outputs, const thread::ThreadPoolOptions &threadPoolOptions)
bool closeSession(Session *&session)
std::string configFileClusterFeatures
DeepSCInputs::InputConfigs inputFeaturesHits
static const std::vector< std::string > availableHitsInputs
Session * createSession()
void setLogging(const std::string &level="3")
std::unique_ptr< tensorflow::GraphDef > graphDef_
tensorflow::Session * session_
std::vector< std::vector< float > > evaluate(const DeepSCInputs::Inputs &inputs) const