ATLAS Offline Software
Loading...
Searching...
No Matches
python.scripts.cmake_newanalysisalg Namespace Reference

Classes

class  Templates

Functions

 main (args)

Variables

str __version__ = "$Revision: 795362 $"
str __author__ = "Will Buttinger"
str __doc__ = "streamline and ease the creation of new AthAnalysisAlgorithm"

Function Documentation

◆ main()

python.scripts.cmake_newanalysisalg.main ( args)
create a new AthAnalysisAlgorithm inside the current package. Call from within the package directory

ex:
 $ acmd cmake new-analysisalg MyAlg

Definition at line 286 of file cmake_newanalysisalg.py.

286def main(args):
287 """create a new AthAnalysisAlgorithm inside the current package. Call from within the package directory
288
289 ex:
290 $ acmd cmake new-analysisalg MyAlg
291 """
292 sc = 0
293
294 full_alg_name = args.algname
295
296 #determine the package from the cwd
297 cwd = os.getcwd()
298 #check that src dir exists and CMakeLists.txt exists (i.e. this is a package)
299 if not os.path.isdir(cwd+"/src") or not os.path.isfile(cwd+"/CMakeLists.txt"):
300 print("ERROR you must call new-analysisalg from within the package you want to add the algorithm to")
301 return -1
302
303
304 full_pkg_name = os.path.basename(cwd)
305 print(textwrap.dedent("""\
306 ::: create alg [%(full_alg_name)s] in pkg [%(full_pkg_name)s]""" %locals()))
307
308
309 #first we must check that CMakeLists.txt file has the AthAnalysisBaseComps dependency in it
310 foundBaseComps=False
311 hasxAODEventInfo=False
312 hasAtlasROOT=False
313 hasAsgTools=False
314 lastUse=0
315 lineCount=0
316 hasLibraryLine=False
317 hasComponentLine=False
318 for line in open('CMakeLists.txt'):
319 lineCount +=1
320 if "atlas_add_library" in line: hasLibraryLine=True
321 if "atlas_add_component" in line: hasComponentLine=True
322
323#GOT THIS FAR WITH EDITING
324
325
326
327
328
329 #following code borrowed from gen_klass
330 hdr = Templates.alg_hdr_template
331 cxx = Templates.alg_cxx_template
332
333 namespace_klass = full_alg_name.replace('::','__')
334 namespace_begin,namespace_end = "",""
335 namespace = ""
336 if full_alg_name.count("::")>0:
337 namespace = full_alg_name.split("::")[0]
338 full_alg_name = full_alg_name.split("::")[1]
339 namespace_begin = "namespace %s {" % namespace
340 namespace_end = "} //> end namespace %s" % namespace
341 pass
342
343 guard = "%s_%s_H" % (full_pkg_name.upper(), namespace_klass.upper())
344
345 d = dict( pkg=full_pkg_name,
346 klass=full_alg_name,
347 guard=guard,
348 namespace_begin=namespace_begin,
349 namespace_end=namespace_end,namespace_klass=namespace_klass,namespace=namespace
350 )
351 fname = os.path.splitext("src/%s"%namespace_klass)[0]
352 #first check doesn't exist
353 if os.path.isfile(fname+'.h'):
354 print("::: ERROR %s.h already exists" % fname)
355 return -1
356 print("::: INFO Creating %s.h" % fname)
357 o_hdr = open(fname+'.h', 'w')
358 o_hdr.writelines(hdr%d)
359 o_hdr.flush()
360 o_hdr.close()
361
362 if os.path.isfile(fname+'.cxx'):
363 print("::: ERROR %s.cxx already exists" % fname)
364 return -1
365 print("::: INFO Creating %s.cxx" % fname)
366 o_cxx = open(fname+'.cxx', 'w')
367 o_cxx.writelines(cxx%d)
368 o_cxx.flush()
369 o_cxx.close()
370
371
372 #now add the algorithm to the _entries.cxx file in the components folder
373 #first check they exist
374 if not os.path.exists("src/components"): os.mkdir("src/components")
375 if not os.path.isfile("src/components/%s_entries.cxx"%full_pkg_name):
376 print("::: INFO Creating src/components/%s_entries.cxx"%full_pkg_name)
377 loadFile = open("src/components/%s_entries.cxx"%full_pkg_name,'w')
378 if len(namespace_begin)>0:
379 d["namespace"] = args.algname.split("::")[0]
380 loadFile.writelines("""
381#include "../%(namespace_klass)s.h"
382DECLARE_COMPONENT(%(namespace)s::%(klass)s )
383"""%d)
384 else:
385 loadFile.writelines("""
386#include "../%(namespace_klass)s.h"
387DECLARE_COMPONENT( %(klass)s )
388"""%d)
389 loadFile.flush()
390 loadFile.close()
391 else:
392 #first check algorithm not already in _entries file
393 inFile=False
394 for line in open("src/components/%s_entries.cxx"%full_pkg_name):
395 if len(namespace_begin)==0 and "DECLARE_COMPONENT" in line and d["klass"] in line: inFile=True
396 if len(namespace_begin)>0 and "DECLARE_COMPONENT" in line and d["klass"] in line and d["namespace"]: inFile=True
397
398 if not inFile:
399 print("::: INFO Adding %s to src/components/%s_entries.cxx"% (args.algname,full_pkg_name))
400 nextAdd=True
401 with open("src/components/%s_entries.cxx"%full_pkg_name, "a") as f:
402 if len(namespace_begin)>0:
403 f.write(""" DECLARE_COMPONENT(%(namespace)s::%(klass)s );"""%d)
404 else:
405 f.write(""" DECLARE_COMPONENT( %(klass)s );"""%d)
406
407
408 if args.newJobo:
409 #make the joboptions file too
410 full_jobo_name = namespace_klass + "JobOptions"
411 full_script_name = "run" + namespace_klass
412 full_alg_name = namespace_klass
413
414 print(textwrap.dedent("""\
415 ::: create jobo [%(full_jobo_name)s] and script [%(full_script_name)s] for alg [%(full_alg_name)s]""" %locals()))
416
417 #following code borrowed from gen_klass
418 jobo = Templates.jobo_template
419
420 e = dict( klass=full_alg_name,
421 inFile=os.environ['ASG_TEST_FILE_MC'],
422 )
423 fname = 'share/%s.py' % full_jobo_name
424 #first check doesn't exist
425 if os.path.isfile(fname):
426 print("::: WARNING %s already exists .. will not overwrite" % fname)
427 else:
428 o_hdr = open(fname, 'w')
429 o_hdr.writelines(jobo%e)
430 o_hdr.flush()
431 o_hdr.close()
432
433 scripto = Templates.script_template
434
435 e = dict( klass=full_alg_name,
436 inFile=os.environ['ASG_TEST_FILE_MC'],
437 )
438 fname = 'scripts/%s.py' % full_script_name
439 #first check doesn't exist
440 if os.path.isfile(fname):
441 print("::: WARNING %s already exists .. will not overwrite" % fname)
442 else:
443 o_hdr = open(fname, 'w')
444 o_hdr.writelines(scripto%e)
445 o_hdr.flush()
446 o_hdr.close()
447 os.chmod(fname, 0o755)
448
449 #need to reconfigure cmake so it knows about the new files
450 #rely on the WorkDir_DIR env var for this
451 workDir = os.environ.get("WorkDir_DIR")
452 if workDir is None:
453 print("::: ERROR No WorkDir_DIR env var, did you forget to source the setup.sh script?")
454 print("::: ERROR Please do this and reconfigure cmake manually!")
455 else:
456 print("::: INFO Reconfiguring cmake %s/../." % workDir)
457 res = subprocess.getstatusoutput('cmake %s/../.' % workDir)
458 if res[0]!=0:
459 print("::: WARNING reconfigure unsuccessful. Please reconfigure manually!")
460
461
462 print("::: INFO Please ensure your CMakeLists.txt file has ")
463 print("::: atlas_add_component( %s src/component/*.cxx ... )" % full_pkg_name)
464 print("::: INFO and necessary dependencies declared ")
465 print("::: INFO Minimum dependency is: Control/AthAnalysisBaseComps")
466
void print(char *figname, TCanvas *c1)
int main()
Definition hello.cxx:18

Variable Documentation

◆ __author__

str python.scripts.cmake_newanalysisalg.__author__ = "Will Buttinger"
private

Definition at line 11 of file cmake_newanalysisalg.py.

◆ __doc__

str python.scripts.cmake_newanalysisalg.__doc__ = "streamline and ease the creation of new AthAnalysisAlgorithm"
private

Definition at line 12 of file cmake_newanalysisalg.py.

◆ __version__

str python.scripts.cmake_newanalysisalg.__version__ = "$Revision: 795362 $"
private

Definition at line 10 of file cmake_newanalysisalg.py.