~vpec/maus/tof_calib_read

« back to all changes in this revision

Viewing changes to workers/CIS/.svn/text-base/ReadCIS.py.svn-base

  • Committer: tunnell
  • Date: 2010-09-20 10:52:02 UTC
  • Revision ID: tunnell@itchy-20100920105202-ce4w9jm59zvgnsxq
moving stuff from tucs

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Author: Christopher Tunnell <tunnell@hep.uchicago.edu>
 
2
#
 
3
# March 04, 2009
 
4
#
 
5
 
 
6
from src.ReadGenericCalibration import *
 
7
from src.region import *
 
8
from math import *
 
9
 
 
10
class ReadCIS(ReadGenericCalibration):
 
11
    "Read in CIS calibration factors and quality flags from CIS calibration ntuples"
 
12
 
 
13
    def __init__(self, processingDir='/afs/cern.ch/user/t/tilecali/w0/ntuples/cis', turnOffStatCuts=False):
 
14
        self.processingDir = processingDir
 
15
        self.ftDict = {} # Used for linear constant.  Each element is a [TTree, TFile]
 
16
        self.badRuns = set()
 
17
        self.turnOffStatCuts = turnOffStatCuts
 
18
 
 
19
    def get_index(self, ros, mod, chan, gain):
 
20
        return ros  *64*48*2\
 
21
            + mod      *48*2\
 
22
            + chan        *2\
 
23
            + gain
 
24
 
 
25
    def ProcessStart(self):
 
26
        pass
 
27
 
 
28
    def ProcessStop(self):
 
29
        pass
 
30
    
 
31
    def ProcessRegion(self, region):
 
32
        # See if it's a gain
 
33
        newevents = set()
 
34
        if 'gain' not in region.GetHash():
 
35
            for event in region.GetEvents():
 
36
                if event.runType != 'staging':
 
37
                    newevents.add(event)
 
38
            region.SetEvents(newevents)
 
39
            return
 
40
 
 
41
        # Prepare events
 
42
        foundEventToProcess = False
 
43
        for event in region.GetEvents():
 
44
            if event.runType == 'staging':
 
45
                foundEventToProcess = True
 
46
            
 
47
                if event.runNumber and\
 
48
                       not self.ftDict.has_key(event.runNumber):
 
49
                    f, t = self.getFileTree('tileCalibCIS_%s_CIS.0.root' % event.runNumber, 'h3000')
 
50
 
 
51
                    if [f, t] == [None, None]:
 
52
                        f, t = self.getFileTree('tileCalibCIS_%s_0.root' % event.runNumber, 'h3000')
 
53
 
 
54
                        if [f, t] == [None, None]:
 
55
                            f, t = self.getFileTree('tileCalibTool_%s_CIS.0.root' % event.runNumber, 'h3000')
 
56
                                                    
 
57
                            if [f, t] == [None, None]:
 
58
                                if event.runNumber not in self.badRuns:
 
59
                                    print "Error: ReadCISFile couldn't load run", event.runNumber, "..."
 
60
                                    self.badRuns.add(event.runNumber)
 
61
                                continue
 
62
                    if hasattr(t, 'nDigitalErrors'):
 
63
                        self.ftDict[event.runNumber] = [f, t]
 
64
                    #elif event.runNumber not in self.badRuns: 
 
65
                    #    print '\tRun %s has old format' % event.runNumber
 
66
                    #    self.badRuns.add(event.runNumber) 
 
67
 
 
68
        if not foundEventToProcess:
 
69
            return
 
70
 
 
71
        for event in region.GetEvents():
 
72
            if event.runType != 'staging':
 
73
                newevents.add(event)
 
74
            else:
 
75
                # Load up the tree, file
 
76
                if not self.ftDict.has_key(event.runNumber):
 
77
                    continue 
 
78
                [f, t] = self.ftDict[event.runNumber]
 
79
                t.GetEntry(0) 
 
80
        
 
81
                # Get the index of this region within the file
 
82
                [x, y, z, w] = region.GetNumber()
 
83
                index = self.get_index(x, y - 1, z, w)
 
84
 
 
85
                # See if there were even injections
 
86
                if int(t.qflag[index]) & 3 != 3:
 
87
                    continue
 
88
 
 
89
                # Start filling the data for this event
 
90
                data = {}
 
91
                
 
92
                data['calibration'] = t.calib[index]
 
93
                
 
94
                data['quality_flag'] =  int(t.qflag[index])  #tk remove me temoporary backward compatibility!
 
95
 
 
96
#                if event.runNumber < 100489 and\
 
97
#                       int(t.qflag[index]) & 7 == 7:
 
98
#                    data['isBad'] = False
 
99
#                elif event.runNumber >= 100489 and\
 
100
#                         int(t.qflag[index]) & 27 == 27:
 
101
#                    data['isBad'] = False
 
102
#                else:
 
103
#                    data['isBad'] = True
 
104
                                                                                                                        
 
105
                data['nDigitalErrors'] = int(t.nDigitalErrors[index])
 
106
 
 
107
                # Define problems for the channel
 
108
                problems = {}
 
109
                
 
110
                data['chi2'] =   t.chi2[index]
 
111
                
 
112
                # Check for digital errors
 
113
                if int(t.qflag[index]) & 64 != 64:
 
114
                    assert(data['nDigitalErrors'] != 0)
 
115
                    problems['Digital Errors'] = True
 
116
                else:
 
117
                    problems['Digital Errors'] = False
 
118
 
 
119
                # Maximum point in fit range
 
120
                if int(t.qflag[index]) & 16 != 16:
 
121
                    problems['Fail Max. Point'] = True
 
122
                else:
 
123
                    problems['Fail Max. Point'] = False
 
124
 
 
125
                # Maximum point in fit range
 
126
                if int(t.qflag[index]) & 8 != 8:
 
127
                    problems['Fail Likely Calib.'] = True
 
128
                else:
 
129
                    problems['Fail Likely Calib.'] = False
 
130
                    
 
131
                if not self.turnOffStatCuts:
 
132
                    # Large RMS cut
 
133
                    if int(t.qflag[index]) & 32 != 32:
 
134
                        problems['Large Injection RMS'] = True
 
135
                    else:
 
136
                        problems['Large Injection RMS'] = False
 
137
 
 
138
                    # Chi2
 
139
                    if int(t.qflag[index]) & 128 != 128:
 
140
                        problems['Low Chi2'] = True
 
141
                    else:
 
142
                        problems['Low Chi2'] = False
 
143
                    
 
144
                if int(t.qflag[index]) & 256 != 256:
 
145
                    problems['Edge Sample'] = True
 
146
                else:
 
147
                    problems['Edge Sample'] = False
 
148
                    
 
149
                if  int(t.qflag[index]) & 512 != 512:
 
150
                    problems['Next To Edge Sample'] = True
 
151
                else:
 
152
                    problems['Next To Edge Sample'] = False
 
153
                
 
154
                if ('low' in region.GetHash() and t.calib[index] < 0.14) or\
 
155
                   ('high' in region.GetHash() and t.calib[index] < 10):
 
156
                    problems['No Response'] = True
 
157
                else:
 
158
                    problems['No Response'] = False
 
159
 
 
160
                data['problems'] = problems
 
161
 
 
162
                if abs(t.calib[index] - 1.29) > 0.000001 and abs(t.calib[index] - 81.8) > 0.000001:
 
163
                    newevents.add(Event('CIS', event.runNumber, data, event.time))
 
164
 
 
165
        region.SetEvents(newevents)