def draw_box(self): z1 = self.center_z - self.length / 2 z2 = z1 + self.length x1 = self.center_x - self.rad2 x2 = x1 + 2 * self.rad2 #to cm x1 *= 100 x2 *= 100 #representation as a box self.box = TFrame(z1, x1, z2, x2) self.box.SetBorderMode(0) self.box.SetFillColor(rt.kGray + 1) col = rt.kRed if self.is_electron == True: col = rt.kBlue self.box.SetLineColor(col) self.box.SetLineWidth(2) #self.box.Draw("same") #label lx = x2 + 2 align = 11 if lx < 0 and lx > -62: lx = x1 - 5 # negative lx align = 31 if self.center_z < 0 and self.center_x < 0.1: lx = x1 - 2 # negative z and x align = 31 if lx > 0 and lx < 22: lx = 22 # small positive lx self.label = TText(z2, lx, self.name) self.label.SetTextSize(0.03) self.label.SetTextAngle(90) self.label.SetTextAlign(align)
def AutoFixYaxis(can): # # Makes space for text as well! # from ROOT import TFrame import math maxy_frac = 1 # # Now we make space for any text we drew on the canvas, and # also the Legend # plots_exist = False tframe_height = 1 - can.GetTopMargin() - can.GetBottomMargin() for i in can.GetListOfPrimitives(): if type(i) == type(TFrame()): continue if hasattr(i, 'GetY1NDC'): maxy_frac = min(maxy_frac, i.GetY1NDC()) plots_exist = True if hasattr(i, 'GetY'): maxy_frac = min(maxy_frac, i.GetY()) plots_exist = True if not plots_exist: print 'Your plot %s has nothing in it. Doing nothing.' % ( can.GetName()) return (miny, maxy) = GetYaxisRanges(can, check_all=True) # print 'AutoFixAxes0',miny,maxy if miny == 0 and maxy == 0: return miny = (0.95 * miny) * (miny > 0) + (1.05 * miny) * (miny < 0) maxy_frac = maxy_frac - can.GetBottomMargin() if can.GetLogy(): # special treatment for log plots miny = 0.85 * MinimumForLog(can) # some orders of magnitude *above* miny, making room for text orderofmagnitude_span = math.log(maxy / miny) / math.log(10) orderofmagnitude_span = 1.1 * orderofmagnitude_span * tframe_height / maxy_frac maxy = miny * math.pow(10, orderofmagnitude_span) else: # scale to make space for text maxy_frac = maxy_frac - .02 maxy = tframe_height * (maxy - miny) / float(maxy_frac) + miny # round y axis to nice round numbers (miny, maxy) = NearestNiceNumber(miny, maxy) # print 'AutoFixAxes',miny,maxy # if symmetrize : # (miny,maxy) = -max(math.fabs(miny),math.fabs(maxy)),max(math.fabs(miny),math.fabs(maxy)) SetYaxisRanges(can, miny, maxy) return
def AutoFixAxes(can, ignoreErrors=False, both=False, ymaxFrac=1.0): from ROOT import TFrame import math if can.GetPrimitive('pad_top'): AutoFixAxes(can.GetPrimitive('pad_top'), ignoreErrors) if both and can.GetPrimitive('pad_bot'): AutoFixAxes(can.GetPrimitive('pad_bot'), ignoreErrors) return FixXaxisRanges(can) maxy_frac = 1 tframe_height = 1 - can.GetTopMargin() - can.GetBottomMargin() for i in can.GetListOfPrimitives(): if type(i) == type(TFrame()): continue if hasattr(i, 'GetY1NDC'): maxy_frac = min(maxy_frac, i.GetY1NDC()) if hasattr(i, 'GetY'): maxy_frac = min(maxy_frac, i.GetY()) (miny, maxy) = GetYaxisRanges(can, check_all=True, ignorezeros=True, ignoreErrors=ignoreErrors) if miny == 0 and maxy == 0: return miny = (0.95 * miny) * (miny > 0) + (1.05 * miny) * (miny < 0) maxy_frac = maxy_frac - can.GetBottomMargin() if can.GetLogy(): # special treatment for log plots miny = 0.85 * MinimumForLog(can) # some orders of magnitude *above* miny, making room for text orderofmagnitude_span = math.log(maxy / miny) / math.log(10) orderofmagnitude_span = 1.1 * orderofmagnitude_span * tframe_height / maxy_frac maxy = miny * math.pow(10, orderofmagnitude_span) else: # scale to make space for text maxy_frac = maxy_frac - .02 maxy = tframe_height * (maxy - miny) / float(maxy_frac) + miny # round y axis to nice round numbers (miny, maxy) = NearestNiceNumber(miny, maxy) SetYaxisRanges(can, miny, maxy * ymaxFrac) return
def invert_col(self, pad): bgcol = rt.kBlack fgcol = rt.kOrange - 3 pad.SetFillColor(bgcol) pad.SetFrameLineColor(fgcol) next = TIter(pad.GetListOfPrimitives()) obj = next() while obj != None: #TText if obj.InheritsFrom(TText.Class()) == True: if obj.GetTextColor() == rt.kBlack: obj.SetTextColor(fgcol) #H1 if obj.InheritsFrom(TH1.Class()) == True: if obj.GetLineColor() == rt.kBlack: obj.SetLineColor(fgcol) obj.SetFillColor(bgcol) if obj.GetMarkerColor() == rt.kBlack: obj.SetMarkerColor(fgcol) obj.SetAxisColor(fgcol, "X") obj.SetAxisColor(fgcol, "Y") obj.SetLabelColor(fgcol, "X") obj.SetLabelColor(fgcol, "Y") obj.GetXaxis().SetTitleColor(fgcol) obj.GetYaxis().SetTitleColor(fgcol) #TFrame if obj.InheritsFrom(TFrame.Class()) == True: if obj.GetLineColor() == rt.kBlack: obj.SetLineColor(fgcol) obj.SetFillColor(bgcol) #print obj.GetName(), obj.ClassName() #move to the next item obj = next()
def invert_col(pad, fgcol=rt.kOrange - 3, bgcol=rt.kBlack): #set foreground and background color #fgcol = rt.kAzure #fgcol = rt.kGreen #fgcol = rt.kOrange-3 pad.SetFillColor(bgcol) pad.SetFrameLineColor(fgcol) next = TIter(pad.GetListOfPrimitives()) obj = next() while obj != None: #H1 if obj.InheritsFrom(TH1.Class()) == True: if obj.GetLineColor() == rt.kBlack: obj.SetLineColor(fgcol) obj.SetFillColor(bgcol) if obj.GetMarkerColor() == rt.kBlack: obj.SetMarkerColor(fgcol) obj.SetAxisColor(fgcol, "X") obj.SetAxisColor(fgcol, "Y") obj.SetLabelColor(fgcol, "X") obj.SetLabelColor(fgcol, "Y") obj.GetXaxis().SetTitleColor(fgcol) obj.GetYaxis().SetTitleColor(fgcol) #Legend if obj.InheritsFrom(TLegend.Class()) == True: obj.SetTextColor(fgcol) #obj.SetFillStyle(1000) #obj.SetFillColor(fgcol) #obj.SetTextColor(bgcol) #ln = TIter(obj.GetListOfPrimitives()) #lo = ln.Next() #while lo != None: #if lo.GetObject() == None: #lo = ln.Next() #continue #if lo.GetObject().InheritsFrom(TH1.Class()) == True: #hx = lo.GetObject() #hx.SetFillColor(bgcol) #if hx.GetMarkerColor() == rt.kBlack: #hx.SetMarkerColor(fgcol) #hx.SetLineColor(fgcol) #pass #lo = ln.Next() #RooHist if obj.InheritsFrom(RooHist.Class()) == True: if obj.GetMarkerColor() == rt.kBlack: obj.SetLineColor(fgcol) obj.SetMarkerColor(fgcol) #H2 if obj.InheritsFrom(TH2.Class()) == True: obj.SetAxisColor(fgcol, "Z") obj.SetLabelColor(fgcol, "Z") obj.GetZaxis().SetTitleColor(fgcol) #obj.SetLineColor(fgcol) #obj.SetMarkerColor(fgcol) #TLatex if obj.InheritsFrom(TLatex.Class()) == True: if obj.GetTextColor() == rt.kBlack: obj.SetTextColor(fgcol) #F2 if obj.InheritsFrom(TF2.Class()) == True: axes = [obj.GetXaxis(), obj.GetYaxis(), obj.GetZaxis()] for i in range(len(axes)): axes[i].SetAxisColor(fgcol) axes[i].SetLabelColor(fgcol) axes[i].SetTitleColor(fgcol) #F1 if obj.InheritsFrom(TF1.Class()) == True: axes = [obj.GetXaxis(), obj.GetYaxis()] for i in range(len(axes)): axes[i].SetAxisColor(fgcol) axes[i].SetLabelColor(fgcol) axes[i].SetTitleColor(fgcol) #TGraph if obj.InheritsFrom(TGraph.Class()) == True: obj.SetFillColor(bgcol) ax = obj.GetXaxis() ay = obj.GetYaxis() ax.SetAxisColor(fgcol) ay.SetAxisColor(fgcol) ax.SetLabelColor(fgcol) ay.SetLabelColor(fgcol) ax.SetTitleColor(fgcol) ay.SetTitleColor(fgcol) if obj.GetLineColor() == rt.kBlack: obj.SetLineColor(fgcol) obj.SetMarkerColor(fgcol) #TGaxis if obj.InheritsFrom(TGaxis.Class()) == True: obj.SetLineColor(fgcol) obj.SetLabelColor(fgcol) obj.SetTitleColor(fgcol) #TFrame if obj.InheritsFrom(TFrame.Class()) == True: if obj.GetLineColor() == rt.kBlack: obj.SetLineColor(fgcol) obj.SetFillColor(bgcol) #move to next item obj = next()
class vmagnet(object): #_____________________________________________________________________________ def __init__(self): #set magnet from configuration dictionary self.name = "" self.center_z = 0. self.center_x = 0. self.length = 0. self.rad1 = 0. self.rad2 = 0. self.field = 0. #electron or hadron self.is_electron = True #initial angle self.theta_0 = 0. #parameters from MAD-X survey self.S = 0. self.L = 0. self.X = 0. self.Z = 0. self.THETA = 0. self.has_survey = False #drawing configuration self.fill_style = 1000 self.label_down = False self.label = "" self.no_label = False self.line_col = rt.kBlue self.fill_col = rt.kGreen - 2 #_____________________________________________________________________________ def read_survey(self, lin): #values from MAD-X survey self.S = float(lin["S"]) self.L = float(lin["L"]) self.X = float(lin["X"]) self.Z = float(lin["Z"]) self.THETA = float(lin["THETA"]) + self.theta_0 self.has_survey = True #_____________________________________________________________________________ def rotate_translateX(self, theta, xt): #combined rotation and translation self.rotate(theta) self.translateX(xt) #_____________________________________________________________________________ def translateX(self, xt): #translate the magnet along x self.center_x += xt #_____________________________________________________________________________ def rotate(self, theta): #rotate by angle theta about the origin #get new center_z and center_x by TVector2 rotation vec = TVector2(self.center_z, self.center_x).Rotate(theta) self.center_z = vec.X() self.center_x = vec.Y() #rotate along magnet center self.THETA = self.THETA - theta #_____________________________________________________________________________ def draw_2d(self): #draw only magnets with survey defined if not self.has_survey: return #self.draw_box() self.draw_graph() #_____________________________________________________________________________ def draw_graph(self): #inner and outer radius if self.center_z < 0: rad_right = self.rad1 rad_left = self.rad2 else: rad_right = self.rad2 rad_left = self.rad1 #edge points of the magnet vec = [] vec.append(TVector2(self.length / 2, rad_right)) vec.append(TVector2(self.length / 2, -rad_right)) vec.append(TVector2(-self.length / 2, -rad_left)) vec.append(TVector2(-self.length / 2, rad_left)) #rotate along magnet axis and move to magnet center vpos = TVector2(self.center_z, self.center_x) for i in xrange(len(vec)): vec[i] = vec[i].Rotate(-self.THETA) + vpos #export points to the graph self.gbox = TGraph(len(vec) + 1) self.gbox.SetLineColor(self.line_col) self.gbox.SetLineWidth(2) self.gbox.SetFillStyle(self.fill_style) self.gbox.SetFillColor(self.fill_col) for i in xrange(len(vec)): self.gbox.SetPoint(i, vec[i].X(), 100 * vec[i].Y()) #last point same as the first self.gbox.SetPoint(len(vec), vec[0].X(), 100 * vec[0].Y()) self.gbox.Draw("lfsame") #label if self.no_label: return #lx = (self.center_x + self.rad2)*100 + 4 lx = (self.center_x + (self.rad1 + self.rad2) / 2) * 100 + 4 if lx < 30: lx = 30 align = 12 #left down if (self.center_z < 0 and not self.is_electron) or self.label_down: lx = (self.center_x - self.rad2) * 100 - 4 align = 32 #right down if self.center_z > 0 and self.is_electron: lx = (self.center_x - self.rad2) * 100 - 4 if lx > -25: lx = -25 align = 32 #label above the magnet if self.center_x < -0.4: lx = (self.center_x + self.rad2) * 100 + 4 align = 12 if self.label == "": self.label = self.name #self.glabel = TText(self.center_z, lx, self.label) self.glabel = TLatex(self.center_z, lx, self.label) self.glabel.SetTextSize(0.03) #self.glabel.SetTextSize(0.02) self.glabel.SetTextAngle(90) self.glabel.SetTextAlign(align) self.glabel.Draw("same") #_____________________________________________________________________________ def draw_box(self): z1 = self.center_z - self.length / 2 z2 = z1 + self.length x1 = self.center_x - self.rad2 x2 = x1 + 2 * self.rad2 #to cm x1 *= 100 x2 *= 100 #representation as a box self.box = TFrame(z1, x1, z2, x2) self.box.SetBorderMode(0) self.box.SetFillColor(rt.kGray + 1) col = rt.kRed if self.is_electron == True: col = rt.kBlue self.box.SetLineColor(col) self.box.SetLineWidth(2) #self.box.Draw("same") #label lx = x2 + 2 align = 11 if lx < 0 and lx > -62: lx = x1 - 5 # negative lx align = 31 if self.center_z < 0 and self.center_x < 0.1: lx = x1 - 2 # negative z and x align = 31 if lx > 0 and lx < 22: lx = 22 # small positive lx self.label = TText(z2, lx, self.name) self.label.SetTextSize(0.03) self.label.SetTextAngle(90) self.label.SetTextAlign(align)