CMS 3D CMS Logo

RemoveAddSevLevel.py
Go to the documentation of this file.
1 import FWCore.ParameterSet.Config as cms
2 
3 
4 def RemoveFlag(sevLevelComputer,flag="HFLongShort"):
5  ''' Removes the specified flag from the Severity Level Computer,
6  and returns the revised Computer.'''
7 
8  removeSeverity=-1 # Track which Severity Level has been modified
9 
10 
11 
12  # Loop over all levels
13  for i in range(len(sevLevelComputer.SeverityLevels)):
14  Flags=sevLevelComputer.SeverityLevels[i].RecHitFlags.value()
15  if flag not in Flags: # Flag not present for this level
16  continue
17  #Remove flag
18  Flags.remove(flag)
19  ChanStat=sevLevelComputer.SeverityLevels[i].ChannelStatus.value()
20  # Check to see if Severity Level no longer contains any useful information
21  if len(Flags)==0 and ChanStat==['']:
22  removeSeverity=i
23  else:
24  # Set revised list of flags for this severity level
25  sevLevelComputer.SeverityLevels[i].RecHitFlags=Flags
26  break
27 
28  # Removing flag results in empty severity level; remove it
29  if (removeSeverity>-1):
30  sevLevelComputer.SeverityLevels.remove(sevLevelComputer.SeverityLevels[removeSeverity])
31 
32  return sevLevelComputer
33 
34 
35 def PrintLevels(SLComp):
36  print "Severity Level Computer Levels and associated flags/Channel Status values:"
37  for i in SLComp.SeverityLevels:
38  print "\t Level = %i"%i.Level.value()
39  print "\t\t RecHit Flags = %s"%i.RecHitFlags.value()
40  print "\t\t Channel Status = %s"%i.ChannelStatus.value()
41  print
42  return
43 
44 
45 def AddFlag(sevLevelComputer,flag="UserDefinedBit0",severity=10):
46  ''' Adds specified flag to severity level computer using specified severity level.
47  If flag already exists at another severity level, it is removed from that level.
48  '''
49 
50  AddedSeverity=False
51  removeSeverity=-1
52 
53  allowedflags=[]
54  for i in sevLevelComputer.SeverityLevels:
55  for j in i.RecHitFlags.value():
56  if j=="":
57  continue
58  allowedflags.append(j)
59 
60  #print "Allowed flags = ",allowedflags
61  if flag not in allowedflags:
62  print "\n\n"
63  for j in range(0,3):
64  print "###################################################"
65  print "\nWARNING!!!!!! You are adding a flag \n\t'%s' \nthat is not defined in the Severity Level Computer!"%flag
66  print "This can be EXCEPTIONALLY dangerous if you do not \nknow what you are doing!\n"
67  print "Proceed with EXTREME caution!\n"
68  for j in range(0,3):
69  print "###################################################"
70  print "\n\n"
71 
72  #Loop over severity Levels
73  for i in range(len(sevLevelComputer.SeverityLevels)):
74  Level=sevLevelComputer.SeverityLevels[i].Level.value()
75  Flags=sevLevelComputer.SeverityLevels[i].RecHitFlags.value()
76  if Level==severity: # Found the specified level
77  if (Flags==['']):
78  Flags=[flag] # Create new vector for this flag
79  else:
80  if flag not in Flags: # don't need to add flag if it's already there
81  Flags.append(flag) # append flag to existing vector
82  sevLevelComputer.SeverityLevels[i].RecHitFlags=Flags # Set new RecHitFlags vector
83  AddedSeverity=True
84  else: # Found some other level; be sure to remove flag from it
85  if flag not in Flags:
86  continue
87  else:
88  Flags.remove(flag)
89  # Removing flag leaves nothing else: need to remove this level completely
90  if len(Flags)==0 and ChanStat==['']:
91  removeSeverity=i
92  else:
93  sevLevelComputer.SeverityLevels[i].RecHitFlags=Flags
94 
95  # Remove any newly-empty levels
96  if (removeSeverity>-1):
97  sevLevelComputer.SeverityLevels.remove(sevLevelComputer.SeverityLevels[removeSeverity])
98 
99  # No existing severity level for specified severity was found;
100  # add a new one
101  if (AddedSeverity==False):
102  sevLevelComputer.SeverityLevels.append(cms.PSet(Level=cms.int32(severity),
103  RecHitFlags=cms.vstring(flag),
104  ChannelStatus=cms.vstring("")))
105  return sevLevelComputer
106 
107 
108 
109 ##########################
110 
111 if __name__=="__main__":
112  import hcalRecAlgoESProd_cfi as ES
113  ES.hcalRecAlgos=RemoveFlag(ES.hcalRecAlgos)
114  ES.hcalRecAlgos=AddFlag(ES.hcalRecAlgos,flag="HOBit",severity=5)
115  PrintLevels(ES.hcalRecAlgos)
116 
def PrintLevels(SLComp)
def RemoveFlag(sevLevelComputer, flag="HFLongShort")
def AddFlag(sevLevelComputer, flag="UserDefinedBit0", severity=10)