67def generate_cpp(point_class_names):
68 """Generate C++ build_time_windows() that reads events sequentially."""
69 includes = '#include "ShipMCTrack.h"\n'
70 for class_name in sorted(set(point_class_names.values())):
71 includes += f'#include "{class_name}.h"\n'
72
73
74 branch_addr_setup = ""
75 for branch_name, class_name in point_class_names.items():
76 branch_addr_setup += f" std::vector<{class_name}>* in_{branch_name} = nullptr;\n"
77 branch_addr_setup += f' chain.SetBranchAddress("{branch_name}", &in_{branch_name});\n'
78
79
80 out_decl = ""
81 for branch_name, class_name in point_class_names.items():
82 out_decl += f" std::vector<{class_name}> out_{branch_name};\n"
83 out_decl += f' out_tree.Branch("{branch_name}", &out_{branch_name});\n'
84
85
86 out_clear = ""
87 for branch_name in point_class_names:
88 out_clear += f" out_{branch_name}.clear();\n"
89
90
91 merge_points = ""
92 for branch_name in point_class_names:
93 merge_points += f"""
94 for (const auto& pt : *in_{branch_name}) {{
95 auto p = pt;
96 p.SetTime(pt.GetTime() + t_offset);
97 p.SetTrackID(pt.GetTrackID() + track_offset);
98 out_{branch_name}.push_back(std::move(p));
99 }}
100 total_points += in_{branch_name}->size();"""
101
102 return f"""{includes}
103
104int build_time_windows(
105 TChain& chain,
106 const std::vector<int>& window_sizes,
107 const std::vector<double>& all_time_offsets,
108 const char* output_path
109) {{
110 int n_windows = static_cast<int>(window_sizes.size());
111 int n_entries = static_cast<int>(chain.GetEntries());
112
113 // Set up input branch addresses
114 std::vector<ShipMCTrack>* in_tracks = nullptr;
115 chain.SetBranchAddress("MCTrack", &in_tracks);
116{branch_addr_setup}
117
118 // Set up output file and trees
119 TFile out_file(output_path, "RECREATE");
120 TTree out_tree("cbmsim", "Time window overlay");
121
122 std::vector<ShipMCTrack> out_tracks;
123 out_tree.Branch("MCTrack", &out_tracks);
124{out_decl}
125
126 TTree meta_tree("timewindow_meta", "Time window metadata");
127 int n_overlaid = 0, n_total_tracks = 0, n_total_points = 0;
128 meta_tree.Branch("n_overlaid", &n_overlaid);
129 meta_tree.Branch("n_total_tracks", &n_total_tracks);
130 meta_tree.Branch("n_total_points", &n_total_points);
131
132 std::vector<double> sub_time_offsets_v;
133 std::vector<int> sub_n_tracks_v, sub_original_indices_v;
134 meta_tree.Branch("sub_time_offsets", &sub_time_offsets_v);
135 meta_tree.Branch("sub_n_tracks", &sub_n_tracks_v);
136 meta_tree.Branch("sub_original_indices", &sub_original_indices_v);
137
138 // Build time windows by reading events sequentially
139 int cursor = 0;
140 int offset = 0;
141 for (int w = 0; w < n_windows; ++w) {{
142 int n_sub = window_sizes[w];
143
144 out_tracks.clear();
145{out_clear}
146 sub_time_offsets_v.clear();
147 sub_n_tracks_v.clear();
148 sub_original_indices_v.clear();
149
150 int track_offset = 0;
151 int total_points = 0;
152
153 for (int k = 0; k < n_sub; ++k) {{
154 int evt_idx = cursor % n_entries;
155 chain.GetEntry(evt_idx);
156 double t_offset = all_time_offsets[offset + k];
157
158 int nt = static_cast<int>(in_tracks->size());
159
160 // Merge MCTracks with time offset and re-indexing
161 for (int i = 0; i < nt; ++i) {{
162 auto t = (*in_tracks)[i];
163 t.SetStartT((*in_tracks)[i].GetStartT() + t_offset);
164 if ((*in_tracks)[i].GetMotherId() >= 0)
165 t.SetMotherId((*in_tracks)[i].GetMotherId() + track_offset);
166 t.SetTrackID(i + track_offset);
167 t.SetEventID(k);
168 out_tracks.push_back(std::move(t));
169 }}
170
171 // Merge Point branches{merge_points}
172
173 sub_time_offsets_v.push_back(t_offset);
174 sub_n_tracks_v.push_back(nt);
175 sub_original_indices_v.push_back(evt_idx);
176
177 track_offset += nt;
178 ++cursor;
179 }}
180
181 n_overlaid = n_sub;
182 n_total_tracks = static_cast<int>(out_tracks.size());
183 n_total_points = total_points;
184
185 out_tree.Fill();
186 meta_tree.Fill();
187
188 offset += n_sub;
189
190 if ((w + 1) % 10 == 0 || w == 0 || w == n_windows - 1) {{
191 std::cout << "\\rBuilding windows: " << (w + 1) << "/" << n_windows
192 << " (" << (100 * (w + 1) / n_windows) << "%)" << std::flush;
193 }}
194 }}
195 std::cout << std::endl;
196
197 out_file.Write();
198 out_file.Close();
199
200 std::cout << "Output written to " << output_path << std::endl;
201 std::cout << " cbmsim tree: " << n_windows << " time window events" << std::endl;
202 std::cout << " timewindow_meta tree: per-window metadata" << std::endl;
203
204 return 0;
205}}
206"""
207
208