SPS-BCT3 lifetime calculation

<< Graphics`Colors`
<< Graphics`MultipleListPlot`
<< Graphics`Legend`

AllColors;

<< Experimental`FileBrowse

Procedures

Read-in bct data

bctRead[infile_] := {ref = OpenRead[infile]; ReadList[ref, String, 4]; 
        cycle = ReadList[ref, Word, 5][[5]]; 
        ReadList[ref, String, 5]; 
        units = ReadList[ref, Word, 4][[4]]; 
        ReadList[ref, String, 2]; 
        rawdata = ReadList[ref, {Number, Number, Number}]; Close[ref]; 
        Transpose[{Transpose[rawdata][[2]], Transpose[
                rawdata][[3]]}]}

\(\(\(filedepth[name_String] := 
      Length[StringPosition[name, "/"]]\)\(
\)
    \)\)
\(\(\(direxpand[deep_] := 
      With[{toodeep = 
            deep}, \((dir_String /; \((\(FileType[dir] === 
                        Directory && \((# > 0 && # < toodeep)\) &\)[
                  filedepth[dir]])\) :> {dir /. nameshrink, 
              FileNames["*", dir]})\)]\)\(
\)
    \)\)
\(\(\(nameshrink = \((name_String /; filedepth[name] > 0 :> 
          StringTake[
            name, {Last[Flatten[StringPosition[name, "/"]]] + 
                1, \(-1\)}])\)\)\(
\)
    \)\)
browse[depth_] := 
      TableForm[{Directory[] //. 
              direxpand[filedepth[Directory[]] + depth] //. nameshrink}, 
        TableAlignments -> Table[Top, {2  depth + 3}]]

browse[2]

Input data where the filename is provided

mydirectory = "G:\Users\j\jpk\MyDocs\Temp";

bctfilename = "sps_bct_3.txt";

wireramp = {{500, 0}, {1000, imax}, {4000, imax}, {4500, 
            0}};

startcalc = 1100; stopcalc = 4000;

Input data with file selection by an index

SetDirectory["G:\Users\j\jpk\MyDocs\Temp"];

files = FileNames[];

fileindex = 
        Table[{i, files[[i]]}, {i, 1, Length[files]}];

fileindex // TableForm

index = 7;

bctfilename = ToString[fileindex[[7]][[2]]];

wireramp = {{500, 0}, {1000, imax}, {4000, imax}, {4500, 
            0}};

startcalc = 1100; stopcalc = 4000;

Input data with file selection by a palette

SetDirectory["G:\Users\j\jpk\MyDocs\Temp"];

files = FileNames[];

Table[
        ButtonBox[files[[i]], Active -> True] // DisplayForm, {i, 1, 
          Length[files]}] // TableForm

bctfilename = sps_bct  _  3.  txt

wireramp = {{500, 0}, {1000, imax}, {4000, imax}, {4500, 
            0}};

startcalc = 1100; stopcalc = 4000;

Input data with file selection by a cycle number

SetDirectory["G:\Users\j\jpk\MyDocs\Temp"];
cyclenumber = 1234;
bctfilename = "sps_bct_3_MTG#" <> 
          ToString[cyclenumber] <> "xrx34";
wireramp = {{500, 0}, {1000, imax}, {4000, imax}, {4500, 
            0}};
startcalc = 1100; stopcalc = 4000;

Program

SetDirectory[mydirectory];
bctdata = bctRead[bctfilename][[1]];
imax = Max[Transpose[bctdata][[2]]];
debut = Position[Transpose[bctdata][[1]], 
              startcalc][[1]][[1]];
fin = Position[Transpose[bctdata][[1]], 
              stopcalc][[1]][[1]];
subrange = Take[bctdata, {debut, fin}];
lnsubrange = 
        Transpose[{Transpose[subrange][[1]], 
            Log[Transpose[subrange][[2]]]}];
fitequation = Fit[lnsubrange, {1, t}, t];
i0 = Exp[fitequation[[1]]];
tau = 
        1/(- 1000 fitequation[[2]] /. t -> 1);
decaycalcperiod = {{0, 0}, {startcalc, 0}, {startcalc, 
            Exp[fitequation] /. t -> startcalc}, {startcalc, 
            0}, {stopcalc, 0}, {stopcalc, 
            Exp[fitequation] /. t -> stopcalc}, {stopcalc, 0}, {4500, 
            0}};
p4 = 
        Plot[Exp[fitequation], {t, startcalc, stopcalc}, 
          PlotStyle -> Blue, DisplayFunction -> Identity];
p1 = 
        ListPlot[bctdata, PlotStyle -> Red, PlotJoined -> True, 
          Frame -> True, DisplayFunction -> Identity, 
          GridLines -> Automatic, 
          FrameLabel -> {"time (ms)",  "current (" <> 
                ToString[units] <> " protons)"}, 
          PlotLabel -> "current decay at cycle " <> 
              ToString[cycle]];
p2 = 
        ListPlot[wireramp, PlotStyle -> Green, PlotJoined -> True, 
          DisplayFunction -> Identity];
p3 = 
        ListPlot[decaycalcperiod, PlotStyle -> Blue, 
          PlotJoined -> True, 
          DisplayFunction -> Identity];
Show[p1, p2, p3, p4, 
      Graphics[Text["lifetime=" <> ToString[tau] <> " s", {2520, 
            21}]], DisplayFunction -> $DisplayFunction, 
      TextStyle -> {FontSize -> 14}]