GCC Code Coverage Report


Directory: ./
File: examples/iguana_ex_cpp_dataframes.cc
Date: 2025-07-31 22:39:38
Exec Total Coverage
Lines: 29 31 93.5%
Functions: 3 3 100.0%
Branches: 33 68 48.5%

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
1/2
✓ Branch 0 (2→3) taken 1 times.
✗ Branch 1 (2→4) not taken.
1 char const* in_file = argc > 1 ? argv[1] : "data.hipo";
31
1/2
✓ Branch 0 (4→5) taken 1 times.
✗ Branch 1 (4→13) not taken.
1 int const num_events = argc > 2 ? std::stoi(argv[2]) : 100;
32
1/4
✗ Branch 0 (13→14) not taken.
✓ Branch 1 (13→21) taken 1 times.
✗ Branch 2 (15→16) not taken.
✗ Branch 3 (15→18) not taken.
1 bool const interactive_mode = argc > 3 ? std::string(argv[3]) == "true" : false;
33
34 // iguana algorithms
35 1 iguana::clas12::EventBuilderFilter algo_eventbuilder_filter;
36
4/10
✓ Branch 0 (22→23) taken 1 times.
✗ Branch 1 (22→116) not taken.
✓ Branch 2 (23→24) taken 1 times.
✗ Branch 3 (23→90) not taken.
✓ Branch 4 (24→25) taken 1 times.
✗ Branch 5 (24→84) not taken.
✓ Branch 6 (33→34) taken 1 times.
✗ Branch 7 (33→36) not taken.
✗ Branch 8 (90→91) not taken.
✗ Branch 9 (90→93) not taken.
2 algo_eventbuilder_filter.SetOption<std::vector<int>>("pids", {11, 211, -211});
37
1/2
✓ Branch 0 (36→37) taken 1 times.
✗ Branch 1 (36→116) not taken.
1 algo_eventbuilder_filter.Start();
38
39 // enable interactive mode
40
1/4
✗ Branch 0 (37→38) not taken.
✓ Branch 1 (37→40) taken 1 times.
✗ Branch 2 (39→40) not taken.
✗ Branch 3 (39→94) not taken.
1 auto app = interactive_mode ? new TApplication("app", &argc, argv) : nullptr;
41
42 // enable multi-threading
43 // ROOT::EnableImplicitMT();
44
45 // open the HIPO file
46
3/6
✓ Branch 0 (40→41) taken 1 times.
✗ Branch 1 (40→116) not taken.
✓ Branch 2 (41→42) taken 1 times.
✗ Branch 3 (41→96) not taken.
✓ Branch 4 (43→44) taken 1 times.
✗ Branch 5 (43→114) not taken.
1 auto frame_init = MakeHipoDataFrame(in_file).Range(0, num_events);
47
48 // print the column names
49 1 fmt::print("DATAFRAME COLUMNS:\n");
50
3/4
✓ Branch 0 (44→45) taken 1 times.
✗ Branch 1 (44→114) not taken.
✓ Branch 2 (50→46) taken 298 times.
✓ Branch 3 (50→51) taken 1 times.
299 for(auto const& column_name : frame_init.GetColumnNames())
51 299 fmt::print(" - {}\n", column_name);
52
53 // run algorithms
54 // FIXME: we want to be able to apply an Iguana Filter as an RDataFrame Filter to a set of columns;
55 // this example chain filters only `REC::Particle::pid`, whereas ideally we want all of `REC::Particle`'s columns
56 // to be filtered
57 auto frame_filtered = frame_init
58
2/4
✓ Branch 0 (52→53) taken 1 times.
✗ Branch 1 (52→114) not taken.
✓ Branch 2 (53→54) taken 1 times.
✗ Branch 3 (53→104) not taken.
2 .Define( // define a filter column, type std::deque<bool> (to avoid std::vector<bool>)
59 "REC_Particle_EventBuilderFilter",
60 100 [&](std::vector<int> const& pids)
61
1/2
✓ Branch 0 (3→4) taken 100 times.
✗ Branch 1 (3→8) not taken.
200 { return algo_eventbuilder_filter.Filter(pids); },
62 {"REC_Particle_pid"})
63
1/2
✓ Branch 0 (54→55) taken 1 times.
✗ Branch 1 (54→102) not taken.
2 .Define( // apply the filtering column to `REC_Particle_pid`
64 "REC_Particle_pid_good",
65 100 [](std::vector<int> const& pids, std::deque<bool>& filter)
66 {
67 std::vector<int> result;
68
2/2
✓ Branch 0 (13→3) taken 683 times.
✓ Branch 1 (13→14) taken 100 times.
1566 for(std::deque<bool>::size_type i = 0; i < filter.size(); i++) {
69
2/2
✓ Branch 0 (5→6) taken 201 times.
✓ Branch 1 (5→10) taken 482 times.
683 if(filter.at(i))
70
1/2
✓ Branch 0 (6→7) taken 201 times.
✗ Branch 1 (6→15) not taken.
201 result.push_back(pids.at(i));
71 }
72 100 return result;
73 },
74
1/2
✓ Branch 0 (55→56) taken 1 times.
✗ Branch 1 (55→100) not taken.
1 {"REC_Particle_pid", "REC_Particle_EventBuilderFilter"});
75
76 // draw
77
3/6
✓ Branch 0 (59→60) taken 1 times.
✗ Branch 1 (59→112) not taken.
✓ Branch 2 (60→61) taken 1 times.
✗ Branch 3 (60→106) not taken.
✓ Branch 4 (62→63) taken 1 times.
✗ Branch 5 (62→110) not taken.
1 auto hist = frame_filtered.Histo1D({"pid_filter", "PDG", 6000, -3000, 3000}, "REC_Particle_pid_good");
78
79 // write or hold open
80
1/2
✓ Branch 0 (63→64) taken 1 times.
✗ Branch 1 (63→108) not taken.
1 auto canv = new TCanvas("canv", "canv", 1600, 1200);
81
1/2
✓ Branch 0 (64→65) taken 1 times.
✗ Branch 1 (64→110) not taken.
1 canv->SetGrid(1, 1);
82
1/2
✓ Branch 0 (67→68) taken 1 times.
✗ Branch 1 (67→110) not taken.
1 hist->Draw();
83
1/2
✗ Branch 0 (68→69) not taken.
✓ Branch 1 (68→71) taken 1 times.
1 if(interactive_mode) {
84 fmt::print("\n\nShowing plots interactively;\npress ^C to exit.\n\n");
85 app->Run();
86 }
87 else {
88
1/2
✓ Branch 0 (71→72) taken 1 times.
✗ Branch 1 (71→110) not taken.
1 canv->SaveAs("out-iguana-dataframe-example.png");
89 }
90
91 return 0;
92 1 }
93