-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdemo.C
55 lines (46 loc) · 1.78 KB
/
demo.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <chrono>
#include <functional>
#include <iostream>
#include <string>
#include <vector>
#include "canvas/Utilities/InputTag.h"
#include "gallery/Event.h"
#include "TFile.h"
#include "TH1F.h"
#include "TInterpreter.h"
#include "TROOT.h"
using namespace art;
using namespace std;
using namespace std::chrono;
void
demo(std::string const& filename)
{
InputTag mctruths_tag{ "generator" };
// Create a vector of length 1, containing the given filename.
vector<string> filenames(1, filename);
// Don't do the following in compiled C++. This code relies on the
// interactive ROOT system to (implicitly) own the histograms we
// create on the heap. In a C++ program, this object would be leaked
// (the memory unrecoverable by the program).
TH1F* npart_hist =
new TH1F("npart", "Number of particles per MCTruth", 51, -0.5, 50.5);
auto start_time = system_clock::now();
// We'll record the time it takes to process each gallery::Event.
vector<microseconds> times;
for (gallery::Event ev(filenames); !ev.atEnd(); ev.next()) {
auto const t0 = system_clock::now();
auto const& mctruths =
*ev.getValidHandle<vector<simb::MCTruth>>(mctruths_tag);
if (!mctruths.empty())
npart_hist->Fill(mctruths[0].NParticles());
times.push_back(duration_cast<microseconds>(system_clock::now() - t0));
}
auto const elapsed_time =
duration_cast<milliseconds>(system_clock::now() - start_time);
auto const sum_times = accumulate(begin(times), end(times), microseconds(0));
cout << "Processed " << times.size() << " events in an average of "
<< sum_times.count() / times.size() << " microseconds/event\n";
cout << "Total processing time (including file opening, but not macro "
"compilation) was "
<< elapsed_time.count() << " milliseconds\n";
}