GCC Code Coverage Report


Directory: ./
File: examples/iguana_ex_cpp_dataframes.cc
Date: 2025-01-05 09:03:17
Exec Total Coverage
Lines: 29 31 93.5%
Functions: 3 3 100.0%
Branches: 34 68 50.0%

Line Branch Exec Source
1 /// @begin_doc_example{cpp}
2 /// @file iguana_ex_cpp_dataframes.cc
3 /// @brief Example using HIPO data frames with Iguana. Requires ROOT.
4 /// @par Usage
5 /// ```bash
6 /// iguana_ex_cpp_dataframes [HIPO_FILE] [NUM_EVENTS] [INTERACTIVE_MODE]
7 ///
8 /// HIPO_FILE the HIPO file to analyze
9 ///
10 /// NUM_EVENTS the number of events to analyze;
11 /// set to zero to analyze all events
12 ///
13 /// INTERACTIVE_MODE if 'true', plot will be drawn interactively,
14 /// otherwise it will be saved to a PNG file
15 /// (default = false)
16 /// ```
17 /// @end_doc_example
18
19 #include <hipo4/RHipoDS.hxx>
20 #include <iguana/algorithms/clas12/EventBuilderFilter/Algorithm.h>
21
22 #include <TApplication.h>
23 #include <TCanvas.h>
24
25 /// main function
26 1 int main(int argc, char** argv)
27 {
28
29 // parse arguments
30 int argi = 1;
31
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 char const* in_file = argc > argi ? argv[argi++] : "data.hipo";
32
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 int const num_events = argc > argi ? std::stoi(argv[argi++]) : 100;
33
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 bool const interactive_mode = argc > argi ? std::string(argv[argi++]) == "true" : false;
34
35 // iguana algorithms
36 1 iguana::clas12::EventBuilderFilter algo_eventbuilder_filter;
37
4/10
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
2 algo_eventbuilder_filter.SetOption<std::vector<int>>("pids", {11, 211, -211});
38
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 algo_eventbuilder_filter.Start();
39
40 // enable interactive mode
41
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1 auto app = interactive_mode ? new TApplication("app", &argc, argv) : nullptr;
42
43 // enable multi-threading
44 // ROOT::EnableImplicitMT();
45
46 // open the HIPO file
47
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 auto frame_init = MakeHipoDataFrame(in_file).Range(0, num_events);
48
49 // print the column names
50 1 fmt::print("DATAFRAME COLUMNS:\n");
51
3/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 298 times.
✓ Branch 3 taken 1 times.
299 for(auto const& column_name : frame_init.GetColumnNames())
52 299 fmt::print(" - {}\n", column_name);
53
54 // run algorithms
55 // FIXME: we want to be able to apply an Iguana Filter as an RDataFrame Filter to a set of columns;
56 // this example chain filters only `REC::Particle::pid`, whereas ideally we want all of `REC::Particle`'s columns
57 // to be filtered
58 auto frame_filtered = frame_init
59
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
2 .Define( // define a filter column, type std::deque<bool> (to avoid std::vector<bool>)
60 "REC_Particle_EventBuilderFilter",
61 100 [&](std::vector<int> const& pids)
62
1/2
✓ Branch 0 taken 100 times.
✗ Branch 1 not taken.
200 { return algo_eventbuilder_filter.Filter(pids); },
63 {"REC_Particle_pid"})
64
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
2 .Define( // apply the filtering column to `REC_Particle_pid`
65 "REC_Particle_pid_good",
66 100 [](std::vector<int> const& pids, std::deque<bool>& filter)
67 {
68 std::vector<int> result;
69
2/2
✓ Branch 0 taken 702 times.
✓ Branch 1 taken 100 times.
802 for(std::deque<bool>::size_type i = 0; i < filter.size(); i++) {
70
2/2
✓ Branch 0 taken 193 times.
✓ Branch 1 taken 509 times.
702 if(filter.at(i))
71
1/2
✓ Branch 0 taken 193 times.
✗ Branch 1 not taken.
193 result.push_back(pids.at(i));
72 }
73 100 return result;
74 },
75
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 {"REC_Particle_pid", "REC_Particle_EventBuilderFilter"});
76
77 // draw
78
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 auto hist = frame_filtered.Histo1D({"pid_filter", "PDG", 6000, -3000, 3000}, "REC_Particle_pid_good");
79
80 // write or hold open
81
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 auto canv = new TCanvas("canv", "canv", 1600, 1200);
82
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 canv->SetGrid(1, 1);
83
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 hist->Draw();
84
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if(interactive_mode) {
85 fmt::print("\n\nShowing plots interactively;\npress ^C to exit.\n\n");
86 app->Run();
87 }
88 else {
89
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 canv->SaveAs("out-iguana-dataframe-example.png");
90 }
91
92 return 0;
93 1 }
94