Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-10-25 09:56:12

0001 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0002 #include "FWCore/Framework/interface/Run.h"
0003 #include "FWCore/Framework/interface/Event.h"
0004 #include "FWCore/Framework/interface/EventSetup.h"
0005 #include "FWCore/Framework/interface/MakerMacros.h"
0006 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0007 #include "FWCore/Utilities/interface/EDGetToken.h"
0008 #include "FWCore/Utilities/interface/ESGetToken.h"
0009 #include "FWCore/Utilities/interface/InputTag.h"
0010 #include "FWCore/Utilities/interface/Exception.h"
0011 #include "DataFormats/Common/interface/Handle.h"
0012 
0013 #include "L1Trigger/TrackerTFP/interface/Demonstrator.h"
0014 
0015 #include <sstream>
0016 
0017 using namespace std;
0018 using namespace edm;
0019 using namespace tt;
0020 
0021 namespace trackerTFP {
0022 
0023   /*! \class  trackerTFP::AnalyzerDemonstrator
0024    *  \brief  Class to demontrate correctness of track trigger emulators
0025    *          by comparing FW with SW
0026    *  \author Thomas Schuh
0027    *  \date   2020, Nov
0028    */
0029   class AnalyzerDemonstrator : public one::EDAnalyzer<one::WatchRuns> {
0030   public:
0031     AnalyzerDemonstrator(const ParameterSet& iConfig);
0032     void beginJob() override {}
0033     void beginRun(const Run& iEvent, const EventSetup& iSetup) override;
0034     void analyze(const Event& iEvent, const EventSetup& iSetup) override;
0035     void endRun(const Run& iEvent, const EventSetup& iSetup) override {}
0036     void endJob() override {}
0037 
0038   private:
0039     //
0040     void convert(const Event& iEvent,
0041                  const EDGetTokenT<StreamsTrack>& tokenTracks,
0042                  const EDGetTokenT<StreamsStub>& tokenStubs,
0043                  vector<vector<Frame>>& bits) const;
0044     //
0045     template <typename T>
0046     void convert(const T& collection, vector<vector<Frame>>& bits) const;
0047     // ED input token of Tracks
0048     EDGetTokenT<StreamsStub> edGetTokenStubsIn_;
0049     EDGetTokenT<StreamsStub> edGetTokenStubsOut_;
0050     // ED input token of Stubs
0051     EDGetTokenT<StreamsTrack> edGetTokenTracksIn_;
0052     EDGetTokenT<StreamsTrack> edGetTokenTracksOut_;
0053     // Setup token
0054     ESGetToken<Setup, SetupRcd> esGetTokenSetup_;
0055     // Demonstrator token
0056     ESGetToken<Demonstrator, DemonstratorRcd> esGetTokenDemonstrator_;
0057     //
0058     const Setup* setup_ = nullptr;
0059     //
0060     const Demonstrator* demonstrator_ = nullptr;
0061   };
0062 
0063   AnalyzerDemonstrator::AnalyzerDemonstrator(const ParameterSet& iConfig) {
0064     // book in- and output ED products
0065     const string& labelIn = iConfig.getParameter<string>("LabelIn");
0066     const string& labelOut = iConfig.getParameter<string>("LabelOut");
0067     const string& branchStubs = iConfig.getParameter<string>("BranchAcceptedStubs");
0068     const string& branchTracks = iConfig.getParameter<string>("BranchAcceptedTracks");
0069     edGetTokenStubsIn_ = consumes<StreamsStub>(InputTag(labelIn, branchStubs));
0070     edGetTokenStubsOut_ = consumes<StreamsStub>(InputTag(labelOut, branchStubs));
0071     if (labelIn == "TrackerTFPProducerKFin" || labelIn == "TrackerTFPProducerKF")
0072       edGetTokenTracksIn_ = consumes<StreamsTrack>(InputTag(labelIn, branchTracks));
0073     if (labelOut == "TrackerTFPProducerKF" || labelOut == "TrackerTFPProducerDR")
0074       edGetTokenTracksOut_ = consumes<StreamsTrack>(InputTag(labelOut, branchTracks));
0075     // book ES products
0076     esGetTokenSetup_ = esConsumes<Setup, SetupRcd, Transition::BeginRun>();
0077     esGetTokenDemonstrator_ = esConsumes<Demonstrator, DemonstratorRcd, Transition::BeginRun>();
0078   }
0079 
0080   void AnalyzerDemonstrator::beginRun(const Run& iEvent, const EventSetup& iSetup) {
0081     //
0082     setup_ = &iSetup.getData(esGetTokenSetup_);
0083     //
0084     demonstrator_ = &iSetup.getData(esGetTokenDemonstrator_);
0085   }
0086 
0087   void AnalyzerDemonstrator::analyze(const Event& iEvent, const EventSetup& iSetup) {
0088     vector<vector<Frame>> input;
0089     vector<vector<Frame>> output;
0090     convert(iEvent, edGetTokenTracksIn_, edGetTokenStubsIn_, input);
0091     convert(iEvent, edGetTokenTracksOut_, edGetTokenStubsOut_, output);
0092     if (!demonstrator_->analyze(input, output)) {
0093       cms::Exception exception("RunTimeError.");
0094       exception.addContext("trackerTFP::AnalyzerDemonstrator::analyze");
0095       exception << "Bit error detected.";
0096       throw exception;
0097     }
0098   }
0099 
0100   //
0101   void AnalyzerDemonstrator::convert(const Event& iEvent,
0102                                      const EDGetTokenT<StreamsTrack>& tokenTracks,
0103                                      const EDGetTokenT<StreamsStub>& tokenStubs,
0104                                      vector<vector<Frame>>& bits) const {
0105     const bool tracks = !tokenTracks.isUninitialized();
0106     const bool stubs = !tokenStubs.isUninitialized();
0107     Handle<StreamsStub> handleStubs;
0108     Handle<StreamsTrack> handleTracks;
0109     int numChannelStubs(0);
0110     if (stubs) {
0111       iEvent.getByToken<StreamsStub>(tokenStubs, handleStubs);
0112       numChannelStubs = handleStubs->size();
0113     }
0114     int numChannelTracks(0);
0115     if (tracks) {
0116       iEvent.getByToken<StreamsTrack>(tokenTracks, handleTracks);
0117       numChannelTracks = handleTracks->size();
0118     }
0119     numChannelTracks /= setup_->numRegions();
0120     numChannelStubs /= (setup_->numRegions() * (tracks ? numChannelTracks : 1));
0121     bits.reserve(numChannelTracks + numChannelStubs);
0122     for (int region = 0; region < setup_->numRegions(); region++) {
0123       if (tracks) {
0124         const int offsetTracks = region * numChannelTracks;
0125         for (int channelTracks = 0; channelTracks < numChannelTracks; channelTracks++) {
0126           const int offsetStubs = (region * numChannelTracks + channelTracks) * numChannelStubs;
0127           if (tracks)
0128             convert(handleTracks->at(offsetTracks + channelTracks), bits);
0129           if (stubs) {
0130             for (int channelStubs = 0; channelStubs < numChannelStubs; channelStubs++)
0131               convert(handleStubs->at(offsetStubs + channelStubs), bits);
0132           }
0133         }
0134       } else {
0135         const int offsetStubs = region * numChannelStubs;
0136         for (int channelStubs = 0; channelStubs < numChannelStubs; channelStubs++)
0137           convert(handleStubs->at(offsetStubs + channelStubs), bits);
0138       }
0139     }
0140   }
0141 
0142   //
0143   template <typename T>
0144   void AnalyzerDemonstrator::convert(const T& collection, vector<vector<Frame>>& bits) const {
0145     bits.emplace_back();
0146     vector<Frame>& bvs = bits.back();
0147     bvs.reserve(collection.size());
0148     transform(collection.begin(), collection.end(), back_inserter(bvs), [](const auto& frame) { return frame.second; });
0149   }
0150 
0151 }  // namespace trackerTFP
0152 
0153 DEFINE_FWK_MODULE(trackerTFP::AnalyzerDemonstrator);