Conditions | 2 |
Total Lines | 78 |
Code Lines | 36 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | from ..psf_fast import PSFWrapper, InvalidPSF |
||
53 | @classmethod |
||
54 | def from_ttree(cls, open_ttree, dec_id, analysis_bin_id, log_log_spectrum, min_dec, dec_center, max_dec): |
||
55 | |||
56 | from ..root_handler import ROOT |
||
57 | |||
58 | # Compute the labels as used in the response file |
||
59 | dec_id_label = "dec_%02i" % dec_id |
||
60 | analysis_bin_id_label = "nh_%02i" % analysis_bin_id |
||
61 | |||
62 | # Read the histogram of the simulated events detected in this bin_name |
||
63 | # NOTE: we do not copy this TH1D instance because we won't use it after the |
||
64 | # file is closed |
||
65 | |||
66 | this_en_sig_th1d, nh_name = cls._get_en_th1d(open_ttree, dec_id, dec_id_label, analysis_bin_id, analysis_bin_id_label, |
||
67 | 'Sig') |
||
68 | |||
69 | # The sum of the histogram is the total number of simulated events detected |
||
70 | # in this analysis bin_name |
||
71 | sim_n_sig_events = this_en_sig_th1d.Integral() |
||
72 | |||
73 | # Now let's see what has been simulated, i.e., the differential flux |
||
74 | # at the center of each bin_name of the en_sig histogram |
||
75 | sim_energy_bin_low = np.zeros(this_en_sig_th1d.GetNbinsX()) |
||
76 | sim_energy_bin_centers = np.zeros(this_en_sig_th1d.GetNbinsX()) |
||
77 | sim_energy_bin_hi = np.zeros(this_en_sig_th1d.GetNbinsX()) |
||
78 | sim_signal_events_per_bin = np.zeros_like(sim_energy_bin_centers) |
||
79 | sim_differential_photon_fluxes = np.zeros_like(sim_energy_bin_centers) |
||
80 | |||
81 | for i in range(sim_energy_bin_centers.shape[0]): |
||
82 | # Remember: bin_name 0 is the underflow bin_name, that is why there |
||
83 | # is a "i+1" and not just "i" |
||
84 | bin_lo = this_en_sig_th1d.GetBinLowEdge(i+1) |
||
85 | bin_center = this_en_sig_th1d.GetBinCenter(i + 1) |
||
86 | bin_hi = this_en_sig_th1d.GetBinWidth(i+1) + bin_lo |
||
87 | |||
88 | # Store the center of the logarithmic bin_name |
||
89 | sim_energy_bin_low[i] = 10 ** bin_lo # TeV |
||
90 | sim_energy_bin_centers[i] = 10 ** bin_center # TeV |
||
91 | sim_energy_bin_hi[i] = 10 ** bin_hi # TeV |
||
92 | |||
93 | # Get from the simulated spectrum the value of the differential flux |
||
94 | # at the center energy |
||
95 | sim_differential_photon_fluxes[i] = 10 ** log_log_spectrum.Eval(bin_center) # TeV^-1 cm^-1 s^-1 |
||
96 | |||
97 | # Get from the histogram the detected events in each log-energy bin_name |
||
98 | sim_signal_events_per_bin[i] = this_en_sig_th1d.GetBinContent(i + 1) |
||
99 | |||
100 | # Read the histogram of the bkg events detected in this bin_name |
||
101 | # NOTE: we do not copy this TH1D instance because we won't use it after the |
||
102 | # file is closed |
||
103 | |||
104 | this_en_bg_th1d, _ = cls._get_en_th1d(open_ttree, dec_id, dec_id_label, analysis_bin_id, analysis_bin_id_label, |
||
105 | 'Bg') |
||
106 | |||
107 | # The sum of the histogram is the total number of simulated events detected |
||
108 | # in this analysis bin_name |
||
109 | sim_n_bg_events = this_en_bg_th1d.Integral() |
||
110 | |||
111 | # Now read the various TF1(s) for PSF, signal and background |
||
112 | |||
113 | # Read the PSF and make a copy (so it will stay when we close the file) |
||
114 | |||
115 | psf_label_tf1 = "PSF_dec%i_%s_fit" % (dec_id, nh_name) |
||
116 | |||
117 | psf_path = "%s/%s/%s" % (dec_id_label, analysis_bin_id_label, psf_label_tf1) |
||
118 | |||
119 | tf1 = ROOT.TF1() |
||
120 | |||
121 | open_ttree.GetObject(psf_path, tf1) |
||
122 | |||
123 | psf_fun = PSFWrapper.from_TF1(tf1) |
||
124 | |||
125 | return cls(analysis_bin_id, min_dec, max_dec, dec_center, sim_n_sig_events, sim_n_bg_events, |
||
126 | sim_energy_bin_low, |
||
127 | sim_energy_bin_centers, |
||
128 | sim_energy_bin_hi, |
||
129 | sim_differential_photon_fluxes, sim_signal_events_per_bin, |
||
130 | psf_fun) |
||
131 | |||
240 | return self._sim_signal_events_per_bin |
||
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.