CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_7_hltpatch2/src/RecoLocalCalo/HcalRecAlgos/python/RemoveAddSevLevel.py

Go to the documentation of this file.
00001 import FWCore.ParameterSet.Config as cms
00002 
00003 
00004 def RemoveFlag(sevLevelComputer,flag="HFLongShort"):
00005     ''' Removes the specified flag from the Severity Level Computer,
00006     and returns the revised Computer.'''
00007     
00008     removeSeverity=-1 # Track which Severity Level has been modified
00009 
00010 
00011 
00012     # Loop over all levels
00013     for i in range(len(sevLevelComputer.SeverityLevels)):
00014         Flags=sevLevelComputer.SeverityLevels[i].RecHitFlags.value()
00015         if flag not in Flags:  # Flag not present for this level
00016             continue
00017         #Remove flag
00018         Flags.remove(flag)
00019         ChanStat=sevLevelComputer.SeverityLevels[i].ChannelStatus.value()
00020         # Check to see if Severity Level no longer contains any useful information
00021         if len(Flags)==0 and ChanStat==['']:
00022             removeSeverity=i
00023         else:
00024             # Set revised list of flags for this severity level
00025             sevLevelComputer.SeverityLevels[i].RecHitFlags=Flags
00026         break
00027 
00028     # Removing flag results in empty severity level; remove it
00029     if (removeSeverity>-1):
00030         sevLevelComputer.SeverityLevels.remove(sevLevelComputer.SeverityLevels[removeSeverity])
00031 
00032     return sevLevelComputer
00033 
00034 
00035 def PrintLevels(SLComp):
00036     print "Severity Level Computer Levels and associated flags/Channel Status values:"
00037     for i in SLComp.SeverityLevels:
00038         print "\t Level = %i"%i.Level.value()
00039         print "\t\t RecHit Flags = %s"%i.RecHitFlags.value()
00040         print "\t\t Channel Status = %s"%i.ChannelStatus.value()
00041         print
00042     return
00043 
00044 
00045 def AddFlag(sevLevelComputer,flag="UserDefinedBit0",severity=10):
00046     ''' Adds specified flag to severity level computer using specified severity level.
00047         If flag already exists at another severity level, it is removed from that level.
00048         '''
00049     
00050     AddedSeverity=False
00051     removeSeverity=-1
00052 
00053     allowedflags=[]
00054     for i in sevLevelComputer.SeverityLevels:
00055         for j in i.RecHitFlags.value():
00056             if j=="":
00057                 continue
00058             allowedflags.append(j)
00059             
00060     #print "Allowed flags = ",allowedflags
00061     if flag not in allowedflags:
00062         print "\n\n"
00063         for j in range(0,3):
00064             print "###################################################"
00065         print "\nWARNING!!!!!! You are adding a flag \n\t'%s' \nthat is not defined in the Severity Level Computer!"%flag
00066         print "This can be EXCEPTIONALLY dangerous if you do not \nknow what you are doing!\n"
00067         print "Proceed with EXTREME caution!\n"
00068         for j in range(0,3):
00069             print "###################################################"
00070         print "\n\n"
00071 
00072     #Loop over severity Levels
00073     for i in range(len(sevLevelComputer.SeverityLevels)):
00074         Level=sevLevelComputer.SeverityLevels[i].Level.value()
00075         Flags=sevLevelComputer.SeverityLevels[i].RecHitFlags.value()
00076         if Level==severity:  # Found the specified level
00077             if (Flags==['']):
00078                 Flags=[flag] # Create new vector for this flag
00079             else:
00080                 if flag not in Flags:  # don't need to add flag if it's already there
00081                     Flags.append(flag) # append flag to existing vector
00082             sevLevelComputer.SeverityLevels[i].RecHitFlags=Flags  # Set new RecHitFlags vector
00083             AddedSeverity=True
00084         else:  # Found some other level; be sure to remove flag from it
00085             if flag not in Flags:
00086                 continue
00087             else:
00088                 Flags.remove(flag)
00089                 # Removing flag leaves nothing else:  need to remove this level completely
00090                 if len(Flags)==0 and ChanStat==['']:
00091                     removeSeverity=i
00092                 else:
00093                     sevLevelComputer.SeverityLevels[i].RecHitFlags=Flags
00094 
00095     # Remove any newly-empty levels
00096     if (removeSeverity>-1):
00097         sevLevelComputer.SeverityLevels.remove(sevLevelComputer.SeverityLevels[removeSeverity])
00098 
00099     # No existing severity level for specified severity was found;
00100     # add a new one
00101     if (AddedSeverity==False):
00102         sevLevelComputer.SeverityLevels.append(cms.PSet(Level=cms.int32(severity),
00103                                                         RecHitFlags=cms.vstring(flag),
00104                                                         ChannelStatus=cms.vstring("")))
00105     return sevLevelComputer
00106 
00107 
00108     
00109 ##########################
00110 
00111 if __name__=="__main__":
00112     import hcalRecAlgoESProd_cfi as ES
00113     ES.hcalRecAlgos=RemoveFlag(ES.hcalRecAlgos)
00114     ES.hcalRecAlgos=AddFlag(ES.hcalRecAlgos,flag="HOBit",severity=5)
00115     PrintLevels(ES.hcalRecAlgos)
00116