From: <ro...@us...> - 2011-01-19 10:36:20
|
Revision: 1792 http://nsclspectcl.svn.sourceforge.net/nsclspectcl/?rev=1792&view=rev Author: ron-fox Date: 2011-01-19 10:36:13 +0000 (Wed, 19 Jan 2011) Log Message: ----------- Add fitting to plotCBar and add a button for it in the button bar. Modified Paths: -------------- branches/LLNLMadcChainSpecTcl/SpecTclRC.tcl Added Paths: ----------- branches/LLNLMadcChainSpecTcl/plotCbar.tcl Modified: branches/LLNLMadcChainSpecTcl/SpecTclRC.tcl =================================================================== --- branches/LLNLMadcChainSpecTcl/SpecTclRC.tcl 2011-01-18 11:01:38 UTC (rev 1791) +++ branches/LLNLMadcChainSpecTcl/SpecTclRC.tcl 2011-01-19 10:36:13 UTC (rev 1792) @@ -160,6 +160,10 @@ # puts -nonewline "Adding plotting interface.." + +# Not using these plots for now... + +if {0} { source [file join $here plotTotalMult.tcl] button .ptotal -text {TotalMult} -command [list totalMultiplicity .tm] pack .ptotal -fill x -expand 1 @@ -167,10 +171,13 @@ source [file join $here plotMultBins.tcl] button .pmult -text {Specific Mult} -command [list plotMultBins .pb] pack .pmult -fill x -expand 1 - +} source [file join $here plotGateMult.tcl] button .pgate -text {Gate Multiplicity} -command [list gateMultiplicity .pg] pack .pgate -fill x -expand 1 +source [file join $here plotCbar.tcl] +button .pcbar -text {CBar} -command [list Cbar .pcb] +pack .pcbar -fill x -expand 1 puts done \ No newline at end of file Added: branches/LLNLMadcChainSpecTcl/plotCbar.tcl =================================================================== --- branches/LLNLMadcChainSpecTcl/plotCbar.tcl (rev 0) +++ branches/LLNLMadcChainSpecTcl/plotCbar.tcl 2011-01-19 10:36:13 UTC (rev 1792) @@ -0,0 +1,215 @@ +# This software is Copyright by the Board of Trustees of Michigan +# State University (c) Copyright 2009. +# +# You may use this software under the terms of the GNU public license +# (GPL). The terms of this license are described at: +# +# http://www.gnu.org/licenses/gpl.txt +# +# Author: +# Ron Fox +# NSCL +# Michigan State University +# East Lansing, MI 48824-1321 + + +package require Tk +package require snit + + +# +# Code to plot CBAR as a function of time gate. +# +# The following control panel controls the plot: +# +# +--------------------------------------------------------------+ +# | [ ] Log Scale | +# | ( ) View ( ) Postscript | +# | XTitle [Gate ] | +# | YTitle [CBar ] | +# | Title [CBar v Gate ] | +# +--------------------------------------------------------------+ +# | [ Plot ] [ Cancel ] | +# +--------------------------------------------------------------+ + +snit::widget Cbar { + hulltype toplevel + # + # Default values for 'stuff' + # + typevariable defaultLogScale 0 + typevariable defaultPostscript 0 + typevariable defaultXtitle {Gate} + typevariable defaultYtitle {Cbar} + typevariable defaultTitle {Cbar vs. Gate} + + + # The following hold the actual values through the life of the dialog + + variable logScale + variable postscript + variable Xtitle + variable Ytitle + variable title + + + constructor args { + + + # + # Build the widgets: + # + + set work [ttk::frame $win.work -relief groove -borderwidth 3]; # Top frame with work controls + set action [ttk::frame $win.action]; # Bottom frame with action buttons. + + # Work area controls. + + ttk::checkbutton $work.log -text {Log Scale} -variable [myvar logScale] + ttk::radiobutton $work.screen -text {View} -variable [myvar postscript] -value 0 + ttk::radiobutton $work.postscript -text {Postscript} -variable [myvar postscript] -value 1 + ttk::label $work.xtitlel -text {X title} + ttk::entry $work.xtitle -textvariable [myvar Xtitle] + ttk::label $work.ytitlel -text {Y title} + ttk::entry $work.ytitle -textvariable [myvar Ytitle] + ttk::label $work.titlel -text {Plot Title} + ttk::entry $work.title -textvariable [myvar title] + + # Action area controls: + + ttk::button $action.plot -text {Plot} -command [mymethod plot] + ttk::button $action.cancel -text {Cancel} -command [mymethod cancel] + + # Layout the widgets + + grid $work.log -sticky w + grid $work.screen $work.postscript -sticky w + grid $work.xtitlel $work.xtitle -sticky w + grid $work.ytitlel $work.ytitle -sticky w + grid $work.titlel $work.title -sticky w + + grid $action.plot $action.cancel + + grid $work -sticky ew + grid $action -sticky ew + + # Apply the defaults + + set logScale $defaultLogScale + set postscript $defaultPostscript + set Xtitle $defaultXtitle + set Ytitle $defaultYtitle + set title $defaultTitle + + } + #------------------------- Action methods ------------------------------------- + + # + # cancel - called when the cancel action area button is clicked. + # $win is destroyed and no changes made to the typevariables. + # + method cancel {} { + after 100 [list destroy $win]; # Done to allow us exit before destruction. + } + # + # plot - Called when the plot button is clicked. A GNU Plot session is + # created and controlled via a pipe to plot the data + # + method plot {} { + $self gnuPlot + $self updateDefaults + $self cancel; # Kill the widget after the plot is done. + } + + #------------------------ Private methods --------------------------------- + + # Make the last set of values sticky to the next invocation + # + method updateDefaults {} { + set defaultLogScale $logScale + set defaultPostscript $postscript + set defaultXtitle $Xtitle + set defaultYtitle $Ytitle + set defaultTitle $title + + } + # + # Actually plot the data. + # + method gnuPlot {} { + set gateCount ${::multiplicity.bins}; # This many gates need plotting. + + # Create the data file: + + set dataFile [exec mktemp] + set df [open $dataFile w] + set sumxy 0.0 + set sumx2 0.0 + for {set i 0} {$i < $gateCount} {incr i} { + set y [cbar $i] + puts $df $y + set sumxy [expr {$sumxy + $i*$y}] + set sumx2 [expr {$sumx2 + $i*$i}] + } + close $df + + # now the fit: + + if {$sumx2 != 0.0} { + set m [expr {$sumxy/$sumx2}] + } else { + set m 0.0 + } + set fitFile [exec mktemp] + set fitf [open $fitFile w] + for {set i 0} {$i < $gateCount} {incr i} { + puts $fitf [expr {$i*$m}] + } + close $fitf + + # Open the gnu plot and tell it what to do: + # note that if we're making postscript we need to prompt for the + # output file and set the appropriate paramters in gnuplot for postscript + # output to that file: + + set gp [open |gnuplot w] + + if {$postscript} { + set outputFile [tk_getSaveFile \ + -defaultextension .ps \ + -title {Output file} \ + -filetypes { + {{Postscript} {.ps} } + {{All files} * } + } + ] + # + # An emtpy filename means he wants to reconsider the settings: + + if {$outputFile eq ""} return + puts $gp "set size 1.0, 0.6" + puts $gp "set terminal postscript portrait enhanced mono dashed lw 1 'Helvetica' 14" + puts $gp "set output '$outputFile'" + } + puts $gp "set title '$title : Fit slope $m'" + puts $gp "set xlabel '$Xtitle'" + puts $gp "set ylabel '$Ytitle'" + if {$logScale} { + puts $gp "set logscale y" + } + puts $gp "set key off" + puts $gp "plot '$dataFile' with points 3 7, '$fitFile' with points 1 22"; # blue solid circles + puts $gp "pause mouse button3 'Click right mouse button in window to close'" + puts $gp "exit" + flush $gp + fconfigure $gp -blocking 0; # otherwise the close will hang until gnuplot exits. + close $gp + + if {$postscript} { + exec gv $outputFile & + } + + + } + +} \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |