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;
645 for (
auto const& measurement : meas) {
652 LogTrace(
"TSGForOIDNN") <<
"TSGForOIDNN::makeSeedsFromHitDoublets: TSOS for TM " <<
found;
659 seedHits.
push_back(*measurement.recHit()->hit());
667 int addtnl_layers_scanned = 0;
668 int found_compatible_on_next_layer = 0;
675 for (
auto compLayer : compLayers) {
678 if (addtnl_layers_scanned >= max_addtnl_layers)
680 if (found_compatible_on_next_layer > 0)
684 std::vector<GeometricSearchDet::DetWithState> dets_next;
690 std::vector<TrajectoryMeasurement> meas_next;
693 for (
auto const& detI_next : dets_next) {
698 if (!detI_next.second.isValid())
702 std::vector<TrajectoryMeasurement> mymeas_next =
705 for (
auto const& mea_next : mymeas_next) {
707 if (mea_next.recHit()->isValid())
708 meas_next.push_back(mea_next);
717 for (
auto const& mea_next : meas_next) {
718 if (nmeas >= max_meas)
722 updatedTSOS_next =
updator_->update(mea_next.forwardPredictedState(), *mea_next.recHit());
724 if (not updatedTSOS_next.
isValid())
729 seedHits.
push_back(*mea_next.recHit()->hit());
730 det_id = mea_next.recHit()->geographicalId().rawId();
732 found_compatible_on_next_layer++;
736 addtnl_layers_scanned++;
740 if (found_compatible_on_next_layer == 0)
748 LogTrace(
"TSGForOIDNN") <<
"TSGForOIDNN::makeSeedsFromHitDoublets: Number of seedHits: " << seedHits.
size();
753 hitDoubletSeedsMade++;
771 the_map[
"pt"] = l2.
pt();
772 the_map[
"eta"] = l2.
eta();
773 the_map[
"phi"] = l2.
phi();
774 the_map[
"validHits"] = l2.
found();
782 the_map[
"err0_IP"] =
sqrt(matrix_IP[0][0]);
783 the_map[
"err1_IP"] =
sqrt(matrix_IP[1][1]);
784 the_map[
"err2_IP"] =
sqrt(matrix_IP[2][2]);
785 the_map[
"err3_IP"] =
sqrt(matrix_IP[3][3]);
786 the_map[
"err4_IP"] =
sqrt(matrix_IP[4][4]);
787 the_map[
"tsos_IP_valid"] = 1.0;
789 the_map[
"tsos_IP_eta"] = -999;
790 the_map[
"tsos_IP_phi"] = -999;
791 the_map[
"tsos_IP_pt"] = -999;
792 the_map[
"tsos_IP_pt_eta"] = -999;
793 the_map[
"tsos_IP_pt_phi"] = -999;
794 the_map[
"err0_IP"] = -999;
795 the_map[
"err1_IP"] = -999;
796 the_map[
"err2_IP"] = -999;
797 the_map[
"err3_IP"] = -999;
798 the_map[
"err4_IP"] = -999;
799 the_map[
"tsos_IP_valid"] = 0.0;
808 the_map[
"err0_MuS"] =
sqrt(matrix_MuS[0][0]);
809 the_map[
"err1_MuS"] =
sqrt(matrix_MuS[1][1]);
810 the_map[
"err2_MuS"] =
sqrt(matrix_MuS[2][2]);
811 the_map[
"err3_MuS"] =
sqrt(matrix_MuS[3][3]);
812 the_map[
"err4_MuS"] =
sqrt(matrix_MuS[4][4]);
813 the_map[
"tsos_MuS_valid"] = 1.0;
815 the_map[
"tsos_MuS_eta"] = -999;
816 the_map[
"tsos_MuS_phi"] = -999;
817 the_map[
"tsos_MuS_pt"] = -999;
818 the_map[
"tsos_MuS_pt_eta"] = -999;
819 the_map[
"tsos_MuS_pt_phi"] = -999;
820 the_map[
"err0_MuS"] = -999;
821 the_map[
"err1_MuS"] = -999;
822 the_map[
"err2_MuS"] = -999;
823 the_map[
"err3_MuS"] = -999;
824 the_map[
"err4_MuS"] = -999;
825 the_map[
"tsos_MuS_valid"] = 0.0;
833 tensorflow::Session* session,
836 bool& dnnSuccess)
const {
837 int n_features =
metadata.get<
int>(
"n_features", 0);
840 tensorflow::Tensor
input(tensorflow::DT_FLOAT, {1, n_features});
844 fname = feature.second.data();
845 if (feature_map.find(
fname) == feature_map.end()) {
855 std::vector<tensorflow::Tensor>
outputs;
862 tensorflow::Tensor out_tensor =
outputs[0];
868 for (
long long int i = 0;
i < out_tensor.dim_size(1);
i++) {
869 float ith_output = dnn_outputs(0,
i);
870 if (ith_output > out_max) {
872 out_max = ith_output;
890 tensorflow::Session* session_HB,
891 const pt::ptree& metadata_HB,
892 tensorflow::Session* session_HLIP,
893 const pt::ptree& metadata_HLIP,
894 tensorflow::Session* session_HLMuS,
895 const pt::ptree& metadata_HLMuS,
897 bool& dnnSuccess)
const {
898 int n_features = metadata_HB.get<
int>(
"n_features", 0);
901 tensorflow::Tensor
input(tensorflow::DT_FLOAT, {1, n_features});
905 fname = feature.second.data();
906 if (feature_map.find(
fname) == feature_map.end()) {
916 std::vector<tensorflow::Tensor> outputs_HB;
921 tensorflow::Tensor out_tensor_HB = outputs_HB[0];
925 std::vector<tensorflow::Tensor> outputs_HLIP;
930 tensorflow::Tensor out_tensor_HLIP = outputs_HLIP[0];
934 std::vector<tensorflow::Tensor> outputs_HLMuS;
938 tensorflow::run(session_HLMuS, {{inputLayer_HLMuS,
input}}, {outputLayer_HLMuS}, &outputs_HLMuS);
939 tensorflow::Tensor out_tensor_HLMuS = outputs_HLMuS[0];
943 out.nHBd = round(dnn_outputs_HB(0, 0));
944 out.nHLIP = round(dnn_outputs_HLIP(0, 0));
945 out.sf = round(dnn_outputs_HLIP(0, 1));
946 out.nHLMuS = round(dnn_outputs_HLMuS(0, 0));
949 out.nHBd = std::clamp(
out.nHBd, 0, 10);
950 out.nHLIP = std::clamp(
out.nHLIP, 0, 10);
951 out.nHLMuS = std::clamp(
out.nHLMuS, 0, 10);
954 if (
out.nHBd == 0 &&
out.nHLIP == 0 &&
out.nHLMuS == 0) {
976 desc.add<
int>(
"layersToTry", 2);
977 desc.add<
double>(
"fixedErrorRescaleFactorForHitless", 2.0);
978 desc.add<
int>(
"hitsToTry", 1);
980 desc.add<
std::string>(
"estimator",
"hltESPChi2MeasurementEstimator100");
981 desc.add<
double>(
"maxEtaForTOB", 1.8);
982 desc.add<
double>(
"minEtaForTEC", 0.7);
983 desc.addUntracked<
bool>(
"debug",
false);
984 desc.add<
unsigned int>(
"maxSeeds", 20);
985 desc.add<
unsigned int>(
"maxHitlessSeeds", 5);
986 desc.add<
unsigned int>(
"maxHitSeeds", 1);
987 desc.add<
std::string>(
"propagatorName",
"PropagatorWithMaterialParabolicMf");
988 desc.add<
unsigned int>(
"maxHitlessSeedsIP", 5);
989 desc.add<
unsigned int>(
"maxHitlessSeedsMuS", 0);
990 desc.add<
unsigned int>(
"maxHitDoubletSeeds", 0);
991 desc.add<
bool>(
"getStrategyFromDNN",
false);
992 desc.add<
bool>(
"useRegressor",
false);
994 descriptions.
add(
"tsgForOIDNN",
desc);
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.
const double minEtaForTEC_
Minimum eta value to activate searching in the TEC.
Container for DNN outupts.
static std::string to_string(const XMLCh *ch)
static std::string const input
unsigned short found() const
Number of valid hits on track.
static PlanePointer build(Args &&... args)
GlobalPoint position() const
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)
Session * createSession()
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_