1+ # pylint: disable=missing-function-docstring, invalid-name
2+ """
3+ file: multitrial.py
4+ brief: Plot multitrial systematics based on multiple fit trials, one file per trial.
5+ usage: python3 multitrial.py config_multitrial.json
6+ author: Maja Karwowska <[email protected] >, Warsaw University of Technology 7+ """
18import argparse
29import glob
310import json
@@ -21,10 +28,11 @@ def get_yields(cfg):
2128 for filename in filenames :
2229 with TFile .Open (filename ) as fin :
2330 hist = fin .Get (cfg ["histoname" ])
24- dirname = re .search ( cfg [ "dir_pattern" ] , filename ). group ( 0 )
25- trial_name = re . split ( "_" , dirname )[ - 1 ]
31+ dirname = re .split ( "/" , filename )[ - 2 ]
32+ trial_name = dirname . replace ( cfg [ "dir_pattern" ], "" )
2633 trials .append (trial_name )
27- for ind , (pt_bin_min , pt_bin_max ) in enumerate (zip (cfg ["pt_bins_min" ], cfg ["pt_bins_max" ])):
34+ for ind , (pt_bin_min , pt_bin_max ) in enumerate (zip (cfg ["pt_bins_min" ],
35+ cfg ["pt_bins_max" ])):
2836 yields [f"{ pt_bin_min } _{ pt_bin_max } " ].append (hist .GetBinContent (ind + 1 ))
2937 yields_err [f"{ pt_bin_min } _{ pt_bin_max } " ].append (hist .GetBinError (ind + 1 ))
3038 print (f"final yields:\n { yields } \n trials:\n { trials } \n yields error:\n { yields_err } " )
@@ -48,14 +56,16 @@ def plot_yields_trials(yields, yields_err, trials, cfg, pt_string):
4856 c = "m" , linestyle = "-" , linewidth = 4.0 )
4957 #ax.set_xticks(ax.get_xticks()[::50])
5058 plt .savefig (f'{ cfg ["outdir" ]} /{ cfg ["outfile" ]} _yields_trials_{ pt_string } .png' )
59+ plt .close ()
5160
5261
5362def plot_yields_distr (yields , cfg , pt_string ):
5463 plt .figure (figsize = (20 , 15 ))
5564 ax = plt .subplot (1 , 1 , 1 )
5665 ax .set_xlabel (cfg ["y_axis" ])
57- plt .hist (yields , c = "b" , linewidth = 4.0 )
66+ plt .hist (yields [ pt_string ], color = "b" , linewidth = 4.0 )
5867 plt .savefig (f'{ cfg ["outdir" ]} /{ cfg ["outfile" ]} _distr_{ pt_string } .png' )
68+ plt .close ()
5969
6070
6171def main ():
@@ -75,9 +85,10 @@ def main():
7585 plot_yields_trials (yields , yields_err , trials , cfg , pt_string )
7686 plot_yields_distr (yields , cfg , pt_string )
7787
78- with open (f'{ cfg ["outdir" ]} /{ cfg ["outfile" ]} _trials_{ pt_bin_min } _{ pt_bin_max } .txt' , "w" ) as ftext :
79- for trial in trials :
80- f .write (f"{ trial } \n " )
88+ with open (f'{ cfg ["outdir" ]} /{ cfg ["outfile" ]} _trials.txt' ,
89+ "w" , encoding = "utf-8" ) as ftext :
90+ for trial in trials :
91+ ftext .write (f"{ trial } \n " )
8192
8293
8394if __name__ == "__main__" :
0 commit comments