Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:15:47

0001 #ifndef HeterogeneousCore_SonicCore_SonicOneEDAnalyzer
0002 #define HeterogeneousCore_SonicCore_SonicOneEDAnalyzer
0003 
0004 #include "FWCore/Framework/interface/Event.h"
0005 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0006 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0007 #include "FWCore/Framework/interface/EventSetup.h"
0008 #include "FWCore/Concurrency/interface/WaitingTaskWithArenaHolder.h"
0009 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0010 #include "HeterogeneousCore/SonicCore/interface/SonicClientBase.h"
0011 #include "HeterogeneousCore/SonicCore/interface/sonic_utils.h"
0012 
0013 #include <string>
0014 #include <chrono>
0015 
0016 //this is a one analyzer to enable writing trees/histograms for analysis users with results from inference as a service
0017 template <typename Client, typename... Capabilities>
0018 class SonicOneEDAnalyzer : public edm::one::EDAnalyzer<Capabilities...> {
0019 public:
0020   //typedefs to simplify usage
0021   typedef typename Client::Input Input;
0022   typedef typename Client::Output Output;
0023   //constructor
0024   SonicOneEDAnalyzer(edm::ParameterSet const& cfg, bool verbose = true)
0025       : clientPset_(cfg.getParameterSet("Client")),
0026         debugName_(cfg.getParameter<std::string>("@module_label")),
0027         verbose_(clientPset_.getUntrackedParameter<bool>("verbose")) {
0028     //ExternalWork is not compatible with one modules, so Sync mode is enforced
0029     if (clientPset_.getParameter<std::string>("mode") != "Sync") {
0030       edm::LogWarning("ResetClientMode") << "Resetting client mode to Sync for SonicOneEDAnalyzer";
0031       clientPset_.addParameter<std::string>("mode", "Sync");
0032     }
0033   }
0034   //destructor
0035   ~SonicOneEDAnalyzer() override = default;
0036 
0037   //construct client at beginning of job
0038   //in case client constructor depends on operations happening in derived module constructors
0039   void beginJob() override { makeClient(); }
0040 
0041   //derived classes still use a dedicated acquire() interface that incorporates client_->input() for consistency
0042   virtual void acquire(edm::Event const& iEvent, edm::EventSetup const& iSetup, Input& iInput) = 0;
0043   //derived classes use a dedicated analyze() interface that incorporates client_->output()
0044   void analyze(edm::Event const& iEvent, edm::EventSetup const& iSetup) final {
0045     auto t0 = std::chrono::high_resolution_clock::now();
0046     acquire(iEvent, iSetup, client_->input());
0047     if (verbose_)
0048       sonic_utils::printDebugTime(debugName_, "acquire() time: ", t0);
0049 
0050     //pattern similar to ExternalWork, but blocking
0051     auto t1 = std::chrono::high_resolution_clock::now();
0052     client_->dispatch();
0053 
0054     //measure time between acquire and produce
0055     if (verbose_)
0056       sonic_utils::printDebugTime(debugName_, "dispatch() time: ", t1);
0057 
0058     auto t2 = std::chrono::high_resolution_clock::now();
0059     analyze(iEvent, iSetup, client_->output());
0060     if (verbose_)
0061       sonic_utils::printDebugTime(debugName_, "analyze() time: ", t2);
0062 
0063     //reset client data
0064     client_->reset();
0065   }
0066   virtual void analyze(edm::Event const& iEvent, edm::EventSetup const& iSetup, Output const& iOutput) = 0;
0067 
0068 protected:
0069   //helper
0070   void makeClient() { client_ = std::make_unique<Client>(clientPset_, debugName_); }
0071 
0072   //members
0073   edm::ParameterSet clientPset_;
0074   std::unique_ptr<Client> client_;
0075   std::string debugName_;
0076   bool verbose_;
0077 };
0078 
0079 #endif