CMS 3D CMS Logo

SonicOneEDAnalyzer.h
Go to the documentation of this file.
1 #ifndef HeterogeneousCore_SonicCore_SonicOneEDAnalyzer
2 #define HeterogeneousCore_SonicCore_SonicOneEDAnalyzer
3 
12 
13 #include <string>
14 #include <chrono>
15 
16 //this is a one analyzer to enable writing trees/histograms for analysis users with results from inference as a service
17 template <typename Client, typename... Capabilities>
18 class SonicOneEDAnalyzer : public edm::one::EDAnalyzer<Capabilities...> {
19 public:
20  //typedefs to simplify usage
21  typedef typename Client::Input Input;
22  typedef typename Client::Output Output;
23  //constructor
25  : clientPset_(cfg.getParameterSet("Client")),
26  debugName_(cfg.getParameter<std::string>("@module_label")),
27  verbose_(clientPset_.getUntrackedParameter<bool>("verbose")) {
28  //ExternalWork is not compatible with one modules, so Sync mode is enforced
29  if (clientPset_.getParameter<std::string>("mode") != "Sync") {
30  edm::LogWarning("ResetClientMode") << "Resetting client mode to Sync for SonicOneEDAnalyzer";
31  clientPset_.addParameter<std::string>("mode", "Sync");
32  }
33  }
34  //destructor
35  ~SonicOneEDAnalyzer() override = default;
36 
37  //construct client at beginning of job
38  //in case client constructor depends on operations happening in derived module constructors
39  void beginJob() override { makeClient(); }
40 
41  //derived classes still use a dedicated acquire() interface that incorporates client_->input() for consistency
42  virtual void acquire(edm::Event const& iEvent, edm::EventSetup const& iSetup, Input& iInput) = 0;
43  //derived classes use a dedicated analyze() interface that incorporates client_->output()
44  void analyze(edm::Event const& iEvent, edm::EventSetup const& iSetup) final {
46  acquire(iEvent, iSetup, client_->input());
47  if (verbose_)
48  sonic_utils::printDebugTime(debugName_, "acquire() time: ", t0);
49 
50  //pattern similar to ExternalWork, but blocking
52  client_->dispatch();
53 
54  //measure time between acquire and produce
55  if (verbose_)
56  sonic_utils::printDebugTime(debugName_, "dispatch() time: ", t1);
57 
59  analyze(iEvent, iSetup, client_->output());
60  if (verbose_)
61  sonic_utils::printDebugTime(debugName_, "analyze() time: ", t2);
62 
63  //reset client data
64  client_->reset();
65  }
66  virtual void analyze(edm::Event const& iEvent, edm::EventSetup const& iSetup, Output const& iOutput) = 0;
67 
68 protected:
69  //helper
70  void makeClient() { client_ = std::make_unique<Client>(clientPset_, debugName_); }
71 
72  //members
74  std::unique_ptr<Client> client_;
76  bool verbose_;
77 };
78 
79 #endif
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
void beginJob() override
edm::ParameterSet clientPset_
void analyze(edm::Event const &iEvent, edm::EventSetup const &iSetup) final
bool verbose
Client::Output Output
virtual void acquire(edm::Event const &iEvent, edm::EventSetup const &iSetup, Input &iInput)=0
int iEvent
Definition: GenABIO.cc:224
void addParameter(std::string const &name, T const &value)
Definition: ParameterSet.h:135
ParameterSet const & getParameterSet(ParameterSetID const &id)
std::unique_ptr< Client > client_
SonicOneEDAnalyzer(edm::ParameterSet const &cfg, bool verbose=true)
void printDebugTime(std::string_view debugName, std::string_view msg, const TimePoint &t0)
Definition: sonic_utils.cc:8
Log< level::Warning, false > LogWarning
~SonicOneEDAnalyzer() override=default