ATLAS Offline Software
Classes | Functions
python.hanwriter Namespace Reference

Classes

class  Document
 
class  HanAlgorithm
 
class  HanCannotCreateConf
 
class  HanCompositeAlgorithm
 
class  HanDir
 
class  HanHistogram
 
class  HanLimit
 
class  HanOutput
 
class  HanReference
 
class  HanThreshold
 
class  HanWritingError
 
class  Node
 

Functions

def _hanAddDQParameter (handocument, dqparameter, output=DQHanConfMaker._default_output)
 
def _findRoots (input=[], objecttype="DQRegion")
 
def _findAllDQBaseObjects (rootlist)
 
def writeHanConfiguration (filename='dq.han.config', roots=[])
 

Function Documentation

◆ _findAllDQBaseObjects()

def python.hanwriter._findAllDQBaseObjects (   rootlist)
private
This returns all DQBase objects reachable from the rootlist as a set.

Definition at line 696 of file hanwriter.py.

696 def _findAllDQBaseObjects(rootlist):
697  """
698  This returns all DQBase objects reachable from the rootlist as a set.
699  """
700  import operator
701 
702  def recurse(dqbase):
703  if dqbase is None:
704  return set()
705  if isinstance(dqbase, list):
706  return reduce(operator.ior, [recurse(x) for x in dqbase])
707  if not isinstance(dqbase, DQBase):
708  raise ValueError(
709  '%s is not a valid DQBase object; this should never happen' % dqbase)
710  retset = {dqbase}
711  for rel in dqbase.getAllRelatedObjects():
712  retset |= recurse(rel)
713  return retset
714 
715  topset = set()
716  for dqbase in toList(rootlist):
717  topset |= recurse(dqbase)
718  return list(topset)
719 
720 # Writes the han configuration to a file
721 # @param roots: the list of all roots of han trees
722 
723 

◆ _findRoots()

def python.hanwriter._findRoots (   input = [],
  objecttype = "DQRegion" 
)
private
This helper return the list of objects that are the roots in the tree represented by input.

Definition at line 675 of file hanwriter.py.

675 def _findRoots(input=[], objecttype="DQRegion"):
676  """
677  This helper return the list of objects that are the roots in the tree represented by input.
678  """
679  result = []
680  for o in input:
681  try:
682  if o.type == objecttype:
683  if len(o.isReferencedBy) == 0:
684  result += [o]
685  except Exception:
686  raise HanCannotCreateConf(
687  "object %s is not a valid DQBase object" % o)
688  return result
689 
690 # Gets all DQBase objects that corresponds to the input han objects
691 # @param rootlist: a list of han object trees
692 # @return: the list of all DQBaseObjects associated to each node of the input tree
693 # @note: This function is used internally and should not be used by users of this module
694 
695 

◆ _hanAddDQParameter()

def python.hanwriter._hanAddDQParameter (   handocument,
  dqparameter,
  output = DQHanConfMaker._default_output 
)
private
Add a DQParameter object to a HanDocument, Doing this it creates the tree of 
dir and output.

Definition at line 567 of file hanwriter.py.

567 def _hanAddDQParameter(handocument, dqparameter, output=DQHanConfMaker._default_output):
568  """
569  Add a DQParameter object to a HanDocument, Doing this it creates the tree of
570  dir and output.
571  """
572  try:
573  if dqparameter.type != "DQParameter": # This is not valid
574  raise HanCannotCreateConf(
575  "Object: "+str(dqparameter)+" type does not match 'DQParameter'")
576  # gets the directory structure of the histogram and add it, if needed to the document
577  histoname = dqparameter.getInput().split('/')
578  theDir = handocument
579  # add directories to han document if needed
580  for directory in histoname[0:len(histoname)-1]:
581  subdir = theDir.getSubDir(directory)
582  if subdir is None:
583  subdir = theDir.addSubDir(directory)
584  theDir = subdir
585  # prepare to build the han algorithm element
586  algo = dqparameter.getAlgorithm()
587  if algo is None:
588  raise Exception('DQParameter without DQAlgorithm')
589  dqalgoname, dqalgolibrary = algo.id, algo.getLibraryName()
590  # Build the han reference, if any, associated to this DQParameter
591  dqrefname, dqreference, refs = "", dqparameter.getReference(), []
592  if dqreference != [None]: # References are defined
593  refs = '[%s]' % (','.join(_.id for _ in dqreference))
594  for iref in dqreference:
595  ref = iref.id
596  # search if reference is already in the han document
597  if handocument.getReference(ref) is None:
598  # it's not found, let's create it
599  refcompletename = iref.getReference()
600  filename = refcompletename.rpartition(':')[0]
601  refhistoname = refcompletename.rpartition(':')[
602  2].lstrip('/')
603  hanref = HanReference(
604  ref, histogramname=refhistoname, file=filename,
605  info=iref.getAnnotation('info'))
606  handocument.appendChild(hanref)
607  dqrefname = '_'+ref
608  # build the han thresholds
609  # here it's quite tricky: we have to get two sets of DQThresholds and combine them in a single
610  # han thresholds element
611  warnings = toList(dqparameter.getWarnings())
612  errors = toList(dqparameter.getErrors())
613  thresholds, hanthreshold, thresholdsname = '', None, ''
614  if (warnings != [None]) and (errors != [None]):
615  # Found errors and warnings limits
616  thresholds = dqparameter.id.replace(
617  '_', '__').replace('/', '_')+'_thr'
618  hanthreshold = HanThreshold(thresholds)
619  thresholdsname = thresholds
620  thresholds = '_'+thresholds
621 
622  def getCorrespondingRed(warningname):
623  for e in errors:
624  if e.getAttribute('Name')[1] == warningname:
625  return e
626  return None
627  for warning in warnings:
628  warningname = warning.getAttribute('Name')[1]
629  error = getCorrespondingRed(warningname)
630  if error is None:
631  raise HanCannotCreateConf(
632  'Cannot find corresponding error for warning names: '+warning.id)
633  hanthreshold.addLimit(warningname, warning.getAttribute(
634  'Value')[1], error.getAttribute('Value')[1])
635  #print DQHanConfMaker._get_NodeType(hanthreshold.nodeType),hanthreshold.name
636  handocument.appendChild(hanthreshold)
637 
638  # build the parameter id string and key, value pairs
639  parameterstr = '_'.join([x.id for x in toList(
640  dqparameter.getAlgorithmParameters()) if x is not None])
641  parameters = {}
642  for x in toList(dqparameter.getAlgorithmParameters()):
643  if x is None:
644  continue
645  parameters[x.getName()[1]] = x.getValue()[1][0]
646  # create a unique string identifying this han algorithm element so far
647  hanalgoname = ("%s_%s%s%s%s" % (dqalgolibrary, dqalgoname,
648  dqrefname, thresholds.replace('/', '_'), parameterstr))
649  # Finally add the histogram han element
650  theDir.addHistogram(histogram=histoname[len(histoname)-1], algorithm=hanalgoname,
651  annotations=dqparameter.getAllAnnotations(), output=output, attributes=dqparameter.getAllAttributes())
652  # search the han algorithm in the document
653  if handocument.getAlgorithm(hanalgoname) is None:
654  # This algorithm has never been added before in the document
655  node = HanAlgorithm(name=hanalgoname, algoname=dqalgoname, libname=dqalgolibrary,
656  reference=refs, thresholds=thresholdsname, parameters=parameters)
657  handocument.appendChild(node)
658  # check for possible composite algorithms
659  compalg = algo.getCompositeAlgorithm()
660  if compalg is not None:
661  node = HanCompositeAlgorithm(name=compalg.id, subalgnames=compalg.getSubAlgNames(),
662  libnames=compalg.getLibNames())
663  handocument.appendChild(node)
664  except Exception as msg:
665  raise HanCannotCreateConf(
666  "Object: "+str(dqparameter)+" is not a valid DQParameter. Reason: "+str(msg))
667 
668 # Finds the root han objects of the input collection
669 # @param input: the list of objects @c input: a list of DQBase objects
670 # @raise HanCannotCreateConf: in case of errors
671 # @return: the list of roots
672 # @note: This function is used internally and should not be used by users of this module
673 
674 

◆ writeHanConfiguration()

def python.hanwriter.writeHanConfiguration (   filename = 'dq.han.config',
  roots = [] 
)
Writes the configuration to a han file. 
The configuration has to be passed as a list of roots in han configuration.

Definition at line 724 of file hanwriter.py.

724 def writeHanConfiguration(filename='dq.han.config', roots=[]):
725  """
726  Writes the configuration to a han file.
727  The configuration has to be passed as a list of roots in han configuration.
728  """
729  configList = _findAllDQBaseObjects(roots)
730  # First remove the configuration elements that cannot be saved as han
731  for element in configList:
732  try:
733  if not DQHanConfMaker._isAceptedHanElement(element):
734  print("==========WARNING===========")
735  print("Object: " + \
736  str(element) + \
737  " does not have a valid han representation, cannot save")
738  print("The object is referenced by:", element.isReferencedBy)
739  print("You may need to manually edit the configuration")
740  print("============================")
741  configList.remove(element)
742  except Exception:
743  print(element.isReferencedBy)
744  raise HanCannotCreateConf("Not valid object:" + str(element))
745  # Let's create the han document
746  doc = Document()
747  # Find all root regions
748  rootregions = _findRoots(configList)
749 
750  # Navigate through DQRegions tree and add it to the han document
751  def _addchild(hanfather, dqregion, handocument):
752  outputalgo = dqregion.getAlgorithm()
753  if handocument.getAlgorithm(outputalgo.id) is None:
754  # Add to the document the summary maker algorithm
755  # summary makers do not have thresholds or parameters
756  # @todo: in the future extend this to make in enough general to support parameter
757  handocument.appendChild(HanAlgorithm(
758  outputalgo.id, outputalgo.id, outputalgo.getLibraryName())) # , reference, thresholds)
759  output = hanfather.addOutput(dqregion.id, outputalgo.id, annotations=dqregion.getAllAnnotations(
760  ), attributes=dqregion.getAllAttributes())
761  # recursively add subregions
762  for subregion in dqregion.getSubRegions():
763  if subregion is not None:
764  _addchild(output, subregion, doc)
765  # Add each DQParameter to the han document with the helper
766  for parameter in dqregion.getDQParameters():
767  if parameter is not None:
768  _hanAddDQParameter(handocument, parameter,
769  output=output.getOutputPath())
770 
771  for region in rootregions:
772  _addchild(doc, region, doc)
773 
774  fileout = open(filename, 'w')
775  print(doc.toprettyhan(" "), file=fileout)
776 
replace
std::string replace(std::string s, const std::string &s2, const std::string &s3)
Definition: hcg.cxx:307
python.hanwriter._findRoots
def _findRoots(input=[], objecttype="DQRegion")
Definition: hanwriter.py:675
dq_make_web_display.recurse
def recurse(rdir, dqregion, ignorepath, reffile=None)
Definition: dq_make_web_display.py:23
reduce
void reduce(HepMC::GenEvent *ge, std::vector< HepMC::GenParticlePtr > toremove)
Remove unwanted particles from the event, collapsing the graph structure consistently.
Definition: FixHepMC.cxx:81
python.hanwriter._hanAddDQParameter
def _hanAddDQParameter(handocument, dqparameter, output=DQHanConfMaker._default_output)
Definition: hanwriter.py:567
python.hanwriter._findAllDQBaseObjects
def _findAllDQBaseObjects(rootlist)
Definition: hanwriter.py:696
histSizes.list
def list(name, path='/')
Definition: histSizes.py:38
CxxUtils::set
constexpr std::enable_if_t< is_bitmask_v< E >, E & > set(E &lhs, E rhs)
Convenience function to set bits in a class enum bitmask.
Definition: bitmask.h:232
print
void print(char *figname, TCanvas *c1)
Definition: TRTCalib_StrawStatusPlots.cxx:25
TCS::join
std::string join(const std::vector< std::string > &v, const char c=',')
Definition: Trigger/TrigT1/L1Topo/L1TopoCommon/Root/StringUtils.cxx:10
Trk::open
@ open
Definition: BinningType.h:40
str
Definition: BTagTrackIpAccessor.cxx:11
Trk::split
@ split
Definition: LayerMaterialProperties.h:38
python.hanwriter.writeHanConfiguration
def writeHanConfiguration(filename='dq.han.config', roots=[])
Definition: hanwriter.py:724