1818"""
1919
2020import os
21+ import sys
2122from ctypes import c_bool
2223from enum import Enum
2324
@@ -161,7 +162,7 @@ def mass(code):
161162
162163def declare_mass (pdg , val_type = "double" ) -> str :
163164 """Returns a C++ declaration of a particle mass constant."""
164- return f"constexpr { val_type } Mass{ pdg .name [1 :]} = { mass (pdg .value )} ;\n "
165+ return f"constexpr { val_type } Mass{ pdg .name [1 :]} = { mass (pdg .value )} ;"
165166
166167
167168def main ():
@@ -170,46 +171,88 @@ def main():
170171 name_script = os .path .basename (__file__ )
171172
172173 # Comment at the beginning of the output
173- str_block_begin = "// BEGINNING OF THE GENERATED BLOCK."
174+ block_begin = "// BEGINNING OF THE GENERATED BLOCK."
174175 # Preamble with instructions
175- str_block_preamble = (
176+ block_preamble = (
176177 "// DO NOT EDIT THIS BLOCK DIRECTLY!"
177178 f"\n // It has been generated by the { name_script } script."
178- "\n // For modifications, edit the script and generate this block again.\n "
179+ "\n // For modifications, edit the script and generate this block again."
179180 )
180181 # Comment at the end of the output
181- str_block_end = "// END OF THE GENERATED BLOCK"
182+ block_end = "// END OF THE GENERATED BLOCK"
182183 # Start of enum declarations of additional particles
183- str_enum_head = (
184+ enum_o2_head = (
184185 "/// \\ brief Declarations of named PDG codes of particles missing in ROOT PDG_t"
185186 "\n /// \\ note Follow kCamelCase naming convention"
186187 "\n /// \\ link https://root.cern/doc/master/TPDGCode_8h.html"
187188 "\n enum Pdg {"
188189 )
189190 # End of enum declarations of additional particles
190- str_enum_foot = "};\n "
191+ enum_o2_foot = "};"
191192 # Documentation string for mass declarations of additional particles
192- str_mass_o2_head = "/// \\ brief Declarations of masses for additional particles"
193+ mass_o2_head = "/// \\ brief Declarations of masses for additional particles"
193194 # Documentation string for mass declarations of PDG_t particles
194- str_mass_root_head = "/// \\ brief Declarations of masses for particles in ROOT PDG_t"
195+ mass_root_head = "/// \\ brief Declarations of masses for particles in ROOT PDG_t"
196+
197+ # Get header content before and after the generated block.
198+ path_header = "PhysicsConstants.h"
199+ try :
200+ with open (path_header , encoding = "utf-8" ) as file :
201+ content_old = file .readlines ()
202+ except OSError :
203+ print (f'Failed to open file "{ path_header } ".' )
204+ sys .exit (1 )
205+ lines_header_before : list [str ] = []
206+ lines_header_after : list [str ] = []
207+ got_block_begin = False
208+ got_block_end = False
209+ for line in content_old :
210+ line = line .strip ()
211+ if line == block_begin :
212+ got_block_begin = True
213+ if not got_block_begin :
214+ lines_header_before .append (line )
215+ if got_block_end :
216+ lines_header_after .append (line )
217+ if line == block_end :
218+ got_block_end = True
219+ if not got_block_begin :
220+ raise ValueError ("Did not find the beginning of the block." )
221+ if not got_block_end :
222+ raise ValueError ("Did not find the end of the block." )
195223
196224 # Additional particles
197- str_enum = str_enum_head
198- str_mass_o2 = str_mass_o2_head
199- for c in Pdg :
200- str_enum += f" { c .name } = { c .value } ,\n "
201- str_mass_o2 += declare_mass (c )
202- str_enum = str_enum [: - 2 ] + " \n " # Remove the last comma.
203- str_enum += str_enum_foot
225+ lines_enum_o2 : list [ str ] = [ enum_o2_head ]
226+ lines_mass_o2 : list [ str ] = [ mass_o2_head ]
227+ for pdg_o2 in Pdg :
228+ lines_enum_o2 . append ( f" { pdg_o2 .name } = { pdg_o2 .value } ," )
229+ lines_mass_o2 . append ( declare_mass (pdg_o2 ) )
230+ lines_enum_o2 [ - 1 ] = lines_enum_o2 [ - 1 ][: - 1 ] # Remove the last comma.
231+ lines_enum_o2 . append ( enum_o2_foot )
204232
205233 # PDG_t particles
206- str_mass_root = str_mass_root_head
207- for d in PdgROOT :
208- str_mass_root += declare_mass (d )
234+ lines_mass_root : list [ str ] = [ mass_root_head ]
235+ for pdg_root in PdgROOT :
236+ lines_mass_root . append ( declare_mass (pdg_root ) )
209237
210238 # Header body
211- str_header = "\n " .join ((str_block_begin , str_block_preamble , str_enum , str_mass_o2 , str_mass_root , str_block_end ))
212- print (str_header )
239+ content_new = "\n " .join (
240+ (
241+ * lines_header_before ,
242+ block_begin ,
243+ block_preamble ,
244+ "" ,
245+ * lines_enum_o2 ,
246+ "" ,
247+ * lines_mass_o2 ,
248+ "" ,
249+ * lines_mass_root ,
250+ "" ,
251+ block_end ,
252+ * lines_header_after ,
253+ )
254+ )
255+ print (content_new )
213256
214257
215258if __name__ == "__main__" :
0 commit comments