PODIO v00-16-03
An Event-Data-Model Toolkit for High Energy Physics Experiments
Loading...
Searching...
No Matches
BenchmarkRecorder.h
Go to the documentation of this file.
1#ifndef PODIO_BENCHMARKRECORDER_H
2#define PODIO_BENCHMARKRECORDER_H
3
5
6#include "TFile.h"
7#include "TTree.h"
8
9#include <algorithm>
10#include <chrono>
11#include <deque>
12#include <string>
13#include <utility>
14#include <vector>
15
17
19public:
21 // Avoid some possible issues that could arise from copying by simply
22 // disallowing it
25
26 BenchmarkRecorderTree(TFile* recFile, const std::string& name, const std::vector<std::string>& steps) :
27 m_stepNames(steps), m_stepTimes(steps.size()) {
28 recFile->cd();
29 m_recordTree = new TTree(name.c_str(), "time recording tree");
30 m_recordTree->SetDirectory(recFile);
31
32 for (size_t i = 0; i < m_stepNames.size(); ++i) {
33 m_recordTree->Branch(m_stepNames[i].c_str(), &m_stepTimes[i]);
34 }
35 }
36
37 template <typename TimingResolution = std::chrono::nanoseconds>
38 void recordTime(const std::string& stepName, const ClockT::duration time) {
39 const auto it = std::find(m_stepNames.cbegin(), m_stepNames.cend(), stepName);
40 const auto index = std::distance(m_stepNames.cbegin(), it);
41 m_stepTimes[index] = std::chrono::duration_cast<TimingResolution>(time).count();
42 }
43
44 void Fill() {
45 m_recordTree->Fill();
46 }
47
48 void Write() {
49 m_recordTree->Write();
50 }
51
52private:
53 TTree* m_recordTree{nullptr};
54 std::vector<std::string> m_stepNames;
55 std::vector<double> m_stepTimes;
56};
57
59public:
61 BenchmarkRecorder(const std::string& recFileName = "podio_benchmark_file.root") {
62 m_recordFile = new TFile(recFileName.c_str(), "recreate");
63 }
64
67
69 for (auto& tree : m_recordTrees) {
70 tree.second.Write();
71 }
72 m_recordFile->Write("", TObject::kWriteDelete);
73 m_recordFile->Close();
74 }
75
76 template <typename TimingResolution = std::chrono::nanoseconds>
77 void recordTime(const std::string& treeName, const std::string& stepName, const ClockT::duration time) {
78 auto it = std::find_if(m_recordTrees.begin(), m_recordTrees.end(),
79 [&treeName](const auto& recTree) { return recTree.first == treeName; });
80
81 it->second.template recordTime<TimingResolution>(stepName, time);
82 }
83
84 void Fill(const std::string& treeName) {
85 auto it = std::find_if(m_recordTrees.begin(), m_recordTrees.end(),
86 [&treeName](const auto& recTree) { return recTree.first == treeName; });
87 it->second.Fill();
88 }
89
90 BenchmarkRecorderTree& addTree(const std::string& name, const std::vector<std::string>& steps) {
91 return m_recordTrees
92 .emplace_back(std::piecewise_construct, std::forward_as_tuple(name),
93 std::forward_as_tuple(m_recordFile, name, steps))
94 .second;
95 }
96
97private:
98 TFile* m_recordFile{nullptr};
99 // Stable references outside!!
100 std::deque<std::pair<std::string, BenchmarkRecorderTree>> m_recordTrees{};
101};
102
103} // namespace podio::benchmark
104
105#endif
BenchmarkRecorderTree & operator=(const BenchmarkRecorderTree &)=delete
void recordTime(const std::string &stepName, const ClockT::duration time)
BenchmarkRecorderTree(const BenchmarkRecorderTree &)=delete
BenchmarkRecorderTree(TFile *recFile, const std::string &name, const std::vector< std::string > &steps)
BenchmarkRecorder operator=(const BenchmarkRecorder &)=delete
BenchmarkRecorderTree & addTree(const std::string &name, const std::vector< std::string > &steps)
BenchmarkRecorder(const BenchmarkRecorder &)=delete
BenchmarkRecorder(const std::string &recFileName="podio_benchmark_file.root")
void Fill(const std::string &treeName)
void recordTime(const std::string &treeName, const std::string &stepName, const ClockT::duration time)