Process Experiment 32 Scattering Data

Initialization code

Check for CurveFit

Needs["CurveFit`"];

Select an XY range to keep or remove

Clear[SetXYRange];
SetXYRange[plot:Blank[Graphics],OptionsPattern[{Log->{False,False},Label->""}]]:=Quiet[Block[{xr,yr,xinit1,yinit1,xinit2,yinit2,value,title,windowopts},If[!Check[{xr,yr}=PlotRange/.AbsoluteOptions[plot,PlotRange],False],Message[SetXRange::plotrange];Return[$Canceled]];If[xr=={0.,1.},xr={Min[xx],Max[xx]}];If[yr=={0.,1.},yr={Min[yy],Max[yy]}];xinit1=(Plus@@xr+xr[[1]])/3.;yinit1=(Plus@@yr+yr[[1]])/3.;xinit2=(Plus@@xr+xr[[2]])/3.;yinit2=(Plus@@yr+yr[[2]])/3.;title="Set X and Y Ranges";windowopts=If[$VersionNumber==6.&&$ReleaseNumber==0,{WindowTitle->title,WindowFloating->True},{WindowTitle->title,WindowSize->{Scaled[0.35],All},WindowFloating->True}];DynamicModule[{pt={{xinit1,yinit1},{xinit2,yinit2}},log=OptionValue[Log],prompt=OptionValue[Label],x1,x2,y1,y2},
If[Head[log]=!=List||Length[log]≠2,Clear[log]; log={False,False}];If[log[[1]]=!=True,log[[1]]=False];If[log[[2]]=!=True,log[[2]]=False];If[!StringQ[prompt],prompt=""];value=If[ChoiceDialog[Column[{prompt,Style["Drag the red lines to set the X and Y values:",Larger,Bold],Item[LocatorPane[Dynamic[pt],Show[plot,AspectRatio->1.0,Background->White,ImageSize->Scaled[1.]],Appearance->Graphics[{Red,Line[{{0,-1},{0,1}}],Line[{{-1,0},{1,0}}]},ImageSize->Scaled[2]]],Frame->True],Grid[{{Style["X1 = ",Larger,Bold],Style[Dynamic[x1=Min[If[log[[1]],Exp[pt[[1,1]]],pt[[1,1]]],If[log[[1]],Exp[pt[[2,1]]],pt[[2,1]]]]],Larger,Bold],
Style["   Y1 = ",Larger,Bold],Style[Dynamic[y1=Min[If[log[[2]],Exp[pt[[1,2]]],pt[[1,2]]],If[log[[2]],Exp[pt[[2,2]]],pt[[2,2]]]]],Larger,Bold]},{Style["X2 = ",Larger,Bold],Style[Dynamic[x2=Max[If[log[[1]],Exp[pt[[1,1]]],pt[[1,1]]],If[log[[1]],Exp[pt[[2,1]]],pt[[2,1]]]]],Larger,Bold],
Style["   Y2 = ",Larger,Bold],Style[Dynamic[y2=Max[If[log[[2]],Exp[pt[[1,2]]],pt[[1,2]]],If[log[[2]],Exp[pt[[2,2]]],pt[[2,2]]]]],Larger,Bold]}}]},Spacings->1.5],Sequence@@windowopts],{{x1,x2},{y1,y2}},$Canceled]];value],{Ticks::ticks}]

Clear[KeepXY];
KeepXY[]:=Block[{range=SetXYRange[ScatterDataPlot[]],xmin,xmax,ymin,ymax,data={},num,i},
If[range===$Canceled,Return[$Canceled]];
SaveForUndo[];
{{xmin,xmax},{ymin,ymax}}=range;
For[i=1,i≤n,++i,
If[(xmin≤xx[[i]]≤xmax)&&(ymin≤yy[[i]]≤ymax),
AppendTo[data,{xx[[i]],yy[[i]]}]
]
];
num=Length[data];
sx=sy=ConstantArray[0,num];
{xx,yy}=Transpose[data];
n=num;
Print["n = "<>ToString[n]];
Print["Undo[] to return to previous data set."];
ScatterDataPlot[AspectRatio->1]
]
KeepXY::usage="KeepXY [ ] opens a dialog box to select a subset of the "<>
"current data set. Dragging the red lines on the plot identifies a "<>
"rectangular block of the data set to keep. Returns a "<>
"ScatterPlot[ ] of the new data set. Use Undo[ ] to recover "<>
"the original data set.";

Clear[RemoveXY];
RemoveXY[]:=Block[{range=SetXYRange[ScatterDataPlot[]],xmin,xmax,ymin,ymax,data={},num,i},
If[range===$Canceled,Return[$Canceled]];
SaveForUndo[];
{{xmin,xmax},{ymin,ymax}}=range;
For[i=1,i≤n,++i,
If[!((xmin≤xx[[i]]≤xmax)&&(ymin≤yy[[i]]≤ymax)),
AppendTo[data,{xx[[i]],yy[[i]]}]
]
];
num=Length[data];
sx=sy=ConstantArray[0,num];
{xx,yy}=Transpose[data];
n=num;
Print["n = "<>ToString[n]];
Print["Undo[] to return to previous data set."];
ScatterDataPlot[AspectRatio->1]
]
RemoveXY::usage="RemoveXY [ ] opens a dialog box to select a subset of the "<>
"current data set. Dragging the red lines on the plot identifies a "<>
"rectangular block of the data set to remove. Returns a "<>
"ScatterPlot[ ] of the new data set. Use Undo[ ] to recover "<>
"the original data set.";

Scatter Plot to detector spectrum functions

Clear[xy2x];
xy2x[]:=Block[{xdata,ydata,num,xsig,ysig,data},
num=Floor[Max[xx]]+1;
xdata=Range[0,num-1];
ydata=ConstantArray[0,num];
Scan[(++ydata[[Floor[#]+1]])&,xx];
data=Select[Transpose[{xdata,ydata}],#[[2]]>0&];
{xdata,ydata}=Transpose[data];
num=Length[xdata];
SaveForUndo[];
sx=sy=ConstantArray[0,num];
{n,xx,yy}={num,xdata,1.0 ydata};
Print["n = "<>ToString[n]];
MakePoisson[Xerrors -> False];
Print["Undo[] once to get rid of channel count sigmas."];
Print["Undo[] again to return to Scatter Data."];
Block[{CurveFit`$DataSizeThreshold={0,0}},LinearDataPlot[] ]
]
xy2x::usage="xy2x [ ] converts a scatter plot to an X (target scintillator) "<>
"histogram and assigns Poisson count uncertainties. It generates a plot "<>
"of the resulting data set. Use Undo[ ] twice to return to the original "<>
"data set.";

Clear[xy2y];
xy2y[]:=Block[{xdata,ydata,num,xsig,ysig,data},
num=Floor[Max[yy]]+1;
xdata=Range[0,num-1];
ydata=ConstantArray[0,num];
Scan[(++ydata[[Floor[#]+1]])&,yy];
data=Select[Transpose[{xdata,ydata}],#[[2]]>0&];
{xdata,ydata}=Transpose[data];
num=Length[xdata];
SaveForUndo[];
sx=sy=ConstantArray[0,num];
{n,xx,yy}={num,xdata,1.0 ydata};
Print["n = "<>ToString[n]];
MakePoisson[Xerrors -> False];
Print["Undo[] once to get rid of channel count sigmas."];
Print["Undo[] again to return to Scatter Data."];
Block[{CurveFit`$DataSizeThreshold={0,0}},LinearDataPlot[] ]
]
xy2y::usage="xy2y [ ] converts a scatter plot to a Y (NaI scintillator) "<>
"histogram and assigns Poisson count uncertainties. It generates a plot "<>
"of the resulting data set. Use Undo[ ] twice to return to the original "<>
"data set.";

Cleanup

SelectionMove[EvaluationNotebook[],After,Notebook]

The defined functions’ usage messages

?KeepXY
?RemoveXY
?xy2x
?xy2y

Working area