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"};
37 throw cms::Exception(
"WrongConfiguration") <<
"Mismatch between number of input features for Clusters and " 38 <<
"parameters in the scaler file.";
43 throw cms::Exception(
"WrongConfiguration") <<
"Mismatch between number of input features for Clusters and " 44 <<
"parameters in the scaler file.";
48 throw cms::Exception(
"WrongConfiguration") <<
"Mismatch between number of input features for Clusters and " 49 <<
"parameters in the scaler file.";
60 LogDebug(
"DeepSCGraphEvaluation") <<
"Loading graph";
63 LogDebug(
"DeepSCGraphEvaluation") <<
"Starting TF sessions";
65 LogDebug(
"DeepSCGraphEvaluation") <<
"TF ready";
69 std::string file,
const std::vector<std::string>& availableInputs)
const {
73 if (inputfile.fail()) {
74 throw cms::Exception(
"MissingFile") <<
"Input features config file not found: " <<
file;
80 while (inputfile >> varName >> type_str >>
par1 >>
par2) {
81 if (type_str ==
"MeanRms")
83 else if (type_str ==
"MinMax")
89 auto match =
std::find(availableInputs.begin(), availableInputs.end(), varName);
90 if (match == std::end(availableInputs)) {
91 throw cms::Exception(
"MissingInput") <<
"Requested input (" << varName <<
") not available between DNN inputs";
93 LogDebug(
"DeepSCGraphEvalutation") <<
"Registered input feature: " << varName <<
", scaler=" << type_str;
101 std::vector<float>
inputs;
120 LogDebug(
"DeepSCGraphEvaluation") <<
"Starting evaluation";
123 std::vector<std::vector<float>> outputs_clustering;
127 size_t nInputs =
inputs.clustersX.size();
129 while (nInputs > 0) {
141 tensorflow::Tensor clsX_{tensorflow::DT_FLOAT,
143 tensorflow::Tensor windX_{tensorflow::DT_FLOAT, {
static_cast<long int>(nItems),
cfg_.
nWindowFeatures}};
144 tensorflow::Tensor hitsX_{tensorflow::DT_FLOAT,
146 tensorflow::Tensor isSeedX_{tensorflow::DT_FLOAT, {
static_cast<long int>(nItems),
cfg_.
maxNClusters, 1}};
147 tensorflow::Tensor nClsSize_{tensorflow::DT_FLOAT, {
static_cast<long int>(nItems)}};
150 for (
size_t b = 0;
b < nItems;
b++) {
156 if (
k < cls_data.size()) {
159 clsX_.tensor<
float, 3>()(
b,
k, z) = 0.;
166 for (
size_t b = 0;
b < nItems;
b++) {
170 windX_.matrix<
float>()(
b,
k) =
float(wind_features[
k]);
175 for (
size_t b = 0;
b < nItems;
b++) {
177 size_t ncls_in_window = hits_data.size();
181 size_t nhits_in_cluster;
182 if (
k < ncls_in_window)
183 nhits_in_cluster = hits_data[
k].size();
185 nhits_in_cluster = 0;
190 bool ok =
j < nhits_in_cluster;
196 hitsX_.tensor<
float, 4>()(
b,
k,
j, z) = 0.;
203 for (
size_t b = 0;
b < nItems;
b++) {
207 if (
k < isSeed_data.size()) {
208 isSeedX_.tensor<
float, 3>()(
b,
k, 0) =
float(isSeed_data[
k]);
210 isSeedX_.tensor<
float, 3>()(
b,
k, 0) = 0.;
215 for (
size_t b = 0;
b < nItems;
b++) {
219 std::vector<std::pair<std::string, tensorflow::Tensor>> feed_dict = {
220 {
"input_1", clsX_}, {
"input_2", windX_}, {
"input_3", hitsX_}, {
"input_4", isSeedX_}, {
"input_5", nClsSize_}};
223 std::vector<tensorflow::Tensor> outputs_tf;
226 LogDebug(
"DeepSCGraphEvaluation") <<
"Run model";
229 const auto& y_cl = outputs_tf[0].tensor<
float, 3>();
231 for (
size_t b = 0;
b < nItems;
b++) {
233 std::vector<float> cl_output(ncls);
234 for (
size_t iC = 0; iC < ncls; iC++) {
236 float y = y_cl(
b, iC, 0);
238 cl_output[iC] = 1 / (1 +
std::exp(-y));
244 outputs_clustering.push_back(cl_output);
248 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()
std::unique_ptr< tensorflow::GraphDef > graphDef_
tensorflow::Session * session_
std::vector< std::vector< float > > evaluate(const DeepSCInputs::Inputs &inputs) const