def _MakeBackgroundSelections(self): #First bin the data by the Knuth rule or something else self.MakeConstantBins (1.) #self.MakeKnuth() for i in xrange(len(self.bkgIntervals)): diffs = abs(self.bins-self.bkgIntervals[i][0]) self.bkgIntervals[i][0] = self.bins[diffs == min(diffs) ][0] diffs = abs(self.bins-self.bkgIntervals[i][1]) self.bkgIntervals[i][1] = self.bins[diffs == min(diffs) ][0] evts = self.allEvts truthTables = [] for sel in self.bkgIntervals: truthTables.append(logical_and(evts>= sel[0] , evts<= sel[1] )) tt = truthTables[0] if len(truthTables)>1: for y in truthTables[1:]: tt=logical_or(tt,y) filteredEvts = evts[tt] cnts,bins=histtools.histogram(filteredEvts,bins=self.bins) tt=cnts>0 meanT=[] for i in xrange(len(bins)-1): m = mean((bins[i],bins[i+1])) meanT.append(m) meanT = array(meanT) meanT = meanT[tt] cnts = cnts/self.binWidth self.optimalPolGrade = self._fitGlobalAndDetermineOptimumGrade(cnts[tt],meanT) print "Optimal poly grade: %d"% self.optimalPolGrade
def MakeHardnessBlocks(self,binLow,binHi,p0): self.S2N(3.,minNumberOfEvents=25) binWidth = diff(self.bins) #Make two light curves between the energy bins chanLo1,chanLo2 = map(self._selectEnergy,binLow) chanHi1,chanHi2 = map(self._selectEnergy,binHi) tt = logical_and(self.evtExt["PHA"] >= chanLo1, self.evtExt["PHA"]<= chanLo2) curveLo = self.allEvts[tt] ####Bin the counts using the fine bins cnts, _ = histtools.histogram(curveLo,self.bins) cnts = cnts / binWidth ### Get the bkg for this channel selection bkg = [] for j in xrange(len(self.bins)-1): tot = 0 for i in xrange(chanLo1,chanLo2+1): tot+=self.polynomials[i].integral(self.bins[i],self.bins[i+1]) bkg.append(tot) bkg = array(bkg) assert len(cnts)==len(bkg), "Background wrong length" #bkgErr = sqrt(bkg) #cntErr = sqrt(cnts) subCntsLo = cnts-bkg subCntsLoErr = sqrt(cnts+bkg) #######NEED TO ADD ERROR CALCS HERE tt = logical_and(self.evtExt["PHA"] >= chanHi1, self.evtExt["PHA"]<= chanHi2) curveHi = self.allEvts[tt] cnts, _ = histtools.histogram(curveHi,self.bins) cnts = cnts / binWidth ### Get the bkg for this channel selection bkg = [] for j in xrange(len(self.bins)-1): tot = 0 for i in xrange(chanHi1,chanHi2+1): tot+=self.polynomials[i].integral(self.bins[i],self.bins[i+1]) bkg.append(tot) bkg = array(bkg) assert len(cnts)==len(bkg), "Background wrong length" subCntsHi = cnts-bkg subCntsHiErr = sqrt(cnts+bkg) hardRatio = subCntsHi/subCntsLo print len(self.bins) errors = hardRatio**2.*(subCntsLoErr/(subCntsLo**2.) + subCntsHiErr/(subCntsHi**2.)) #bb = BayesianBlocks(binWidth,hardRatio,errors,self.bins[0]) bb = BayesianBlocks(hardRatio,binWidth,self.bins[0]) bins,_ = bb.globalOpt(ncp_prior=p0) lastBin = bins[-1] newBins=array(bins)[::2] newBins =newBins.tolist() newBins.append(lastBin) self.bins = array(newBins) print len(self.bins) self.bType = "hr"
def MakeHardnessBlocks(self, binLow, binHi, p0): self.S2N(3., minNumberOfEvents=25) binWidth = diff(self.bins) #Make two light curves between the energy bins chanLo1, chanLo2 = map(self._selectEnergy, binLow) chanHi1, chanHi2 = map(self._selectEnergy, binHi) tt = logical_and(self.evtExt["PHA"] >= chanLo1, self.evtExt["PHA"] <= chanLo2) curveLo = self.allEvts[tt] ####Bin the counts using the fine bins cnts, _ = histtools.histogram(curveLo, self.bins) cnts = cnts / binWidth ### Get the bkg for this channel selection bkg = [] for j in xrange(len(self.bins) - 1): tot = 0 for i in xrange(chanLo1, chanLo2 + 1): tot += self.polynomials[i].integral(self.bins[i], self.bins[i + 1]) bkg.append(tot) bkg = array(bkg) assert len(cnts) == len(bkg), "Background wrong length" #bkgErr = sqrt(bkg) #cntErr = sqrt(cnts) subCntsLo = cnts - bkg subCntsLoErr = sqrt(cnts + bkg) #######NEED TO ADD ERROR CALCS HERE tt = logical_and(self.evtExt["PHA"] >= chanHi1, self.evtExt["PHA"] <= chanHi2) curveHi = self.allEvts[tt] cnts, _ = histtools.histogram(curveHi, self.bins) cnts = cnts / binWidth ### Get the bkg for this channel selection bkg = [] for j in xrange(len(self.bins) - 1): tot = 0 for i in xrange(chanHi1, chanHi2 + 1): tot += self.polynomials[i].integral(self.bins[i], self.bins[i + 1]) bkg.append(tot) bkg = array(bkg) assert len(cnts) == len(bkg), "Background wrong length" subCntsHi = cnts - bkg subCntsHiErr = sqrt(cnts + bkg) hardRatio = subCntsHi / subCntsLo print len(self.bins) errors = hardRatio**2. * (subCntsLoErr / (subCntsLo**2.) + subCntsHiErr / (subCntsHi**2.)) #bb = BayesianBlocks(binWidth,hardRatio,errors,self.bins[0]) bb = BayesianBlocks(hardRatio, binWidth, self.bins[0]) bins, _ = bb.globalOpt(ncp_prior=p0) lastBin = bins[-1] newBins = array(bins)[::2] newBins = newBins.tolist() newBins.append(lastBin) self.bins = array(newBins) print len(self.bins) self.bType = "hr"