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

Classes

class  Templates

Functions

 main (args)

Variables

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 244 of file cmake_newanalysisalg.py.

244def main(args):
245 """create a new AthAnalysisAlgorithm inside the current package. Call from within the package directory
246
247 ex:
248 $ acmd cmake new-analysisalg MyAlg
249 """
250 sc = 0
251
252 full_alg_name = args.algname
253
254 #determine the package from the cwd
255 cwd = os.getcwd()
256 #check that src dir exists and CMakeLists.txt exists (i.e. this is a package)
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")
259 return -1
260
261
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()))
265
266
267 #first we must check that CMakeLists.txt file has the AthAnalysisBaseComps dependency in it
268 foundBaseComps=False
269 hasxAODEventInfo=False
270 hasAtlasROOT=False
271 hasAsgTools=False
272 lastUse=0
273 lineCount=0
274 hasLibraryLine=False
275 hasComponentLine=False
276 for line in open('CMakeLists.txt'):
277 lineCount +=1
278 if "atlas_add_library" in line: hasLibraryLine=True
279 if "atlas_add_component" in line: hasComponentLine=True
280
281#GOT THIS FAR WITH EDITING
282
283
284
285
286
287 #following code borrowed from gen_klass
288 hdr = Templates.alg_hdr_template
289 cxx = Templates.alg_cxx_template
290
291 namespace_klass = full_alg_name.replace('::','__')
292 namespace_begin,namespace_end = "",""
293 namespace = ""
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
299 pass
300
301 guard = "%s_%s_H" % (full_pkg_name.upper(), namespace_klass.upper())
302
303 d = dict( pkg=full_pkg_name,
304 klass=full_alg_name,
305 guard=guard,
306 namespace_begin=namespace_begin,
307 namespace_end=namespace_end,namespace_klass=namespace_klass,namespace=namespace
308 )
309 fname = os.path.splitext("src/%s"%namespace_klass)[0]
310 #first check doesn't exist
311 if os.path.isfile(fname+'.h'):
312 print("::: ERROR %s.h already exists" % fname)
313 return -1
314 print("::: INFO Creating %s.h" % fname)
315 o_hdr = open(fname+'.h', 'w')
316 o_hdr.writelines(hdr%d)
317 o_hdr.flush()
318 o_hdr.close()
319
320 if os.path.isfile(fname+'.cxx'):
321 print("::: ERROR %s.cxx already exists" % fname)
322 return -1
323 print("::: INFO Creating %s.cxx" % fname)
324 o_cxx = open(fname+'.cxx', 'w')
325 o_cxx.writelines(cxx%d)
326 o_cxx.flush()
327 o_cxx.close()
328
329
330 #now add the algorithm to the _entries.cxx file in the components folder
331 #first check they exist
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 )
341"""%d)
342 else:
343 loadFile.writelines("""
344#include "../%(namespace_klass)s.h"
345DECLARE_COMPONENT( %(klass)s )
346"""%d)
347 loadFile.flush()
348 loadFile.close()
349 else:
350 #first check algorithm not already in _entries file
351 inFile=False
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
355
356 if not inFile:
357 print("::: INFO Adding %s to src/components/%s_entries.cxx"% (args.algname,full_pkg_name))
358 nextAdd=True
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)
362 else:
363 f.write(""" DECLARE_COMPONENT( %(klass)s );"""%d)
364
365
366 full_script_name = "run" + namespace_klass
367 full_alg_name = namespace_klass
368
369 print(textwrap.dedent("""\
370 ::: create script [%(full_script_name)s] for alg [%(full_alg_name)s]""" %locals()))
371
372 e = dict( klass=full_alg_name,
373 inFile=os.environ['ASG_TEST_FILE_MC'],
374 )
375 fname = 'scripts/%s.py' % full_script_name
376 #first check doesn't exist
377 if os.path.isfile(fname):
378 print("::: WARNING %s already exists .. will not overwrite" % fname)
379 else:
380 o_hdr = open(fname, 'w')
381 o_hdr.writelines(Templates.script_template % e)
382 o_hdr.flush()
383 o_hdr.close()
384 os.chmod(fname, 0o755)
385
386 #need to reconfigure cmake so it knows about the new files
387 #rely on the WorkDir_DIR env var for this
388 workDir = os.environ.get("WorkDir_DIR")
389 if workDir is None:
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!")
392 else:
393 print("::: INFO Reconfiguring cmake %s/../." % workDir)
394 res = subprocess.getstatusoutput('cmake %s/../.' % workDir)
395 if res[0]!=0:
396 print("::: WARNING reconfigure unsuccessful. Please reconfigure manually!")
397
398
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")
403
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 10 of file cmake_newanalysisalg.py.

◆ __doc__

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

Definition at line 11 of file cmake_newanalysisalg.py.