289 """create a new AthAnalysisAlgorithm inside the current package. Call from within the package directory
292 $ acmd cmake new-analysisalg MyAlg
296 full_alg_name = args.algname
301 if not os.path.isdir(cwd+
"/src")
or not os.path.isfile(cwd+
"/CMakeLists.txt"):
302 print(
"ERROR you must call new-analysisalg from within the package you want to add the algorithm to")
306 full_pkg_name = os.path.basename(cwd)
307 print(textwrap.dedent(
"""\
308 ::: create alg [%(full_alg_name)s] in pkg [%(full_pkg_name)s]""" %locals()))
313 hasxAODEventInfo=
False
319 hasComponentLine=
False
320 for line
in open(
'CMakeLists.txt'):
322 if "atlas_add_library" in line: hasLibraryLine=
True
323 if "atlas_add_component" in line: hasComponentLine=
True
332 hdr = Templates.alg_hdr_template
333 cxx = Templates.alg_cxx_template
335 namespace_klass = full_alg_name.replace(
'::',
'__')
336 namespace_begin,namespace_end =
"",
""
338 if full_alg_name.count(
"::")>0:
339 namespace = full_alg_name.split(
"::")[0]
340 full_alg_name = full_alg_name.split(
"::")[1]
341 namespace_begin =
"namespace %s {" % namespace
342 namespace_end =
"} //> end namespace %s" % namespace
345 guard =
"%s_%s_H" % (full_pkg_name.upper(), namespace_klass.upper())
347 d = dict( pkg=full_pkg_name,
350 namespace_begin=namespace_begin,
351 namespace_end=namespace_end,namespace_klass=namespace_klass,namespace=namespace
353 fname = os.path.splitext(
"src/%s"%namespace_klass)[0]
355 if os.path.isfile(fname+
'.h'):
356 print(
"::: ERROR %s.h already exists" % fname)
358 print(
"::: INFO Creating %s.h" % fname)
359 o_hdr =
open(fname+
'.h',
'w')
360 o_hdr.writelines(hdr%d)
364 if os.path.isfile(fname+
'.cxx'):
365 print(
"::: ERROR %s.cxx already exists" % fname)
367 print(
"::: INFO Creating %s.cxx" % fname)
368 o_cxx =
open(fname+
'.cxx',
'w')
369 o_cxx.writelines(cxx%d)
376 if not os.path.exists(
"src/components"): os.mkdir(
"src/components")
377 if not os.path.isfile(
"src/components/%s_entries.cxx"%full_pkg_name):
378 print(
"::: INFO Creating src/components/%s_entries.cxx"%full_pkg_name)
379 loadFile =
open(
"src/components/%s_entries.cxx"%full_pkg_name,
'w')
380 if len(namespace_begin)>0:
381 d[
"namespace"] = args.algname.split(
"::")[0]
382 loadFile.writelines(
"""
383 #include "../%(namespace_klass)s.h"
384 DECLARE_COMPONENT(%(namespace)s::%(klass)s )
387 loadFile.writelines(
"""
388 #include "../%(namespace_klass)s.h"
389 DECLARE_COMPONENT( %(klass)s )
396 for line
in open(
"src/components/%s_entries.cxx"%full_pkg_name):
397 if len(namespace_begin)==0
and "DECLARE_COMPONENT" in line
and d[
"klass"]
in line: inFile=
True
398 if len(namespace_begin)>0
and "DECLARE_COMPONENT" in line
and d[
"klass"]
in line
and d[
"namespace"]: inFile=
True
401 print(
"::: INFO Adding %s to src/components/%s_entries.cxx"% (args.algname,full_pkg_name))
403 with open(
"src/components/%s_entries.cxx"%full_pkg_name,
"a")
as f:
404 if len(namespace_begin)>0:
405 f.write(
""" DECLARE_COMPONENT(%(namespace)s::%(klass)s );"""%d)
407 f.write(
""" DECLARE_COMPONENT( %(klass)s );"""%d)
412 full_jobo_name = namespace_klass +
"JobOptions"
413 full_script_name =
"run" + namespace_klass
414 full_alg_name = namespace_klass
416 print(textwrap.dedent(
"""\
417 ::: create jobo [%(full_jobo_name)s] and script [%(full_script_name)s] for alg [%(full_alg_name)s]""" %locals()))
420 jobo = Templates.jobo_template
422 e = dict( klass=full_alg_name,
423 inFile=os.environ[
'ASG_TEST_FILE_MC'],
425 fname =
'share/%s.py' % full_jobo_name
427 if os.path.isfile(fname):
428 print(
"::: WARNING %s already exists .. will not overwrite" % fname)
430 o_hdr =
open(fname,
'w')
431 o_hdr.writelines(jobo%e)
435 scripto = Templates.script_template
437 e = dict( klass=full_alg_name,
438 inFile=os.environ[
'ASG_TEST_FILE_MC'],
440 fname =
'scripts/%s.py' % full_script_name
442 if os.path.isfile(fname):
443 print(
"::: WARNING %s already exists .. will not overwrite" % fname)
445 o_hdr =
open(fname,
'w')
446 o_hdr.writelines(scripto%e)
449 os.chmod(fname, 0o755)
453 workDir = os.environ.get(
"WorkDir_DIR")
455 print(
"::: ERROR No WorkDir_DIR env var, did you forget to source the setup.sh script?")
456 print(
"::: ERROR Please do this and reconfigure cmake manually!")
458 print(
"::: INFO Reconfiguring cmake %s/../." % workDir)
459 res = subprocess.getstatusoutput(
'cmake %s/../.' % workDir)
461 print(
"::: WARNING reconfigure unsuccessful. Please reconfigure manually!")
464 print(
"::: INFO Please ensure your CMakeLists.txt file has ")
465 print(
"::: atlas_add_component( %s src/component/*.cxx ... )" % full_pkg_name)
466 print(
"::: INFO and necessary dependencies declared ")
467 print(
"::: INFO Minimum dependency is: Control/AthAnalysisBaseComps")