@@ -183,74 +183,47 @@ def fill_histograms(hists, chunk):
183
183
hists [(particle , "eta" )].fill (eta = eta )
184
184
185
185
186
- def serialize_histograms_to_json ( hists , outdir = "plots" ):
186
+ def save_histogram_json ( hist_obj , particle , var , filepath ):
187
187
"""
188
- Serialize histograms to JSON format suitable for Apache ECharts.
189
- Each histogram is saved as a separate JSON file with bin centers, counts, and metadata.
188
+ Save a single histogram to JSON file with the same name as the PNG.
190
189
"""
191
- json_dir = os . path . join ( outdir , "json" )
192
- os . makedirs ( json_dir , exist_ok = True )
190
+ # Get axis info
191
+ axis = hist_obj . axes [ 0 ]
193
192
194
- histogram_metadata = {}
193
+ # Get data
194
+ counts = hist_obj .values ()
195
+ bin_centers = axis .centers
195
196
196
- for (particle , var ), hist_obj in hists .items ():
197
- # Get the axis
198
- axis = hist_obj .axes [0 ]
199
-
200
- # Get bin edges and centers
201
- bin_edges = axis .edges
202
- bin_centers = axis .centers
203
-
204
- # Get histogram values (counts)
205
- counts = hist_obj .values ()
206
-
207
- # Create data structure for ECharts
208
- chart_data = {
209
- "metadata" : {
210
- "particle" : particle ,
211
- "variable" : var ,
212
- "particle_full_name" : particle_full_names [particle ],
213
- "x_label" : axis .label ,
214
- "y_label" : "Counts" ,
215
- "title" : f"{ particle_full_names [particle ]} - { var .replace ('_' , ' ' )} "
216
- },
217
- "data" : {
218
- "bin_edges" : bin_edges .tolist (),
219
- "bin_centers" : bin_centers .tolist (),
220
- "counts" : counts .tolist (),
221
- "entries" : int (hist_obj .sum ()),
222
- "mean" : float (hist_obj .mean ()),
223
- "std" : float (hist_obj .std ())
224
- },
225
- "echarts_series" : {
226
- "type" : "bar" ,
227
- "data" : [[float (center ), int (count )] for center , count in zip (bin_centers , counts )],
228
- "barWidth" : float (bin_centers [1 ] - bin_centers [0 ]) if len (bin_centers ) > 1 else 1.0
229
- }
230
- }
231
-
232
- # Save individual histogram JSON
233
- filename = f"{ particle } _{ var } .json"
234
- filepath = os .path .join (json_dir , filename )
235
- with open (filepath , 'w' ) as f :
236
- json .dump (chart_data , f , indent = 2 )
237
-
238
- print (f"Saved JSON: { filename } " )
239
-
240
- # Add to metadata collection
241
- histogram_metadata [f"{ particle } _{ var } " ] = {
242
- "file" : filename ,
243
- "particle" : particle ,
244
- "variable" : var ,
245
- "title" : chart_data ["metadata" ]["title" ]
246
- }
197
+ # Calculate statistics
198
+ total_entries = np .sum (counts )
199
+ if total_entries > 0 :
200
+ mean = np .sum (bin_centers * counts ) / total_entries
201
+ variance = np .sum (counts * (bin_centers - mean )** 2 ) / total_entries
202
+ std = np .sqrt (variance )
203
+ else :
204
+ mean = 0.0
205
+ std = 0.0
247
206
248
- # Save metadata file listing all histograms
249
- metadata_filepath = os .path .join (json_dir , "histogram_metadata.json" )
250
- with open (metadata_filepath , 'w' ) as f :
251
- json .dump (histogram_metadata , f , indent = 2 )
207
+ # Simple JSON structure
208
+ data = {
209
+ "particle" : particle ,
210
+ "variable" : var ,
211
+ "title" : f"{ particle_full_names [particle ]} - { var .replace ('_' , ' ' )} " ,
212
+ "x_label" : axis .label ,
213
+ "bins" : {
214
+ "edges" : axis .edges .tolist (),
215
+ "centers" : bin_centers .tolist (),
216
+ "counts" : counts .tolist ()
217
+ },
218
+ "stats" : {
219
+ "entries" : int (total_entries ),
220
+ "mean" : float (mean ),
221
+ "std" : float (std )
222
+ }
223
+ }
252
224
253
- print (f"Saved histogram metadata: histogram_metadata.json" )
225
+ with open (filepath , 'w' ) as f :
226
+ json .dump (data , f , indent = 2 )
254
227
255
228
256
229
def plot_histograms (hists , outdir = "plots" ):
@@ -276,12 +249,18 @@ def plot_histograms(hists, outdir="plots"):
276
249
fig .tight_layout ()
277
250
278
251
# Save with friendly particle name
279
- filename = f"{ particle } _{ var } .png"
280
- filepath = os .path .join (outdir , filename )
281
- plt .savefig (filepath , dpi = 100 )
252
+ filename_base = f"{ particle } _{ var } "
253
+
254
+ # Save PNG
255
+ png_filepath = os .path .join (outdir , f"{ filename_base } .png" )
256
+ plt .savefig (png_filepath , dpi = 100 )
282
257
plt .close (fig )
283
258
284
- print (f"Saved plot: { filename } " )
259
+ # Save JSON with same name
260
+ json_filepath = os .path .join (outdir , f"{ filename_base } .json" )
261
+ save_histogram_json (hist_obj , particle , var , json_filepath )
262
+
263
+ print (f"Saved: { filename_base } .png and { filename_base } .json" )
285
264
286
265
287
266
def main ():
@@ -348,14 +327,9 @@ def main():
348
327
349
328
print (f"Processed { events_processed } events in total." )
350
329
351
- # Generate plots
352
- print (f"Creating plots in directory: { args .outdir } " )
330
+ # Generate plots and JSON files
331
+ print (f"Creating plots and JSON files in directory: { args .outdir } " )
353
332
plot_histograms (hists , outdir = args .outdir )
354
-
355
- # Serialize histograms to JSON
356
- print (f"\n Serializing histograms to JSON..." )
357
- serialize_histograms_to_json (hists , outdir = args .outdir )
358
-
359
333
print ("\n Done!" )
360
334
361
335
0 commit comments