Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:31:39

0001 #include "TrackingTools/TrackRefitter/interface/TrackTransformer.h"
0002 #include "TrackingTools/TrackRefitter/interface/TrackTransformerForGlobalCosmicMuons.h"
0003 #include "TrackingTools/TrackRefitter/interface/TrackTransformerForCosmicMuons.h"
0004 
0005 #include "FWCore/Framework/interface/ConsumesCollector.h"
0006 #include "FWCore/Framework/interface/stream/EDProducer.h"
0007 #include "FWCore/Framework/interface/Event.h"
0008 #include "FWCore/Framework/interface/EventSetup.h"
0009 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0010 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0011 #include "FWCore/Utilities/interface/InputTag.h"
0012 
0013 #include "TrackingTools/PatternTools/interface/Trajectory.h"
0014 #include "TrackingTools/PatternTools/interface/TrajTrackAssociation.h"
0015 
0016 #include "DataFormats/TrackReco/interface/Track.h"
0017 #include "DataFormats/TrackReco/interface/TrackFwd.h"
0018 #include "DataFormats/Common/interface/Handle.h"
0019 
0020 /** \class TracksToTrajectories
0021  *  This class, which is a EDProducer, takes a reco::TrackCollection from the Event and refits the rechits 
0022  *  strored in the reco::Tracks. The final result is a std::vector of Trajectories (objs of the type "Trajectory"), 
0023  *  which is loaded into the Event in a transient way
0024  *
0025  *  \author R. Bellan - INFN Torino <riccardo.bellan@cern.ch>
0026  */
0027 
0028 // In principle this should be anonymous namespace, but then
0029 // DEFINE_FWK_MODULE() will yield compilation warnings, so using
0030 // (hopefully) unique namespace instead.
0031 // The point of the namespace is to not to pollute the global
0032 // namespace (and symbol space).
0033 namespace tracksToTrajectories {
0034   struct Count {
0035     Count() : theNTracks(0), theNFailures(0) {}
0036     //Using mutable since we want to update the value.
0037     mutable std::atomic<int> theNTracks;
0038     mutable std::atomic<int> theNFailures;
0039   };
0040 }  // namespace tracksToTrajectories
0041 using namespace tracksToTrajectories;
0042 
0043 class TracksToTrajectories : public edm::stream::EDProducer<edm::GlobalCache<Count>> {
0044 public:
0045   /// Constructor
0046   TracksToTrajectories(const edm::ParameterSet&, const Count*);
0047 
0048   /// Destructor
0049   ~TracksToTrajectories() override;
0050 
0051   static std::unique_ptr<Count> initializeGlobalCache(edm::ParameterSet const&) { return std::make_unique<Count>(); }
0052 
0053   // Operations
0054   static void globalEndJob(Count const* iCount);
0055 
0056   /// Convert a reco::TrackCollection into std::vector<Trajectory>
0057   void produce(edm::Event&, const edm::EventSetup&) override;
0058 
0059 private:
0060   edm::EDGetTokenT<reco::TrackCollection> theTracksToken;
0061   std::unique_ptr<TrackTransformerBase> theTrackTransformer;
0062 };
0063 
0064 using namespace std;
0065 using namespace edm;
0066 
0067 /// Constructor
0068 TracksToTrajectories::TracksToTrajectories(const ParameterSet& parameterSet, const Count*) {
0069   theTracksToken = consumes<reco::TrackCollection>(parameterSet.getParameter<InputTag>("Tracks"));
0070 
0071   ParameterSet trackTransformerParam = parameterSet.getParameter<ParameterSet>("TrackTransformer");
0072 
0073   string type = parameterSet.getParameter<string>("Type");
0074 
0075   if (type == "Default")
0076     theTrackTransformer = std::make_unique<TrackTransformer>(trackTransformerParam, consumesCollector());
0077   else if (type == "GlobalCosmicMuonsForAlignment")
0078     theTrackTransformer =
0079         std::make_unique<TrackTransformerForGlobalCosmicMuons>(trackTransformerParam, consumesCollector());
0080   else if (type == "CosmicMuonsForAlignment")
0081     theTrackTransformer = std::make_unique<TrackTransformerForCosmicMuons>(trackTransformerParam, consumesCollector());
0082   else {
0083     throw cms::Exception("TracksToTrajectories")
0084         << "The selected algorithm does not exist"
0085         << "\n"
0086         << "Possible choices are:"
0087         << "\n"
0088         << "Type = [Default, GlobalCosmicMuonsForAlignment, CosmicMuonsForAlignment]";
0089   }
0090 
0091   produces<vector<Trajectory>>("Refitted");
0092   produces<TrajTrackAssociationCollection>("Refitted");
0093 }
0094 
0095 /// Destructor
0096 TracksToTrajectories::~TracksToTrajectories() {}
0097 
0098 void TracksToTrajectories::globalEndJob(Count const* iCount) {
0099   constexpr char metname[] = "Reco|TrackingTools|TracksToTrajectories";
0100 
0101   auto theNFailures = iCount->theNFailures.load();
0102   auto theNTracks = iCount->theNTracks.load();
0103 
0104   if (theNFailures != 0)
0105     LogWarning(metname) << "During the refit there were " << theNFailures << " out of " << theNTracks
0106                         << " tracks, i.e. failure rate is: " << double(theNFailures) / theNTracks;
0107   else {
0108     LogTrace(metname) << "Refit of the tracks done without any failure";
0109   }
0110 }
0111 
0112 /// Convert Tracks into Trajectories
0113 void TracksToTrajectories::produce(Event& event, const EventSetup& setup) {
0114 #ifdef EDM_ML_DEBUG
0115   constexpr char metname[] = "Reco|TrackingTools|TracksToTrajectories";
0116 #endif
0117 
0118   theTrackTransformer->setServices(setup);
0119 
0120   // Collection of Trajectory
0121   auto trajectoryCollection = std::make_unique<vector<Trajectory>>();
0122 
0123   // Get the reference
0124   RefProd<vector<Trajectory>> trajectoryCollectionRefProd = event.getRefBeforePut<vector<Trajectory>>("Refitted");
0125 
0126   // Get the RecTrack collection from the event
0127   Handle<reco::TrackCollection> tracks;
0128   event.getByToken(theTracksToken, tracks);
0129 
0130   // Association map between Trajectory and Track
0131   auto trajTrackMap = std::make_unique<TrajTrackAssociationCollection>(trajectoryCollectionRefProd, tracks);
0132 
0133   Ref<vector<Trajectory>>::key_type trajectoryIndex = 0;
0134   reco::TrackRef::key_type trackIndex = 0;
0135 
0136   // Loop over the Rec tracks
0137   for (auto const& newTrack : *tracks) {
0138     ++(globalCache()->theNTracks);
0139 
0140     auto const& trajectoriesSM = theTrackTransformer->transform(newTrack);
0141 
0142     if (!trajectoriesSM.empty()) {
0143       // Load the trajectory in the Trajectory Container
0144       trajectoryCollection->push_back(trajectoriesSM.front());
0145 
0146       // Make the association between the Trajectory and the original Track
0147       trajTrackMap->insert(Ref<vector<Trajectory>>(trajectoryCollectionRefProd, trajectoryIndex++),
0148                            reco::TrackRef(tracks, trackIndex++));
0149     } else {
0150       LogTrace(metname) << "Error in the Track refitting. This should not happen";
0151       ++(globalCache()->theNFailures);
0152     }
0153   }
0154   LogTrace(metname) << "Load the Trajectory Collection";
0155   event.put(std::move(trajectoryCollection), "Refitted");
0156   event.put(std::move(trajTrackMap), "Refitted");
0157 }
0158 
0159 #include "FWCore/Framework/interface/MakerMacros.h"
0160 DEFINE_FWK_MODULE(TracksToTrajectories);