Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-05-01 06:57:19

0001 from __future__ import print_function
0002 from __future__ import absolute_import
0003 import FWCore.ParameterSet.Config as cms
0004 
0005 
0006 def RemoveFlag(sevLevelComputer,flag="HFLongShort"):
0007     ''' Removes the specified flag from the Severity Level Computer,
0008     and returns the revised Computer.'''
0009     
0010     removeSeverity=-1 # Track which Severity Level has been modified
0011 
0012 
0013 
0014     # Loop over all levels
0015     for i in range(len(sevLevelComputer.SeverityLevels)):
0016         Flags=sevLevelComputer.SeverityLevels[i].RecHitFlags.value()
0017         if flag not in Flags:  # Flag not present for this level
0018             continue
0019         #Remove flag
0020         Flags.remove(flag)
0021         ChanStat=sevLevelComputer.SeverityLevels[i].ChannelStatus.value()
0022         # Check to see if Severity Level no longer contains any useful information
0023         if len(Flags)==0 and ChanStat==['']:
0024             removeSeverity=i
0025         else:
0026             # Set revised list of flags for this severity level
0027             sevLevelComputer.SeverityLevels[i].RecHitFlags=Flags
0028         break
0029 
0030     # Removing flag results in empty severity level; remove it
0031     if (removeSeverity>-1):
0032         sevLevelComputer.SeverityLevels.remove(sevLevelComputer.SeverityLevels[removeSeverity])
0033 
0034     return sevLevelComputer
0035 
0036 
0037 def PrintLevels(SLComp):
0038     print("Severity Level Computer Levels and associated flags/Channel Status values:")
0039     for i in SLComp.SeverityLevels:
0040         print("\t Level = %i"%i.Level.value())
0041         print("\t\t RecHit Flags = %s"%i.RecHitFlags.value())
0042         print("\t\t Channel Status = %s"%i.ChannelStatus.value())
0043         print()
0044     return
0045 
0046 
0047 def AddFlag(sevLevelComputer,flag="UserDefinedBit0",severity=10,verbose=True):
0048     ''' Adds specified flag to severity level computer using specified severity level.
0049         If flag already exists at another severity level, it is removed from that level.
0050         '''
0051     
0052     AddedSeverity=False
0053     removeSeverity=-1
0054 
0055     allowedflags=[]
0056     for i in sevLevelComputer.SeverityLevels:
0057         for j in i.RecHitFlags.value():
0058             if j=="":
0059                 continue
0060             allowedflags.append(j)
0061             
0062     #print "Allowed flags = ",allowedflags
0063     if flag not in allowedflags and verbose:
0064         print("\n\n")
0065         for j in range(0,3):
0066             print("###################################################")
0067         print("\nWARNING!!!!!! You are adding a flag \n\t'%s' \nthat is not defined in the Severity Level Computer!"%flag)
0068         print("This can be EXCEPTIONALLY dangerous if you do not \nknow what you are doing!\n")
0069         print("Proceed with EXTREME caution!\n")
0070         for j in range(0,3):
0071             print("###################################################")
0072         print("\n\n")
0073 
0074     #Loop over severity Levels
0075     for i in range(len(sevLevelComputer.SeverityLevels)):
0076         Level=sevLevelComputer.SeverityLevels[i].Level.value()
0077         Flags=sevLevelComputer.SeverityLevels[i].RecHitFlags.value()
0078         if Level==severity:  # Found the specified level
0079             if (Flags==['']):
0080                 Flags=[flag] # Create new vector for this flag
0081             else:
0082                 if flag not in Flags:  # don't need to add flag if it's already there
0083                     Flags.append(flag) # append flag to existing vector
0084             sevLevelComputer.SeverityLevels[i].RecHitFlags=Flags  # Set new RecHitFlags vector
0085             AddedSeverity=True
0086         else:  # Found some other level; be sure to remove flag from it
0087             if flag not in Flags:
0088                 continue
0089             else:
0090                 Flags.remove(flag)
0091                 ChanStat=sevLevelComputer.SeverityLevels[i].ChannelStatus.value()
0092                 # Removing flag leaves nothing else:  need to remove this level completely
0093                 if len(Flags)==0 and ChanStat==['']:
0094                     removeSeverity=i
0095                 else:
0096                     sevLevelComputer.SeverityLevels[i].RecHitFlags=Flags
0097 
0098     # Remove any newly-empty levels
0099     if (removeSeverity>-1):
0100         sevLevelComputer.SeverityLevels.remove(sevLevelComputer.SeverityLevels[removeSeverity])
0101 
0102     # No existing severity level for specified severity was found;
0103     # add a new one
0104     if (AddedSeverity==False):
0105         sevLevelComputer.SeverityLevels.append(cms.PSet(Level=cms.int32(severity),
0106                                                         RecHitFlags=cms.vstring(flag),
0107                                                         ChannelStatus=cms.vstring("")))
0108     return sevLevelComputer
0109 
0110 
0111     
0112 ##########################
0113 
0114 if __name__=="__main__":
0115     from . import hcalRecAlgoESProd_cfi as ES
0116     ES.hcalRecAlgos=RemoveFlag(ES.hcalRecAlgos)
0117     ES.hcalRecAlgos=AddFlag(ES.hcalRecAlgos,flag="HOBit",severity=5)
0118     PrintLevels(ES.hcalRecAlgos)
0119