CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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
24  SonicOneEDAnalyzer(edm::ParameterSet const& cfg, const std::string& debugName, bool verbose = true)
25  : clientPset_(cfg.getParameterSet("Client")), debugName_(debugName), verbose_(verbose) {
26  //ExternalWork is not compatible with one modules, so Sync mode is enforced
27  if (clientPset_.getParameter<std::string>("mode") != "Sync") {
28  edm::LogWarning("ResetClientMode") << "Resetting client mode to Sync for SonicOneEDAnalyzer";
29  clientPset_.addParameter<std::string>("mode", "Sync");
30  }
31  }
32  //destructor
33  ~SonicOneEDAnalyzer() override = default;
34 
35  //construct client at beginning of job
36  //in case client constructor depends on operations happening in derived module constructors
37  void beginJob() override { makeClient(); }
38 
39  //derived classes still use a dedicated acquire() interface that incorporates client_->input() for consistency
40  virtual void acquire(edm::Event const& iEvent, edm::EventSetup const& iSetup, Input& iInput) = 0;
41  //derived classes use a dedicated analyze() interface that incorporates client_->output()
42  void analyze(edm::Event const& iEvent, edm::EventSetup const& iSetup) final {
44  acquire(iEvent, iSetup, client_->input());
45  if (verbose_)
46  sonic_utils::printDebugTime(debugName_, "acquire() time: ", t0);
47 
48  //pattern similar to ExternalWork, but blocking
50  client_->dispatch();
51 
52  //measure time between acquire and produce
53  if (verbose_)
54  sonic_utils::printDebugTime(debugName_, "dispatch() time: ", t1);
55 
57  analyze(iEvent, iSetup, client_->output());
58  if (verbose_)
59  sonic_utils::printDebugTime(debugName_, "analyze() time: ", t2);
60 
61  //reset client data
62  client_->reset();
63  }
64  virtual void analyze(edm::Event const& iEvent, edm::EventSetup const& iSetup, Output const& iOutput) = 0;
65 
66 protected:
67  //helper
68  void makeClient() { client_ = std::make_unique<Client>(clientPset_, debugName_); }
69 
70  //members
72  std::unique_ptr<Client> client_;
74  bool verbose_;
75 };
76 
77 #endif
void beginJob() override
tuple cfg
Definition: looper.py:296
edm::ParameterSet clientPset_
void analyze(edm::Event const &iEvent, edm::EventSetup const &iSetup) final
SonicOneEDAnalyzer(edm::ParameterSet const &cfg, const std::string &debugName, bool verbose=true)
ParameterSet const & getParameterSet(ParameterSetID const &id)
Client::Output Output
virtual void acquire(edm::Event const &iEvent, edm::EventSetup const &iSetup, Input &iInput)=0
int iEvent
Definition: GenABIO.cc:224
static constexpr int verbose
void addParameter(std::string const &name, T const &value)
Definition: ParameterSet.h:135
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
std::unique_ptr< Client > client_
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