def do2dDiff(self,doEB):
        part = 'eb' if doEB else 'ee'
        customROOTstyle()
        refData = self.parseDic(self._allData["ref"])
        newData = self.parseDic(self._allData["current"])
        histos = {}
        if doEB: histos['eb'] = []
        else: 
            histos['eeplus'] = []
            histos['eeminus'] = []
        for s in range(12): 
            if doEB:
                h = rt.TProfile2D(('%s_diff_2d_sample%d' % (part,s)),"",360,1,360,170,-85,85)
                h.GetXaxis().SetTitle('i#phi')
                h.GetYaxis().SetTitle('i#eta')
                h.SetTitle('sample_{%d}^{new}-sample_{%d}^{ref}' % (s,s))
                h.GetZaxis().SetRangeUser(-0.01,0.01)
                histos['eb'].append(h)
            else: 
                hplus = rt.TProfile2D(('%s_plus_diff_2d_sample%d' % (part,s)),"",100,1,100,100,1,100)
                hplus.GetXaxis().SetTitle('ix')
                hplus.GetYaxis().SetTitle('iy')
                hplus.SetTitle('sample_{%d}^{new}-sample_{%d}^{ref}' % (s,s))
                hplus.GetZaxis().SetRangeUser(-0.01,0.01)
                histos['eeplus'].append(hplus)
                hminus = hplus.Clone('%s_minus_diff_2d_sample%d' % (part,s))
                histos['eeminus'].append(hminus)

        detids = EcalDetId('/afs/cern.ch/work/e/emanuele/public/ecal/pulseshapes_db/detids_ECAL.txt')

        for (partition,detid),samples in refData.iteritems():
            key = (partition,detid)
            (x,y,z) = detids.xyz(detid)

            if key not in newData: continue
            if ((doEB and int(partition)==0) or (not doEB and int(partition)==1)): continue
            if z==-999: continue
            if z==0: htofill = histos['eb']
            elif z==1: htofill = histos['eeplus']
            else: htofill = histos['eeminus']
            (ix,iy) = (y,x) if doEB else (x+1,y+1)
            for s in range(12):
                htofill[s].Fill(ix,iy,float((newData[key])[s])-float((refData[key])[s]))

        xsize = 1200
        ysize = int(xsize*170/360+0.1*xsize) if doEB else int(xsize*0.9)
        canv = rt.TCanvas("c","",xsize,ysize)

        for k,v in histos.iteritems():
            for s in range(12):
                #(histos[k])[s].Draw("colz")
                #canv.SaveAs('%s_diff_2d_sample%d.pdf' % (k,s))
                self.printOnePlot( (histos[k])[s], canv, '%s_diff_2d_sample%d' % (k,s) )
    def do2dDiff(self,doEB):
        part = 'eb' if doEB else 'ee'
        customROOTstyle()
        refData = self.parseDic(self._allData["ref"])
        newData = self.parseDic(self._allData["current"])
        histos = {}
        if doEB: histos['eb'] = []
        else: 
            histos['eeplus'] = []
            histos['eeminus'] = []
        for s in range(12): 
            if doEB: 
                h = rt.TProfile2D(('%s_diff_2d_sample%d' % (part,s)),"",360,1,360,170,-85,85)
                h.GetXaxis().SetTitle('i#phi')
                h.GetYaxis().SetTitle('i#eta')
                h.SetTitle('sample_{%d}^{new}-sample_{%d}^{ref}' % (s,s))
                h.GetZaxis().SetRangeUser(-0.03,0.03)
                histos['eb'].append(h)
            else: 
                hplus = rt.TProfile2D(('%s_plus_diff_2d_sample%d' % (part,s)),"",100,1,100,100,1,100)
                hplus.GetXaxis().SetTitle('ix')
                hplus.GetYaxis().SetTitle('iy')
                hplus.SetTitle('sample_{%d}^{new}-sample_{%d}^{ref}' % (s,s))
                hplus.GetZaxis().SetRangeUser(-0.03,0.03)
                histos['eeplus'].append(hplus)
                hminus = hplus.Clone('%s_minus_diff_2d_sample%d' % (part,s))
                histos['eeminus'].append(hminus)

        detids = EcalDetId('/afs/cern.ch/work/e/emanuele/public/ecal/pulseshapes_db/detids_ECAL.txt')

        for (partition,detid),samples in refData.iteritems():
            key = (partition,detid)
            (x,y,z) = detids.xyz(detid)

            if key not in newData: continue
            if ((doEB and int(partition)==0) or (not doEB and int(partition)==1)): continue
            if z==-999: continue
            if z==0: htofill = histos['eb']
            elif z==1: htofill = histos['eeplus']
            else: htofill = histos['eeminus']
            (ix,iy) = (y,x) if doEB else (x,y)
            for s in range(12):
                htofill[s].Fill(ix,iy,float((newData[key])[s])-float((refData[key])[s]))

        xsize = 1200
        ysize = int(xsize*170/360+0.1*xsize) if doEB else int(xsize*0.9)
        canv = rt.TCanvas("c","",xsize,ysize)
        for k,v in histos.iteritems():
            for s in range(12):
                #(histos[k])[s].Draw("colz")
                #canv.SaveAs('%s_diff_2d_sample%d.pdf' % (k,s))
                self.printOnePlot( (histos[k])[s], canv, '%s_diff_2d_sample%d' % (k,s) )
    tv = TagValidation([txt], options)
    data = tv.parseDic(tv._allData["current"])

    ebdata = {(partition, detid): samples
              for (partition, detid), samples in data.iteritems()
              if partition == '1'}
    eedata = {(partition, detid): samples
              for (partition, detid), samples in data.iteritems()
              if partition == '0'}

    print "Original has {neb} crystals in EB and {nee} in EE".format(
        neb=len(ebdata), nee=len(eedata))

    # some geometry
    detids = EcalDetId(
        '/afs/cern.ch/work/e/emanuele/public/ecal/pulseshapes_db/detids_ECAL.txt'
    )
    ringmap = etaRingMapping()

    averages = {}
    filledCry = {}
    irCry = {}

    print "Starting the first loop..."
    for (partition, detid), samples in eedata.iteritems():
        (x, y, z) = detids.xyz(detid)
        x0 = x - 50
        y0 = y - 50
        ir = int(math.sqrt(x0 * x0 + y0 * y0))
        iring = ringmap.getRing(False, x, y, z)
        if iring not in averages:
    def do2dTime(self,currentTimeIC,doEB,newTimeIC=''):
        part = 'EB' if doEB else 'EE'
        customROOTstyle()
        refData = self.parseDic(self._allData["ref"])
        newData = self.parseDic(self._allData["current"])
        of = rt.TFile.Open('%s_timeVals.root' % part,'recreate')
        histos = []
        histosDiff = []
        if doEB: 
            h = rt.TProfile2D(('%s_time' % part),"",360,1,360,170,-85,85)
            h.GetXaxis().SetTitle('i#phi')
            h.GetYaxis().SetTitle('i#eta')
            h.SetTitle('Time (ns)')
            h.GetZaxis().SetRangeUser(-1,1)
            histos.append(h)
            histosDiff.append(h.Clone(('%s_time_diff' % part)))
        else: 
            hplus = rt.TProfile2D(('%splus_time' % part),"",100,1,100,100,1,100)
            hplus.GetXaxis().SetTitle('ix')
            hplus.GetYaxis().SetTitle('iy')
            hplus.SetTitle('Time (ns)')
            hplus.GetZaxis().SetRangeUser(-1,1)
            histos.append(hplus)
            histosDiff.append(hplus.Clone(('%splus_time_diff' % part)))
            hminus = hplus.Clone('%sminus_time' % part)
            histos.append(hminus)
            histosDiff.append(hminus.Clone(('%sminus_time_diff' % part)))

        detids = EcalDetId('/afs/cern.ch/work/e/emanuele/public/ecal/pulseshapes_db/detids_ECAL.txt')

        # load the current time ICs (dump of the tag)
        self.timeICs = self.loadTimeICs(currentTimeIC)
        if len(newTimeIC)>0: newTimeICs = self.loadTimeICs(newTimeIC)

        histo = rt.TH1F("histo","",15,0,15)
        fitter = AlphaBetaFitter( rt.TF1("alphabeta",alphabeta,0,10,5), doEB)
        cryfit = 0
        for (partition,detid),samples in newData.iteritems():
            key = (partition,detid)
            (x,y,z) = detids.xyz(detid)

            if detid not in self.timeICs: continue
            if ((doEB and int(partition)==0) or (not doEB and int(partition)==1)): continue
            if z==0 or z==1: 
                htofill = histos[0]
                hdifftofill = histosDiff[0]
            else: 
                htofill = histos[1]
                hdifftofill = histosDiff[1]
            (ix,iy) = (y,x) if doEB else (x,y)

            # fill the template and fit it
            for s in range(3):  histo.SetBinContent(s+1,0)
            for s in range(12): histo.SetBinContent(s+4,float((newData[key])[s]))
            # results = fitter.fit(histo,doEB,('pulse_%d_%d_%d.png' % (x,y,z)))
            results = fitter.fit(histo,doEB)
            currentCorr = float(self.timeICs[detid]) + self.timeOffsets[part]
            time = 25.*((results['pars'])[2]-5.5)
            correctedTime = time + currentCorr
            # print "detid = ",key," has time from fit [IC] = ",time, " [",currentCorr,"]"
            htofill.Fill(ix,iy,correctedTime)

            if len(newTimeIC)>0: 
                if detid not in newTimeICs: continue
                newCorr = float(newTimeICs[detid])
                # print '    correctedTime = ',correctedTime,"   newCorr = ",newCorr,"  diff = ", correctedTime - newCorr
                hdifftofill.Fill(ix,iy,correctedTime - newCorr)

            if cryfit % 1000 == 0: print 'fitted ',cryfit,' templates'
            cryfit += 1

        xsize = 1200
        ysize = int(xsize*170/360+0.1*xsize) if doEB else int(xsize*0.9)
        of.cd()
        canv = rt.TCanvas("c","",xsize,ysize)
        for h in histos:
            #h.Draw("colz")
            #canv.SaveAs(h.GetName()+'.pdf')
            self.printOnePlot(h,canv,h.GetName())
            h.Write()
        for h in histosDiff:
            h.Write()
        of.Close()
def asymmetryByRing(tv, data, doPlot=True):

    customROOTstyle()
    rt.gStyle.SetOptStat(0)

    histos = {}
    histos['eeplus'] = []
    histos['eeminus'] = []
    histos['eediff'] = []
    for s in range(12):
        hp = rt.TProfile(('endcap_plus_ring_sample%d' % s), "", 39, 0, 39)
        hp.GetXaxis().SetTitle('#eta ring')
        hp.SetTitle('average sample %d' % s)
        histos['eeplus'].append(hp)
        hm = hp.Clone('endcap_minus_ring_sample%d' % s)
        histos['eeminus'].append(hm)
        hd = rt.TH1D(('endcap_diff_ring_sample%d' % s), "", 39, 0, 39)
        hd.GetXaxis().SetTitle('#eta ring')
        hd.SetTitle('sample %d (EE^{+}-EE^{-})/(EE^{+}+EE^{-})' % s)
        hd.GetYaxis().SetLimits(-0.01, 0.01)
        histos['eediff'].append(hd)

    print "Loading detIds mapping..."
    detids = EcalDetId(
        '/afs/cern.ch/work/e/emanuele/public/ecal/pulseshapes_db/detids_ECAL.txt'
    )
    print "Loading EE rings..."
    ringmap = etaRingMapping()

    for (partition, detid), samples in data.iteritems():
        key = (partition, detid)
        (x, y, z) = detids.xyz(detid)

        if int(partition) == 1: continue  # skip barrel

        htofill = histos['eeplus'] if z > 0 else histos['eeminus']
        ring = ringmap.getRing(False, x, y, z)
        for s in range(12):
            htofill[s].Fill(ring, float(data[key][s]))

    xsize = 1200
    ysize = int(xsize * 0.9)
    canv = rt.TCanvas("c", "", xsize, ysize)

    for s in range(12):
        for b in range(histos['eeplus'][s].GetNbinsX() + 1):
            reldiff = (histos['eeplus'][s].GetBinContent(b) -
                       histos['eeminus'][s].GetBinContent(b)) / (
                           histos['eeplus'][s].GetBinContent(b) +
                           histos['eeminus'][s].GetBinContent(b))
            relerr = hypot(
                histos['eeplus'][s].GetBinError(b) /
                histos['eeplus'][s].GetBinContent(b),
                histos['eeplus'][s].GetBinError(b) /
                histos['eeplus'][s].GetBinContent(b))
            histos['eediff'][s].SetBinContent(b, reldiff)
            histos['eediff'][s].SetBinError(b, relerr)
        if doPlot:
            tv.printOnePlot((histos['eediff'])[s], canv,
                            'endcap_diff_ring_sample%d' % s)
            tv.printOnePlot((histos['eeplus'])[s], canv,
                            'endcap_plus_ring_sample%d' % s)
            tv.printOnePlot((histos['eeminus'])[s], canv,
                            'endcap_minus_ring_sample%d' % s)

    return histos
    def do2dShapeDiff(self,doEB,absoluteShape=False):
        part = 'EB' if doEB else 'EE'
        customROOTstyle()
        refData = self.parseDic(self._allData["ref"])
        newData = self.parseDic(self._allData["current"])
        of = rt.TFile.Open('%s_timeVals.root' % part,'recreate')
        histos = {}
        for ip,par in enumerate(['alpha','beta','T0']):
            (zmin,zmax) = (-0.03,0.03) if par!="T0" else (-1,1) # ns
            if par=="T0" and absoluteShape: (zmin,zmax) = (-5,5) # ns
            if doEB:
                h = rt.TProfile2D(('%s_%s' % (part,par)),"",360,1,360,170,-85,85)
                h.GetXaxis().SetTitle('i#phi')
                h.GetYaxis().SetTitle('i#eta')
                h.SetTitle('Time (ns)' if par=="T0" else '#Delta #{par}/#{par}'.format(par=par))
                h.GetZaxis().SetRangeUser(zmin,zmax)
                histos[par] = [h]
            else: 
                hz = []
                hplus = rt.TProfile2D(('%splus_%s' % (part,par)),"",100,1,100,100,1,100)
                hplus.GetXaxis().SetTitle('ix')
                hplus.GetYaxis().SetTitle('iy')
                hplus.SetTitle('Time (ns)' if par=="T0" else '#Delta #{par}/#{par}'.format(par=par))
                hplus.GetZaxis().SetRangeUser(zmin,zmax)
                hz.append(hplus)
                hminus = hplus.Clone('%sminus_%s' % (part,par))
                hz.append(hminus)
                histos[par] = hz

        detids = EcalDetId('/afs/cern.ch/work/e/emanuele/public/ecal/pulseshapes_db/detids_ECAL.txt')

        cryfit = 0
        for (partition,detid),samples in newData.iteritems():
            key = (partition,detid)
            (x,y,z) = detids.xyz(detid)

            if not absoluteShape and key not in refData: continue
            if ((doEB and int(partition)==0) or (not doEB and int(partition)==1)): continue
            if z==-999: continue

            (ix,iy) = (y,x) if doEB else (x+1,y+1)

            (val,err) = self.timeFit(newData[key],self.abfitter,doEB)
            (valRef,errRef) = self.timeFit(refData[key],self.abfitter,doEB) if not absoluteShape else ({},{})

            for ip,par in enumerate(['alpha','beta','T0']):
                if z==0 or z==1: 
                    htofill = (histos[par])[0]
                else: 
                    htofill = (histos[par])[1]
                value = val[par]-valRef[par] if not absoluteShape else val[par]
                if par!="T0": value = value/valRef[par] if not absoluteShape else value
                #print "par = ",par," val1,2 = ",val[par]," ",valRef[par]," norm value = ",value
                #print "err1,2 = ",err[par]," ",errRef[par]
                htofill.Fill(ix,iy,value)

            if cryfit % 1000 == 0: print 'fitted ',cryfit,' templates'
            cryfit += 1

        xsize = 1200
        ysize = int(xsize*170/360+0.1*xsize) if doEB else int(xsize*0.9)
        of.cd()
        canv = rt.TCanvas("c","",xsize,ysize)
        for k,hvect in histos.iteritems():
            for h in hvect:
                h.Write()
                self.printOnePlot(h,canv,'{name}{absolute}'.format(name=h.GetName(),absolute='_absolute' if absoluteShape else ''),"",True)
        of.Close()
    def do2dTime(self,currentTimeIC,doEB,newTimeIC=''):
        part = 'EB' if doEB else 'EE'
        customROOTstyle()
        refData = self.parseDic(self._allData["ref"])
        newData = self.parseDic(self._allData["current"])
        of = rt.TFile.Open('%s_timeVals.root' % part,'recreate')
        histos = []
        histosDiff = []
        if doEB: 
            h = rt.TProfile2D(('%s_time' % part),"",360,1,360,170,-85,85)
            h.GetXaxis().SetTitle('i#phi')
            h.GetYaxis().SetTitle('i#eta')
            h.SetTitle('Time (ns)')
            h.GetZaxis().SetRangeUser(-1,1)
            histos.append(h)
            histosDiff.append(h.Clone(('%s_time_diff' % part)))
        else: 
            hplus = rt.TProfile2D(('%splus_time' % part),"",100,1,100,100,1,100)
            hplus.GetXaxis().SetTitle('ix')
            hplus.GetYaxis().SetTitle('iy')
            hplus.SetTitle('Time (ns)')
            hplus.GetZaxis().SetRangeUser(-1,1)
            histos.append(hplus)
            histosDiff.append(hplus.Clone(('%splus_time_diff' % part)))
            hminus = hplus.Clone('%sminus_time' % part)
            histos.append(hminus)
            histosDiff.append(hminus.Clone(('%sminus_time_diff' % part)))

        detids = EcalDetId('/afs/cern.ch/work/e/emanuele/public/ecal/pulseshapes_db/detids_ECAL.txt')

        # load the current time ICs (dump of the tag)
        self.timeICs = self.loadTimeICs(currentTimeIC)
        if len(newTimeIC)>0: newTimeICs = self.loadTimeICs(newTimeIC)

        histo = rt.TH1F("histo","",15,0,15)
        cryfit = 0
        for (partition,detid),samples in newData.iteritems():
            key = (partition,detid)
            (x,y,z) = detids.xyz(detid)

            if detid not in self.timeICs: continue
            if ((doEB and int(partition)==0) or (not doEB and int(partition)==1)): continue
            if z==0 or z==1: 
                htofill = histos[0]
                hdifftofill = histosDiff[0]
            else: 
                htofill = histos[1]
                hdifftofill = histosDiff[1]
            (ix,iy) = (y,x) if doEB else (x,y)

            # fill the template and fit it
            for s in range(3):  histo.SetBinContent(s+1,0)
            for s in range(12): histo.SetBinContent(s+4,float((newData[key])[s]))
            self.abfitter.fit(histo,doEB)
            results = {'pars': abfitter.getPars(), 'errs': abfitter.getErrs()}
            currentCorr = float(self.timeICs[detid]) + self.timeOffsets[part]
            time = 25.*((results['pars'])[2]-5.5)
            correctedTime = time + currentCorr
            # print "detid = ",key," has time from fit [IC] = ",time, " [",currentCorr,"]"
            htofill.Fill(ix,iy,correctedTime)

            if len(newTimeIC)>0: 
                if detid not in newTimeICs: continue
                newCorr = float(newTimeICs[detid])
                # print '    correctedTime = ',correctedTime,"   newCorr = ",newCorr,"  diff = ", correctedTime - newCorr
                hdifftofill.Fill(ix,iy,correctedTime - newCorr)

            if cryfit % 1000 == 0: print 'fitted ',cryfit,' templates'
            cryfit += 1

        xsize = 1200
        ysize = int(xsize*170/360+0.1*xsize) if doEB else int(xsize*0.9)
        of.cd()
        canv = rt.TCanvas("c","",xsize,ysize)
        for h in histos:
            #h.Draw("colz")
            #canv.SaveAs(h.GetName()+'.pdf')
            self.printOnePlot(h,canv,h.GetName())
            h.Write()
        for h in histosDiff:
            h.Write()
        of.Close()