~vpec/maus/tof_calib_read

« back to all changes in this revision

Viewing changes to workers/CIS/.svn/text-base/GetSamples.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 array import array
 
9
from src.MakeCanvas import *
 
10
 
 
11
class GetSamples(ReadGenericCalibration):
 
12
    "Get the samples for a scan and the distribution of fit energy for fixed-charges"
 
13
 
 
14
    def __init__(self, processingDir='rfio:/castor/cern.ch/grid/atlas/atlasgroupdisk/det-tile/2009/',\
 
15
                 getSamples=True, getInjDist=True, all=False):
 
16
        self.processingDir = processingDir
 
17
        self.getSamples = getSamples
 
18
        self.getInjDist = getInjDist
 
19
        self.all=all
 
20
        self.ftDict = {} # Used for linear constant.  Each element is a [TTree, TFile]
 
21
        self.badRuns = set()
 
22
        self.dir = getPlotDirectory() + '/cis/samples'
 
23
        createDir(self.dir) 
 
24
        self.c1 = MakeCanvas()
 
25
        self.c1.SetHighLightColor(2)
 
26
        self.h_index = 0
 
27
 
 
28
    def ProcessStart(self):
 
29
        pass
 
30
 
 
31
    def ProcessStop(self):
 
32
        pass
 
33
    
 
34
    def ProcessRegion(self, region):
 
35
        # See if it's a gain
 
36
        if 'gain' not in region.GetHash():
 
37
            return
 
38
 
 
39
        dirstr = '%s%02d' % (region.GetHash().split('_')[1], int(region.GetHash().split('_')[2][1:]))
 
40
 
 
41
        # Prepare events
 
42
        foundEventToProcess = False
 
43
        for event in region.GetEvents():
 
44
            if event.runType == 'CIS':
 
45
                if not event.data.has_key('moreInfo') and not self.all:
 
46
                    continue
 
47
                
 
48
                if not self.all and not event.data['moreInfo']:
 
49
                    continue
 
50
                
 
51
                foundEventToProcess = True
 
52
            
 
53
                if event.runNumber and\
 
54
                       not self.ftDict.has_key(event.runNumber):
 
55
                    #print '%s%s/tiletb_%s_CIS.%s.0.aan.root' % (self.processingDir, dirstr, event.runNumber, dirstr)
 
56
                    f, t = self.getFileTree('%s/tiletb_%s_CIS.%s.0.aan.root' % (dirstr, event.runNumber, dirstr), 'h1000')
 
57
 
 
58
                    if [f, t] == [None, None]:
 
59
                        if event.runNumber not in self.badRuns:
 
60
                            print "Error: GetSamples couldn't load run", event.runNumber, ' for %s...' % dirstr
 
61
                            self.badRuns.add(event.runNumber)
 
62
                        continue
 
63
 
 
64
                    self.ftDict[event.runNumber] = [f, t]
 
65
 
 
66
        
 
67
        if not foundEventToProcess:
 
68
            return
 
69
 
 
70
        map = {'LBA': 'A', 'LBC': 'C', 'EBA': 'D', 'EBC': 'E'}
 
71
        if 'lowgain' in region.GetHash():
 
72
            gain = 'lo'
 
73
        else:
 
74
            gain = 'hi'
 
75
        module = int(region.GetHash().split('_')[2][1:])
 
76
        chan = int(region.GetHash(1).split('_')[3][1:]) - 1
 
77
        partition = region.GetHash().split('_')[1]
 
78
 
 
79
        newevents = set()
 
80
        for event in region.GetEvents():
 
81
            hists = [[[] for i in range(16)] for j in range(15)]
 
82
 
 
83
            maxdac = 0
 
84
            maxphase = 0
 
85
            
 
86
            if event.runType == 'CIS':
 
87
                if not self.all and \
 
88
                       (event.data.has_key('moreInfo') and not event.data['moreInfo']):
 
89
                    continue
 
90
                # Load up the tree, file and scans for this run
 
91
                if not self.ftDict.has_key(event.runNumber):
 
92
                    continue 
 
93
                [f, t] = self.ftDict[event.runNumber]
 
94
 
 
95
                injections = {}
 
96
                
 
97
                for i in range(t.GetEntries()):
 
98
                    if t.GetEntry(i) <= 0:
 
99
                        print 'huh?'
 
100
                        return
 
101
 
 
102
                    if t.m_cispar[7] != 100:
 
103
                        continue
 
104
 
 
105
                    phase = t.m_cispar[5]
 
106
                    dac = t.m_cispar[6]
 
107
 
 
108
                    charge = 2.0*4.096*100.0/1023.0 * dac # From TileCisDefaultCalibTool.cxx
 
109
 
 
110
                    if gain == 'lo' and not 300 < charge < 700:
 
111
                        continue
 
112
                    elif gain == 'hi' and not 3 < charge < 10:
 
113
                        continue
 
114
                    
 
115
                    if not hasattr(t, 'efit%s%02d%s' % (map[partition], module, gain)) or\
 
116
                       not hasattr(t, 'tfit%s%02d%s' % (map[partition], module, gain)) or\
 
117
                       not hasattr(t, 'pedfit%s%02d%s' % (map[partition], module, gain)) or\
 
118
                       not hasattr(t, 'sample%s%02d%s' % (map[partition], module, gain)):
 
119
                        return
 
120
                    
 
121
                    e_of_fit = getattr(t, 'efit%s%02d%s' % (map[partition], module, gain))
 
122
                    t_of_fit = getattr(t, 'tfit%s%02d%s' % (map[partition], module, gain))
 
123
                    p_of_fit = getattr(t, 'pedfit%s%02d%s' % (map[partition], module, gain))
 
124
                    samples = getattr(t, 'sample%s%02d%s' % (map[partition], module, gain))
 
125
 
 
126
                    if gain == 'lo':
 
127
                        ndac = (dac-384)/32
 
128
                    else:
 
129
                        ndac = (dac-4)
 
130
 
 
131
                    nphase = phase/16
 
132
 
 
133
                    if ndac > maxdac:
 
134
                        maxdac = ndac
 
135
 
 
136
                    if nphase > maxphase:
 
137
                        maxphase = nphase
 
138
 
 
139
                    h = ROOT.TH1F('h_%f_%f_%f_%d' % (charge, phase, e_of_fit[chan], self.h_index), '', 7, 0, 7)
 
140
                    self.h_index += 1
 
141
                    
 
142
                    #                    print dac, phase, 
 
143
                    for j in range(7):
 
144
                        h.Fill(j, samples[chan*9 + j])
 
145
 
 
146
                    #                   h.Draw()
 
147
 
 
148
 
 
149
                    # this is a mess.
 
150
                    self.c1.cd()
 
151
                    h.GetXaxis().SetTitle("Sample Number")
 
152
                    h.GetYaxis().SetTitle("ADC counts")
 
153
                    h.GetXaxis().SetTitleOffset(0.93)
 
154
                    h.GetXaxis().SetTitleSize(0.05)
 
155
                    h.GetYaxis().SetTitleSize(0.05)
 
156
                    h.GetYaxis().SetTitleOffset(0.99)
 
157
                    h.SetMinimum(0)
 
158
                    h.SetMaximum(1023)
 
159
                    #h.SetFillColor(1)
 
160
                    #h.SetFillStyle(3002)
 
161
                    #h.SetLineWidth(6)
 
162
                    h.Draw()
 
163
 
 
164
                    tl1 = ROOT.TLatex(0.10, 0.92, "Fit Energy: %f" % e_of_fit[chan])
 
165
                    tl2 = ROOT.TLatex(0.53, 0.92, "Fit Time: %f" % t_of_fit[chan])
 
166
                    tl3 = ROOT.TLatex(0.40, 0.84, "Fit Pedestal: %f" % p_of_fit[chan])
 
167
 
 
168
                    for tl in [tl1, tl2, tl3]:
 
169
                        tl.SetNDC()
 
170
                        tl.SetTextSize(0.06)
 
171
                        tl.Draw()
 
172
 
 
173
                    #self.c1.Print("%s/sample_%s_%f_%f_%f.ps" % (self.dir, region.GetHash(), charge, phase, e_of_fit[chan]))
 
174
                    #self.c1.Print("%s/sample_%s_%f_%f_%f.root" % (self.dir, region.GetHash(), charge, phase, e_of_fit[chan]))
 
175
                        #print samples[chan*9 + j],
 
176
                        #                    print
 
177
 
 
178
                    #print nphase, ndac, gain, dac
 
179
                    #                    print hists[nphase]
 
180
                    #                    print hists[nphase][ndac]
 
181
                    hists[nphase][ndac].append(h)
 
182
 
 
183
                    if not injections.has_key(charge):
 
184
                        injections[charge] = []
 
185
                    injections[charge].append(e_of_fit[chan])
 
186
 
 
187
                event.data['samples'] = hists
 
188
 
 
189
            inj_hists = {}
 
190
            for z, w in  injections.iteritems(): # z charge, w arrany
 
191
                dx = max([(max(w) - min(w))*0.1, 2.5])
 
192
                
 
193
                h = ROOT.TH1F('%s_%f_%d' % (region.GetHash(), z, event.runNumber), '', 10, min(w)-dx, max(w)+dx)
 
194
                for n in w:
 
195
                    h.Fill(n)
 
196
                    
 
197
                inj_hists[z] = h
 
198
 
 
199
            event.data['injections'] = inj_hists
 
200
            event.data['maxphase'] = maxphase
 
201
            event.data['maxdac'] = maxdac
 
202
            newevents.add(event)
 
203
                
 
204
 
 
205
        region.SetEvents(newevents)
 
206
 
 
207
 
 
208
#        arrow1 = ROOT.TArrow(0.03,0.3,0.03,0.60,0.02,"|>")
 
209
#        arrow1.SetAngle(90)
 
210
#        arrow2 = ROOT.TArrow(0.4,0.03,0.55,0.03,0.02,"|>")
 
211
 
 
212
#        for arrow in [arrow1, arrow2]:
 
213
#            arrow.SetLineWidth(1)
 
214
#            arrow.Draw()
 
215
         
 
216