245 """create a new AthAnalysisAlgorithm inside the current package. Call from within the package directory
248 $ acmd cmake new-analysisalg MyAlg
252 full_alg_name = args.algname
257 if not os.path.isdir(cwd+
"/src")
or not os.path.isfile(cwd+
"/CMakeLists.txt"):
258 print(
"ERROR you must call new-analysisalg from within the package you want to add the algorithm to")
262 full_pkg_name = os.path.basename(cwd)
263 print(textwrap.dedent(
"""\
264 ::: create alg [%(full_alg_name)s] in pkg [%(full_pkg_name)s]""" %locals()))
269 hasxAODEventInfo=
False
275 hasComponentLine=
False
276 for line
in open(
'CMakeLists.txt'):
278 if "atlas_add_library" in line: hasLibraryLine=
True
279 if "atlas_add_component" in line: hasComponentLine=
True
288 hdr = Templates.alg_hdr_template
289 cxx = Templates.alg_cxx_template
291 namespace_klass = full_alg_name.replace(
'::',
'__')
292 namespace_begin,namespace_end =
"",
""
294 if full_alg_name.count(
"::")>0:
295 namespace = full_alg_name.split(
"::")[0]
296 full_alg_name = full_alg_name.split(
"::")[1]
297 namespace_begin =
"namespace %s {" % namespace
298 namespace_end =
"} //> end namespace %s" % namespace
301 guard =
"%s_%s_H" % (full_pkg_name.upper(), namespace_klass.upper())
303 d = dict( pkg=full_pkg_name,
306 namespace_begin=namespace_begin,
307 namespace_end=namespace_end,namespace_klass=namespace_klass,namespace=namespace
309 fname = os.path.splitext(
"src/%s"%namespace_klass)[0]
311 if os.path.isfile(fname+
'.h'):
312 print(
"::: ERROR %s.h already exists" % fname)
314 print(
"::: INFO Creating %s.h" % fname)
315 o_hdr = open(fname+
'.h',
'w')
316 o_hdr.writelines(hdr%d)
320 if os.path.isfile(fname+
'.cxx'):
321 print(
"::: ERROR %s.cxx already exists" % fname)
323 print(
"::: INFO Creating %s.cxx" % fname)
324 o_cxx = open(fname+
'.cxx',
'w')
325 o_cxx.writelines(cxx%d)
332 if not os.path.exists(
"src/components"): os.mkdir(
"src/components")
333 if not os.path.isfile(
"src/components/%s_entries.cxx"%full_pkg_name):
334 print(
"::: INFO Creating src/components/%s_entries.cxx"%full_pkg_name)
335 loadFile = open(
"src/components/%s_entries.cxx"%full_pkg_name,
'w')
336 if len(namespace_begin)>0:
337 d[
"namespace"] = args.algname.split(
"::")[0]
338 loadFile.writelines(
"""
339#include "../%(namespace_klass)s.h"
340DECLARE_COMPONENT(%(namespace)s::%(klass)s )
343 loadFile.writelines(
"""
344#include "../%(namespace_klass)s.h"
345DECLARE_COMPONENT( %(klass)s )
352 for line
in open(
"src/components/%s_entries.cxx"%full_pkg_name):
353 if len(namespace_begin)==0
and "DECLARE_COMPONENT" in line
and d[
"klass"]
in line: inFile=
True
354 if len(namespace_begin)>0
and "DECLARE_COMPONENT" in line
and d[
"klass"]
in line
and d[
"namespace"]: inFile=
True
357 print(
"::: INFO Adding %s to src/components/%s_entries.cxx"% (args.algname,full_pkg_name))
359 with open(
"src/components/%s_entries.cxx"%full_pkg_name,
"a")
as f:
360 if len(namespace_begin)>0:
361 f.write(
""" DECLARE_COMPONENT(%(namespace)s::%(klass)s );"""%d)
363 f.write(
""" DECLARE_COMPONENT( %(klass)s );"""%d)
366 full_script_name =
"run" + namespace_klass
367 full_alg_name = namespace_klass
369 print(textwrap.dedent(
"""\
370 ::: create script [%(full_script_name)s] for alg [%(full_alg_name)s]""" %locals()))
372 e = dict( klass=full_alg_name,
373 inFile=os.environ[
'ASG_TEST_FILE_MC'],
375 fname =
'scripts/%s.py' % full_script_name
377 if os.path.isfile(fname):
378 print(
"::: WARNING %s already exists .. will not overwrite" % fname)
380 o_hdr = open(fname,
'w')
381 o_hdr.writelines(Templates.script_template % e)
384 os.chmod(fname, 0o755)
388 workDir = os.environ.get(
"WorkDir_DIR")
390 print(
"::: ERROR No WorkDir_DIR env var, did you forget to source the setup.sh script?")
391 print(
"::: ERROR Please do this and reconfigure cmake manually!")
393 print(
"::: INFO Reconfiguring cmake %s/../." % workDir)
394 res = subprocess.getstatusoutput(
'cmake %s/../.' % workDir)
396 print(
"::: WARNING reconfigure unsuccessful. Please reconfigure manually!")
399 print(
"::: INFO Please ensure your CMakeLists.txt file has ")
400 print(
"::: atlas_add_component( %s src/component/*.cxx ... )" % full_pkg_name)
401 print(
"::: INFO and necessary dependencies declared ")
402 print(
"::: INFO Minimum dependency is: Control/AthAnalysisBaseComps")