CMS 3D CMS Logo

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