34 #include <boost/property_tree/ptree.hpp> 35 #include <boost/property_tree/json_parser.hpp> 36 #include <boost/range/adaptor/reversed.hpp> 38 namespace pt = boost::property_tree;
59 const std::unique_ptr<TrajectoryStateUpdator>
updator_;
119 unsigned int& hitlessSeedsMade,
120 unsigned int& numSeedsMade,
121 std::vector<TrajectorySeed>&
out)
const;
129 unsigned int& hitSeedsMade,
130 unsigned int& numSeedsMade,
132 unsigned int& layerCount,
133 std::vector<TrajectorySeed>&
out)
const;
142 unsigned int& hitDoubletSeedsMade,
143 unsigned int& numSeedsMade,
145 unsigned int& layerCount,
146 std::vector<TrajectorySeed>&
out)
const;
161 tensorflow::Session* session,
164 bool& dnnSuccess)
const;
167 void evaluateRegressor(
const std::unordered_map<std::string, float>& feature_map,
168 tensorflow::Session* session_HB,
169 const pt::ptree& metadata_HB,
170 tensorflow::Session* session_HLIP,
171 const pt::ptree& metadata_HLIP,
172 tensorflow::Session* session_HLMuS,
173 const pt::ptree& metadata_HLMuS,
175 bool& dnnSuccess)
const;
179 : src_(consumes(iConfig.getParameter<
edm::
InputTag>(
"src"))),
189 measurementTrackerTag_(consumes(iConfig.getParameter<
edm::
InputTag>(
"MeasurementTrackerEvent"))),
190 theCategory_(
std::
string(
"Muon|RecoMuon|TSGForOIDNN")),
191 maxSeeds_(iConfig.getParameter<uint32_t>(
"maxSeeds")),
192 maxHitSeeds_(iConfig.getParameter<uint32_t>(
"maxHitSeeds")),
193 maxHitlessSeeds_(iConfig.getParameter<uint32_t>(
"maxHitlessSeeds")),
194 numOfLayersToTry_(iConfig.getParameter<int32_t>(
"layersToTry")),
195 numOfHitsToTry_(iConfig.getParameter<int32_t>(
"hitsToTry")),
196 fixedErrorRescalingForHitless_(iConfig.getParameter<double>(
"fixedErrorRescaleFactorForHitless")),
197 minEtaForTEC_(iConfig.getParameter<double>(
"minEtaForTEC")),
198 maxEtaForTOB_(iConfig.getParameter<double>(
"maxEtaForTOB")),
199 maxHitlessSeedsIP_(iConfig.getParameter<uint32_t>(
"maxHitlessSeedsIP")),
200 maxHitlessSeedsMuS_(iConfig.getParameter<uint32_t>(
"maxHitlessSeedsMuS")),
201 maxHitDoubletSeeds_(iConfig.getParameter<uint32_t>(
"maxHitDoubletSeeds")),
202 getStrategyFromDNN_(iConfig.getParameter<
bool>(
"getStrategyFromDNN")),
203 useRegressor_(iConfig.getParameter<
bool>(
"useRegressor")),
204 dnnMetadataPath_(iConfig.getParameter<
std::
string>(
"dnnMetadataPath")) {
234 produces<std::vector<TrajectorySeed> >();
254 unsigned int numSeedsMade = 0;
255 unsigned int layerCount = 0;
256 unsigned int hitlessSeedsMadeIP = 0;
257 unsigned int hitlessSeedsMadeMuS = 0;
258 unsigned int hitSeedsMade = 0;
259 unsigned int hitDoubletSeedsMade = 0;
262 std::unordered_map<std::string, float> feature_map;
284 std::unique_ptr<std::vector<TrajectorySeed> >
result(
new std::vector<TrajectorySeed>());
288 std::vector<BarrelDetLayer const*>
const& tob = gsTracker->tobLayers();
289 std::vector<ForwardDetLayer const*>
const& tecPositive =
291 std::vector<ForwardDetLayer const*>
const& tecNegative =
304 for (
auto const& l2 : l2TrackCol) {
306 std::vector<TrajectorySeed>
out;
307 LogTrace(
"TSGForOIDNN") <<
"TSGForOIDNN::produce: L2 muon pT, eta, phi --> " << l2.pt() <<
" , " << l2.eta()
308 <<
" , " << l2.phi();
312 dummyPlane->move(fts.
position() - dummyPlane->position());
314 LogTrace(
"TSGForOIDNN") <<
"TSGForOIDNN::produce: Created TSOSatIP: " << tsosAtIP;
318 LogTrace(
"TSGForOIDNN") <<
"TSGForOIDNN::produce: Created TSOSatMuonSystem: " << tsosAtMuonSystem;
321 <<
"TSGForOIDNN::produce: Check the error of the L2 parameter and use hit seeds if big errors";
344 bool dnnSuccess =
false;
371 errorSFHitless = strPars.
sf;
375 hitlessSeedsMadeIP = 0;
376 hitlessSeedsMadeMuS = 0;
378 hitDoubletSeedsMade = 0;
380 auto createSeeds = [&](
auto const&
layers) {
434 LogTrace(
"TSGForOIDNN") <<
"TSGForOIDNN:::produce: NumSeedsMade = " << numSeedsMade
435 <<
" , layerCount = " << layerCount;
441 hitlessSeedsMadeIP = 0;
442 hitlessSeedsMadeMuS = 0;
444 hitDoubletSeedsMade = 0;
450 createSeeds(tecPositive);
451 LogTrace(
"TSGForOIDNN") <<
"TSGForOIDNN:::produce: NumSeedsMade = " << numSeedsMade
452 <<
" , layerCount = " << layerCount;
458 createSeeds(tecNegative);
459 LogTrace(
"TSGForOIDNN") <<
"TSGForOIDNN:::produce: NumSeedsMade = " << numSeedsMade
460 <<
" , layerCount = " << layerCount;
463 for (std::vector<TrajectorySeed>::iterator it =
out.begin(); it !=
out.end(); ++it) {
482 unsigned int& hitlessSeedsMade,
483 unsigned int& numSeedsMade,
484 std::vector<TrajectorySeed>&
out)
const {
486 LogTrace(
"TSGForOIDNN") <<
"TSGForOIDNN::makeSeedsWithoutHits: Start hitless";
487 std::vector<GeometricSearchDet::DetWithState> dets;
490 auto const& detOnLayer = dets.front().first;
491 auto& tsosOnLayer = dets.front().second;
492 LogTrace(
"TSGForOIDNN") <<
"TSGForOIDNN::makeSeedsWithoutHits: tsosOnLayer " << tsosOnLayer;
493 if (!tsosOnLayer.isValid()) {
496 tsosOnLayer.rescaleError(errorSF);
501 LogTrace(
"TSGForOIDNN") <<
"TSGForOIDNN::makeSeedsWithoutHits: TSOS (Hitless) done ";
516 unsigned int& hitSeedsMade,
517 unsigned int& numSeedsMade,
519 unsigned int& layerCount,
520 std::vector<TrajectorySeed>&
out)
const {
526 std::vector<GeometricSearchDet::DetWithState> dets;
530 LogTrace(
"TSGForOIDNN") <<
"TSGForOIDNN::makeSeedsFromHits: Find measurements on each detWithState " << dets.size();
531 std::vector<TrajectoryMeasurement> meas;
532 for (
auto const& detI : dets) {
536 if (!detI.second.isValid())
539 std::vector<TrajectoryMeasurement> mymeas =
541 for (
auto const& measurement : mymeas) {
542 if (measurement.recHit()->isValid())
543 meas.push_back(measurement);
548 LogTrace(
"TSGForOIDNN") <<
"TSGForOIDNN::makeSeedsFromHits: Update TSOS using TMs after sorting, then create " 549 "Trajectory Seed, number of TM = " 553 unsigned int found = 0;
554 for (
auto const& measurement : meas) {
558 LogTrace(
"TSGForOIDNN") <<
"TSGForOIDNN::makeSeedsFromHits: TSOS for TM " <<
found;
563 seedHits.
push_back(*measurement.recHit()->hit());
566 LogTrace(
"TSGForOIDNN") <<
"TSGForOIDNN::makeSeedsFromHits: Number of seedHits: " << seedHits.
size();
589 unsigned int& hitDoubletSeedsMade,
590 unsigned int& numSeedsMade,
592 unsigned int& layerCount,
593 std::vector<TrajectorySeed>&
out)
const {
600 int max_addtnl_layers = 1;
608 std::vector<GeometricSearchDet::DetWithState> dets;
611 LogTrace(
"TSGForOIDNN") <<
"TSGForOIDNN::makeSeedsFromHitDoublets: Find measurements on each detWithState " 613 std::vector<TrajectoryMeasurement> meas;
616 for (
auto const& detI : dets) {
621 if (!detI.second.isValid())
628 for (
auto const& measurement : mymeas) {
629 if (measurement.recHit()->isValid())
630 meas.push_back(measurement);
634 LogTrace(
"TSGForOIDNN") <<
"TSGForOIDNN::makeSeedsFromHitDoublets: Update TSOS using TMs after sorting, then create " 635 "Trajectory Seed, number of TM = " 641 unsigned int found = 0;
646 for (
auto const& measurement : meas) {
655 LogTrace(
"TSGForOIDNN") <<
"TSGForOIDNN::makeSeedsFromHitDoublets: TSOS for TM " <<
found;
662 seedHits.
push_back(*measurement.recHit()->hit());
670 int addtnl_layers_scanned = 0;
671 int found_compatible_on_next_layer = 0;
678 for (
auto compLayer : compLayers) {
681 if (addtnl_layers_scanned >= max_addtnl_layers)
683 if (found_compatible_on_next_layer > 0)
687 std::vector<GeometricSearchDet::DetWithState> dets_next;
693 std::vector<TrajectoryMeasurement> meas_next;
696 for (
auto const& detI_next : dets_next) {
701 if (!detI_next.second.isValid())
705 std::vector<TrajectoryMeasurement> mymeas_next =
708 for (
auto const& mea_next : mymeas_next) {
710 if (mea_next.recHit()->isValid())
711 meas_next.push_back(mea_next);
720 for (
auto const& mea_next : meas_next) {
721 if (nmeas >= max_meas)
725 updatedTSOS_next =
updator_->update(mea_next.forwardPredictedState(), *mea_next.recHit());
727 if (not updatedTSOS_next.
isValid())
732 seedHits.
push_back(*mea_next.recHit()->hit());
733 det_id = mea_next.recHit()->geographicalId().rawId();
735 found_compatible_on_next_layer++;
739 addtnl_layers_scanned++;
743 if (found_compatible_on_next_layer == 0)
751 LogTrace(
"TSGForOIDNN") <<
"TSGForOIDNN::makeSeedsFromHitDoublets: Number of seedHits: " << seedHits.
size();
756 hitDoubletSeedsMade++;
774 the_map[
"pt"] = l2.
pt();
775 the_map[
"eta"] = l2.
eta();
776 the_map[
"phi"] = l2.
phi();
777 the_map[
"validHits"] = l2.
found();
785 the_map[
"err0_IP"] =
sqrt(matrix_IP[0][0]);
786 the_map[
"err1_IP"] =
sqrt(matrix_IP[1][1]);
787 the_map[
"err2_IP"] =
sqrt(matrix_IP[2][2]);
788 the_map[
"err3_IP"] =
sqrt(matrix_IP[3][3]);
789 the_map[
"err4_IP"] =
sqrt(matrix_IP[4][4]);
790 the_map[
"tsos_IP_valid"] = 1.0;
792 the_map[
"tsos_IP_eta"] = -999;
793 the_map[
"tsos_IP_phi"] = -999;
794 the_map[
"tsos_IP_pt"] = -999;
795 the_map[
"tsos_IP_pt_eta"] = -999;
796 the_map[
"tsos_IP_pt_phi"] = -999;
797 the_map[
"err0_IP"] = -999;
798 the_map[
"err1_IP"] = -999;
799 the_map[
"err2_IP"] = -999;
800 the_map[
"err3_IP"] = -999;
801 the_map[
"err4_IP"] = -999;
802 the_map[
"tsos_IP_valid"] = 0.0;
811 the_map[
"err0_MuS"] =
sqrt(matrix_MuS[0][0]);
812 the_map[
"err1_MuS"] =
sqrt(matrix_MuS[1][1]);
813 the_map[
"err2_MuS"] =
sqrt(matrix_MuS[2][2]);
814 the_map[
"err3_MuS"] =
sqrt(matrix_MuS[3][3]);
815 the_map[
"err4_MuS"] =
sqrt(matrix_MuS[4][4]);
816 the_map[
"tsos_MuS_valid"] = 1.0;
818 the_map[
"tsos_MuS_eta"] = -999;
819 the_map[
"tsos_MuS_phi"] = -999;
820 the_map[
"tsos_MuS_pt"] = -999;
821 the_map[
"tsos_MuS_pt_eta"] = -999;
822 the_map[
"tsos_MuS_pt_phi"] = -999;
823 the_map[
"err0_MuS"] = -999;
824 the_map[
"err1_MuS"] = -999;
825 the_map[
"err2_MuS"] = -999;
826 the_map[
"err3_MuS"] = -999;
827 the_map[
"err4_MuS"] = -999;
828 the_map[
"tsos_MuS_valid"] = 0.0;
836 tensorflow::Session* session,
839 bool& dnnSuccess)
const {
840 int n_features =
metadata.get<
int>(
"n_features", 0);
843 tensorflow::Tensor
input(tensorflow::DT_FLOAT, {1, n_features});
847 fname = feature.second.data();
848 if (feature_map.find(
fname) == feature_map.end()) {
858 std::vector<tensorflow::Tensor>
outputs;
865 tensorflow::Tensor out_tensor =
outputs[0];
871 for (
long long int i = 0;
i < out_tensor.dim_size(1);
i++) {
872 float ith_output = dnn_outputs(0,
i);
873 if (ith_output > out_max) {
875 out_max = ith_output;
893 tensorflow::Session* session_HB,
894 const pt::ptree& metadata_HB,
895 tensorflow::Session* session_HLIP,
896 const pt::ptree& metadata_HLIP,
897 tensorflow::Session* session_HLMuS,
898 const pt::ptree& metadata_HLMuS,
900 bool& dnnSuccess)
const {
901 int n_features = metadata_HB.get<
int>(
"n_features", 0);
904 tensorflow::Tensor
input(tensorflow::DT_FLOAT, {1, n_features});
908 fname = feature.second.data();
909 if (feature_map.find(
fname) == feature_map.end()) {
919 std::vector<tensorflow::Tensor> outputs_HB;
924 tensorflow::Tensor out_tensor_HB = outputs_HB[0];
928 std::vector<tensorflow::Tensor> outputs_HLIP;
933 tensorflow::Tensor out_tensor_HLIP = outputs_HLIP[0];
937 std::vector<tensorflow::Tensor> outputs_HLMuS;
941 tensorflow::run(session_HLMuS, {{inputLayer_HLMuS,
input}}, {outputLayer_HLMuS}, &outputs_HLMuS);
942 tensorflow::Tensor out_tensor_HLMuS = outputs_HLMuS[0];
946 out.nHBd = round(dnn_outputs_HB(0, 0));
947 out.nHLIP = round(dnn_outputs_HLIP(0, 0));
948 out.sf = round(dnn_outputs_HLIP(0, 1));
949 out.nHLMuS = round(dnn_outputs_HLMuS(0, 0));
952 out.nHBd = std::clamp(
out.nHBd, 0, 10);
953 out.nHLIP = std::clamp(
out.nHLIP, 0, 10);
954 out.nHLMuS = std::clamp(
out.nHLMuS, 0, 10);
957 if (
out.nHBd == 0 &&
out.nHLIP == 0 &&
out.nHLMuS == 0) {
979 desc.add<
int>(
"layersToTry", 2);
980 desc.add<
double>(
"fixedErrorRescaleFactorForHitless", 2.0);
981 desc.add<
int>(
"hitsToTry", 1);
983 desc.add<
std::string>(
"estimator",
"hltESPChi2MeasurementEstimator100");
984 desc.add<
double>(
"maxEtaForTOB", 1.8);
985 desc.add<
double>(
"minEtaForTEC", 0.7);
986 desc.addUntracked<
bool>(
"debug",
false);
987 desc.add<
unsigned int>(
"maxSeeds", 20);
988 desc.add<
unsigned int>(
"maxHitlessSeeds", 5);
989 desc.add<
unsigned int>(
"maxHitSeeds", 1);
990 desc.add<
std::string>(
"propagatorName",
"PropagatorWithMaterialParabolicMf");
991 desc.add<
unsigned int>(
"maxHitlessSeedsIP", 5);
992 desc.add<
unsigned int>(
"maxHitlessSeedsMuS", 0);
993 desc.add<
unsigned int>(
"maxHitDoubletSeeds", 0);
994 desc.add<
bool>(
"getStrategyFromDNN",
false);
995 desc.add<
bool>(
"useRegressor",
false);
997 descriptions.
add(
"tsgForOIDNN",
desc);
Session * createSession(SessionOptions &sessionOptions)
const unsigned int numOfHitsToTry_
How many hits to try per layer.
std::string dnnModelPath_HLIP_
const unsigned int maxSeeds_
Maximum number of seeds for each L2.
void evaluateRegressor(const std::unordered_map< std::string, float > &feature_map, tensorflow::Session *session_HB, const pt::ptree &metadata_HB, tensorflow::Session *session_HLIP, const pt::ptree &metadata_HLIP, tensorflow::Session *session_HLMuS, const pt::ptree &metadata_HLMuS, StrategyParameters &out, bool &dnnSuccess) const
Evaluate DNN regressor.
void updateFeatureMap(std::unordered_map< std::string, float > &the_map, const reco::Track &l2, const TrajectoryStateOnSurface &tsos_IP, const TrajectoryStateOnSurface &tsos_MuS) const
Update dictionary of inputs for DNN.
std::string dnnModelPath_HLMuS_
tensorflow::Session * tf_session_HLMuS_
T const & getData(const ESGetToken< T, R > &iToken) const noexcept(false)
const edm::ESGetToken< NavigationSchool, NavigationSchoolRecord > t_navSchool_
const edm::ESGetToken< GlobalTrackingGeometry, GlobalTrackingGeometryRecord > t_geometryH_
const edm::EDGetTokenT< MeasurementTrackerEvent > measurementTrackerTag_
std::vector< TrajectoryMeasurement > fastMeasurements(const TrajectoryStateOnSurface &stateOnThisDet, const TrajectoryStateOnSurface &tsos2, const Propagator &prop, const MeasurementEstimator &est) const
GraphDef * loadGraphDef(const std::string &pbFile)
void makeSeedsFromHitDoublets(const GeometricSearchDet &layer, const TrajectoryStateOnSurface &tsos, const Propagator &propagatorAlong, const Chi2MeasurementEstimatorBase &estimator, const MeasurementTrackerEvent &measurementTracker, const NavigationSchool &navSchool, unsigned int &hitDoubletSeedsMade, unsigned int &numSeedsMade, const unsigned int &maxHitDoubletSeeds, unsigned int &layerCount, std::vector< TrajectorySeed > &out) const
Similar to makeSeedsFromHits, but seed is created only if there are compatible hits on two adjacent l...
Geom::Phi< T > phi() const
tensorflow::Session * tf_session_HB_
const unsigned int maxHitlessSeedsMuS_
const edm::EDGetTokenT< reco::TrackCollection > src_
Labels for input collections.
const unsigned int maxHitlessSeedsIP_
std::unique_ptr< tensorflow::GraphDef > graphDef_HLIP_
const edm::ESGetToken< Chi2MeasurementEstimatorBase, TrackingComponentsRecord > t_estimatorH_
Tokens for ESHandle.
std::string to_string(const V &value)
const double minEtaForTEC_
Minimum eta value to activate searching in the TEC.
Container for DNN outupts.
static std::string const input
unsigned short found() const
Number of valid hits on track.
static PlanePointer build(Args &&... args)
GlobalPoint position() const
constexpr std::array< uint8_t, layerIndexSize< TrackerTraits > > layer
double pt() const
track transverse momentum
std::unique_ptr< Propagator > SetPropagationDirection(Propagator const &iprop, PropagationDirection dir)
Container::value_type value_type
const double fixedErrorRescalingForHitless_
Rescale L2 parameter uncertainties (fixed error vs pT, eta)
GlobalPoint globalPosition() const
const std::string dnnMetadataPath_
DNN metadata.
bool isThere(GeomDetEnumerators::SubDetector subdet) const
const std::string theCategory_
const edm::ESGetToken< Propagator, TrackingComponentsRecord > t_propagatorOppositeH_
const std::string estimatorName_
Estimator used to find dets and TrajectoryMeasurements.
const unsigned int maxHitSeeds_
Maximum number of hitbased seeds for each L2.
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)
const unsigned int maxHitDoubletSeeds_
Abs< T >::type abs(const T &t)
std::string dnnModelPath_
Settings for classifier.
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
double phi() const
azimuthal angle of momentum vector
#define DEFINE_FWK_MODULE(type)
TSGForOIDNN(const edm::ParameterSet &iConfig)
ESHandle< T > getHandle(const ESGetToken< T, R > &iToken) const
const bool useRegressor_
Whether to use DNN regressor (if false, will use classifier)
void makeSeedsWithoutHits(const GeometricSearchDet &layer, const TrajectoryStateOnSurface &tsos, const Propagator &propagatorAlong, const Chi2MeasurementEstimatorBase &estimator, double errorSF, unsigned int &hitlessSeedsMade, unsigned int &numSeedsMade, std::vector< TrajectorySeed > &out) const
Create seeds without hits on a given layer (TOB or TEC)
Log< level::Info, false > LogInfo
double eta() const
pseudorapidity of momentum vector
void setLogging(const std::string &level="3")
const CurvilinearTrajectoryError & curvilinearError() const
const edm::ESGetToken< Propagator, TrackingComponentsRecord > t_SHPOpposite_
const double maxEtaForTOB_
Maximum eta value to activate searching in the TOB.
const AlgebraicSymMatrix55 & matrix() const
ROOT::Math::SMatrix< double, 5, 5, ROOT::Math::MatRepSym< double, 5 > > AlgebraicSymMatrix55
std::string dnnModelPath_HB_
Settings for regressor.
void add(std::string const &label, ParameterSetDescription const &psetDescription)
GlobalVector globalMomentum() const
std::unique_ptr< tensorflow::GraphDef > graphDef_
const edm::ESGetToken< MagneticField, IdealMagneticFieldRecord > t_magfieldH_
std::unique_ptr< tensorflow::GraphDef > graphDef_HB_
tensorflow::Session * tf_session_
FreeTrajectoryState const * freeState(bool withErrors=true) const
const edm::ESGetToken< Propagator, TrackingComponentsRecord > t_propagatorAlongH_
const unsigned int numOfLayersToTry_
How many layers to try.
const bool getStrategyFromDNN_
Get number of seeds to use from DNN output instead of "max..Seeds" parameters.
std::unique_ptr< tensorflow::GraphDef > graphDef_HLMuS_
const std::unique_ptr< TrajectoryStateUpdator > updator_
const edm::ESGetToken< TrackerGeometry, TrackerDigiGeometryRecord > t_tmpTkGeometryH_
portabletest::Matrix Matrix
Create L3MuonTrajectorySeeds from L2 Muons in an outside-in manner.
void evaluateClassifier(const std::unordered_map< std::string, float > &feature_map, tensorflow::Session *session, const pt::ptree &metadata, StrategyParameters &out, bool &dnnSuccess) const
Evaluate DNN classifier.
void produce(edm::StreamID sid, edm::Event &iEvent, const edm::EventSetup &iSetup) const override
void makeSeedsFromHits(const GeometricSearchDet &layer, const TrajectoryStateOnSurface &tsos, const Propagator &propagatorAlong, const Chi2MeasurementEstimatorBase &estimator, const MeasurementTrackerEvent &measurementTracker, unsigned int &hitSeedsMade, unsigned int &numSeedsMade, const unsigned int &maxHitSeeds, unsigned int &layerCount, std::vector< TrajectorySeed > &out) const
Find hits on a given layer (TOB or TEC) and create seeds from updated TSOS with hit.
const unsigned int maxHitlessSeeds_
Maximum number of hitless seeds for each L2.
tensorflow::Session * tf_session_HLIP_