<< NumericalMath`ListIntegrate`
<< Statistics`DataSmoothing`
<< NumericalMath`PolynomialFit`
hide = DisplayFunction -> Identity
SetDirectory["/afs/cern.ch/user/a/afabich/public/MERIT"]
<< "prolog.m"

    
SetDirectory["/nfs/cs-ccr-nfs4/data/slopsdata/2007/MD_DATA/CPS_MERIT/\
MERIT_libera"]
Files = FileNames["2007*result.txt"];

\(\(\( (*\ 
      Read\ run\ list\ of\ Marcus\ and\ modify\ *) \)\(
\)\(\(\(ReadList["/afs/cern.ch/user/a/afabich/public/MERIT/RunList.data", String]\)[\([1]\)]\)\(
\)
    \(RunList = 
Transpose[
Map[
            StringSplit, 
Drop[
ReadList["/afs/cern.ch/user/a/afabich/public/MERIT/RunList.data", String]
, 
              1]
]
];\)\(
\)
    \(Harmonic[x_] := 
      If[x == "1.31e-007" || 
          x == "1.31288e-007", "16", 
        If[x == "2.62576e-007" || 
            x == "2.62e-007", "8", 
          If[x == "5.25e-007", "5.25", 
            If[x == "5.27e-007", "6", 
              If[x == "7.95e-007", "8", 
                x]]]]]\)\(
\)
    \(RunList = 
        ReplacePart[RunList, Map[Harmonic, RunList[\([2]\)]], 
          2];\)\(
\)
    \(RunList = 
        Select[
ToExpression[
            Transpose[RunList]
], #[\([1]\)] ≠ 
              0 &];\)\(
\)
    \(RunListOff = 
        Select[
ToExpression[
            RunList]
, #[\([2]\)] ≠ 
                16 && #[\([2]\)] ≠ 
                8 &];\)\(\ \)\(
\)
    \(RunList8 = 
        Select[
ToExpression[
            RunList]
, #[\([2]\)] == 
              8 &];\)\(\ \)\(
\)
    \(RunList16 = 
        Select[
ToExpression[
            RunList]
, #[\([2]\)] == 
              16 &];\)\(\ \)\(
\)
    \(RunList = RunList8;\)\(
\)
    \(RunList = 
        Join[RunList, 
          Select[RunList16, 
            IntegerPart[#[\([\)\(1\)\(]\)]/1000] == 
                17 &]];\)\(
\)
    \(RunList82 = 
        Select[
ToExpression[
            RunList8]
, #[\([10]\)] == 
              2 &];\)\(\ \)\(
\)
    \(RunList[\([1]\)]\)\(
\)
    \( (*\ Check\ availability\ of\ input\ files\ *) \)\)\)\)

AllRuns = {};
\(\(\(Do[{
t = m, 
data = 
          ReadList["/nfs/cs-ccr-nfs4/data/slopsdata/2007/MD_DATA/CPS_MERIT/MERIT_libera/" <> 
ToString[
                RunList82[\([t, 4]\)]] <> "-" <> 
              ToString[RunList82[\([t, 5]\)]] <> "-" <> 
              ToString[RunList82[\([t, 6]\)]] <> "_" <> 
              ToString[RunList82[\([t, 7]\)]] <> "-" <> 
              ToString[RunList82[\([t, 8]\)]] <> "-" <> 
              ToString[RunList82[\([t, 9]\)]] <> "_MEAS_result.txt", 
            Number], 
data = 
          N[data/Max[data]], 
shift = 
          Last[Last[
              Position[
                Take[data, 
                  950], _?\((# > 0.2 &)\)]]], 
shift = 
          800, 
data = 
          Drop[data, 1600 - shift], 
part1 = 
          Take[data, 200], 
part2 = 
          Take[Drop[data, 200 - 69], 1000], 
AppendTo[
          AllRuns, 
part1
], 
AppendTo[
          AllRuns, 
part2 + 
            0.015
]
}, {m, 3, 
        8}]\)\(
\)
    \)\)
Aver = {};
Do[{
part = {}, 
Do[
AppendTo[part, 
            AllRuns[[m, k]]]
, {m, 
            If[k ≤ 200, 1, 2], 8, 
            If[k ≤ 200, 1, 2]}], 
AppendTo[Aver, 
          Mean[part]]
}, {k, 
        Length[AllRuns[[-1]]]}]
Bg = 
        Take[Join[Table[0, {94}], Drop[Aver, 94]], 
          250];
MultipleListPlot[AppendTo[AppendTo[AllRuns, Aver], Bg], 
      PlotRange -> {{1, 300}, {-0.2, 0.2}}, ImageSize -> 900, 
      PlotJoined -> True, 
      PlotStyle -> {RGBColor[0, 0, 0], RGBColor[0, 0, 0], 
          RGBColor[0, 0, 0], RGBColor[0, 0, 0], RGBColor[0, 0, 0], 
          RGBColor[0, 0, 0], RGBColor[0, 0, 0], RGBColor[0, 0, 0], 
          RGBColor[0, 0, 0], RGBColor[0, 0, 0], RGBColor[0, 0, 0], 
          RGBColor[0, 0, 0], RGBColor[1, 0, 0], 
          RGBColor[0, 1, 0]}]

\(Do[
If[
        1 ≠ 
FileNames[
"/nfs/cs-ccr-nfs4/data/slopsdata/2007/MD_DATA/CPS_MERIT/MERIT_libera/" <> 
ToString[RunList[\([k, 4]\)]] <> "-" <> 
              ToString[RunList[\([k, 5]\)]] <> "-" <> 
              ToString[RunList[\([k, 6]\)]] <> "_" <> 
              ToString[RunList[\([k, 7]\)]] <> "-" <> 
              ToString[RunList[\([k, 8]\)]] <> "-" <> 
              ToString[
                RunList[\([k, 
                    9]\)]] <> "_MEAS_result.txt"]
, 
        Print["missing"]
]
, {k, 
        Length[RunList]}]\)
\(\(\(Print["if not <missing> printed, every file is available"]\)\(
\)\(
\)
    \)\)

SetDirectory["/nfs/cs-ccr-nfs4/data/slopsdata/2007/MD_DATA/CPS_\
MERIT/MERIT_libera"]

      
Pulse = Aver;
func = 
        Interpolation[Pulse, InterpolationOrder -> 5];
func2 = 
        Interpolation[Bg, 
          InterpolationOrder -> 5];

    
PulseMaxI = 
      First[First[Position[Pulse, Max[Take[Pulse, {75, 95}]]]]]
PulseMax = Table[{x/1000, func2[x]}, {x, 75, 200, 0.05}];
PulseH = PulseMax[[First[
              First[Position[PulseMax, 
                  Max[PulseMax]]]]]][[1]]
PulseMax = 
      Max[Transpose[PulseMax][[2]]]

    
StartOsc = Select[
Position[
            Bg, _?(# < 0 &)]
, #[[1]] > 
              10 &][[1, 1]]

    
func = 
        Interpolation[Pulse, 
          InterpolationOrder -> 5];

    
offset = 
      First[First[
          Position[
            Bg, _?(# ≤ - .01 &)]]]

    
DisplayTogether[
ListPlot[Pulse, 
        PlotRange -> {{1, 200}, All}], 
Plot[
        func[x], {x, 1, 1000}, DisplayFunction -> $DisplayFunction, 
        PlotStyle -> RGBColor[1, 0, 0]], 
Plot[
        func2[x], {x, 75, 250}, 
        PlotStyle -> 
          RGBColor[0, 1, 0]]
]
Pulse = Bg;

\(\(\( (*\ 
      Verify\ coherence\ of\ Mercus\ input\ file\ and\ outfile\ report2\ *) \)\(
\)\(\(report2 = 
        ReadList["/afs/cern.ch/user/a/afabich/public/MERIT/transformer.dat", Table[Number, {40}]];\)

    \(check = 0;\)

    Do[{
If[
          Length[Position[\(Transpose[report2]\)[\([1]\)], 
                report2[\([k, 1]\)]]] ≠ 
            1, {Print[k, ":", 
              report2[\([k, 
                  1]\)], " several entries for single run"], \(check++\)}], 
pos = 
          First[First[
              Position[\(Transpose[RunList]\)[\([1]\)], 
                report2[\([k, 1]\)]]]], 
If[\(Transpose[
                RunList]\)[\([10, pos]\)] ≠ \(Transpose[
                report2]\)[\([8, k]\)], {Print[
              k, ": ", \(Transpose[RunList]\)[\([10, 
                  pos]\)], "<->", \(Transpose[report2]\)[\([8, 
                  k]\)], " different bunch number"], \(check++\)}]
}, {k, Length[report2]}]

    If[check ≠ 0, 
      Print["differences in databases!!!!"]]\)\)\)

SetDirectory["/nfs/cs-ccr-nfs4/data/slopsdata/2007/MD_DATA/CPS_MERIT/\
MERIT_libera"]
debug = 0;
report = {};
report2 = 
        ReadList["/afs/cern.ch/user/a/afabich/public/MERIT/transformer8.dat\
", Table[Number, {41}]];
\(Do[{
Print[k, ": Working on run: ", 
          RunList[\([k, 1]\)]], 
data = 
          ReadList["/nfs/cs-ccr-nfs4/data/slopsdata/2007/MD_DATA/CPS_MERIT/MERIT_libera/" <> 
ToString[
                RunList[\([k, 4]\)]] <> "-" <> 
              ToString[RunList[\([k, 5]\)]] <> "-" <> 
              ToString[RunList[\([k, 6]\)]] <> "_" <> 
              ToString[RunList[\([k, 7]\)]] <> "-" <> 
              ToString[RunList[\([k, 8]\)]] <> "-" <> 
              ToString[RunList[\([k, 9]\)]] <> "_MEAS_result.txt", 
            Number], 
FullData = 
          Transpose[{Table[m, {m, Length[data]}], 
              data}], 
 (*\ \(ListPlot[
            data]\)\(,\)\ *) 
Integral = {data[\([1]\)]}, 
Do[
          AppendTo[Integral, 
            data[\([m]\)] + Integral[\([\(-1\)]\)]]
, {m, 
            2, Length[data]}], 
Hpeak = 
          Max[data], 

LowBound = {}, 
Do[
If[
            data[\([m - 1]\)] < Hpeak/2 ≤ data[\([m]\)], 
            AppendTo[LowBound, {m, data[\([m]\)]}]]
, {m, 
            2, Length[
              data]}], 
HiBound = {}, 
Do[
If[
            data[\([m - 1]\)] > Hpeak/2 ≥ data[\([m]\)], 
            AppendTo[HiBound, {m, data[\([m]\)]}]]
, {m, 2, 
            Length[data]}], 

PeakPos = 
          IntegerPart[
            N[\((\(Transpose[LowBound]\)[\([1]\)] + \(Transpose[
                        HiBound]\)[\([1]\)])\)/
                2]], 
BunchStart = {}, 
        BunchDel = 
          
          5, 
Do[
Do[
If[data[\([n - 1]\)] > data[\([n]\)] || 
                n == LowBound[\([m, 1]\)] - BunchDel, {AppendTo[
                  BunchStart, {n, data[\([n]\)]}], 
                Break[]}]
, {n, LowBound[\([m, 1]\)], 
              LowBound[\([m, 1]\)] - 
                BunchDel, \(-1\)}]
, {m, 
            Length[LowBound]}], 
Bunch = {}, 
Do[
AppendTo[
            Bunch, 
Table[{n, data[\([n]\)]}, {n, 
                BunchStart[\([m, 1]\)], 
                BunchStart[\([m, 1]\)] + 
                  22}]
]
, {m, 
            Length[BunchStart]}], 
Bunch = 
          Flatten[Bunch, 1], 

If[
          debug ≥ 2, 
          Export["/afs/cern.ch/user/a/afabich/public/MERIT/pic1.eps", 
            Show[GraphicsArray[{
DisplayTogether[
ListPlot[data, 
                      PlotRange -> {{LowBound[\([1, 1]\)] - 100, 
                            HiBound[\([\(-1\), 1]\)] + 300}, All}, 
                      PlotStyle -> RGBColor[0, 0, 0], 
                      PlotJoined -> False, 
                      hide], 
ListPlot[LowBound, 
                      PlotStyle -> RGBColor[1, 0, 0], 
                      PlotJoined -> False], 
ListPlot[
                      HiBound, PlotStyle -> RGBColor[1, 0, 0], 
                      PlotJoined -> False]
, 
                    DisplayFunction -> 
                      Identity], 
ListPlot[Integral, hide, 
                    FrameTicks -> {True, False, False, 
                        False}]
}, 
                ImageSize -> 
                  800]]]], 

ShowPeak = 
          1, 
If[debug ≥ 1, 
          Show[GraphicsArray[{
DisplayTogether[
ListPlot[data, 
                    PlotRange -> {{LowBound[\([ShowPeak, 1]\)] - 30, 
                          HiBound[\([ShowPeak, 1]\)] + 50}, All}, 
                    PlotStyle -> RGBColor[0, 0, 0], 
                    PlotJoined -> False, hide, 
                    PlotLabel -> "First bunch"], 
ListPlot[Bunch, PlotStyle -> RGBColor[0, 1, 0], 
                    PlotJoined -> False], 
ListPlot[
                    LowBound, PlotStyle -> RGBColor[1, 0, 0], 
                    PlotJoined -> False], 
ListPlot[
                    HiBound, PlotStyle -> RGBColor[1, 0, 0], 
                    PlotJoined -> False], 
ListPlot[
                    Transpose[{PeakPos, data[\([PeakPos]\)]}], 
                    PlotStyle -> RGBColor[1, 0, 0], 
                    PlotJoined -> False], 
ListPlot[
                    BunchStart, PlotStyle -> RGBColor[1, 0, 0], 
                    PlotJoined -> False]
, 
                  DisplayFunction -> 
                    Identity], 
DisplayTogether[
ListPlot[data, 
                    PlotRange -> {{LowBound[\([\(-1\), 1]\)] - 30, 
                          HiBound[\([\(-1\), 1]\)] + 50}, All}, 
                    PlotStyle -> RGBColor[0, 0, 0], 
                    PlotJoined -> False, hide, 
                    PlotLabel -> "Last bunch"], 
ListPlot[LowBound, PlotStyle -> RGBColor[1, 0, 0], 
                    PlotJoined -> False], 
ListPlot[
                    HiBound, PlotStyle -> RGBColor[1, 0, 0], 
                    PlotJoined -> False], 
ListPlot[
                    BunchStart, PlotStyle -> RGBColor[1, 0, 0], 
                    PlotJoined -> False]
, 
                  DisplayFunction -> Identity]
}, 
              ImageSize -> 
                900]]], 

dataM = {}, 
dataH = {}, 

Back = 
          Complement[FullData, 
            Bunch], 
Back2 = {}, 
temp = {Back[\([1]\)]}, 
s = 
          0, 
Do[
If[\((Back[\([m, 
                      1]\)] == temp[\([\(-1\), 1]\)] + 1)\) && \((s < 
                  250)\), 
{AppendTo[temp, 
                Back[\([m]\)]], \(s++\)}
, {AppendTo[Back2, 
                Mean[temp]], temp = {Back[\([m]\)]}, 
              s = 0}]
, {m, 2, 
            Length[Back]}], 

PrependTo[
          Back2, {1, \(Transpose[Back2]\)[\([1, 
                2]\)]}], 
func3 = 
          Interpolation[Back2, 
            InterpolationOrder -> 1], 
func4[z_] := 
          0, 
DataMod = 
          Take[\(Transpose[FullData]\)[\([2]\)], {1, \(-1000\)}] - 
            0.8  func3[
                Take[\(Transpose[
                      FullData]\)[\([1]\)], {1, \(-1000\)}]]
, 

Do[{
dataP = 
              Take[DataMod, {IntegerPart[\(LowBound[\([m, 1]\)] + HiBound[\([m, 1]\)]\)\/2] + 35, 
                  IntegerPart[\(LowBound[\([m, 1]\)] + HiBound[\([m, 1]\)]\)\/2] + 47}], 
funcP = 
              PolynomialFit[dataP, 5], 
dataMi = 
              Table[{x, funcP[x]}, {x, 1, Length[dataP], 
                  0.05}], 
dataMi = \(dataMi[\([First[
                      First[Position[dataMi, 
                          Max[dataMi]]]]]\)]\)[\([1]\)], 
AppendTo[dataM, 
              dataMi + \(LowBound[\([m, 1]\)] + HiBound[\([m, 1]\)]\)\/2 + 
                46 - 1], 
AppendTo[dataH, 
              funcP[dataMi]], 

DataModOld = 
              DataMod, 
FirstFit = {}, 
Do[{
DataMod = 
                  ReplacePart[DataMod, 
                    DataMod[\([p]\)] - 
                      func2[p - dataM[\([m]\)] + PulseH + 137]\ /PulseMax*
                        dataH[\([m]\)], p], 
AppendTo[
                  FirstFit, {p, 
                    func2[p - dataM[\([m]\)] + PulseH + 137]\ /PulseMax*
                      dataH[\([m]\)]}]
}, {p, 
                Ceiling[1 + dataM[\([m]\)] - PulseH - 137], 
                Ceiling[1 + dataM[\([m]\)] - PulseH] + 240 - 
                  137}], 

If[
              Mod[m, 1] == 100, 
Show[
                GraphicsArray[{
MultipleListPlot[data, 
                      DataModOld, DataMod, 
                      FirstFit, 
PlotRange -> {{850, 
                            1250}, {\(-1000\), 1000}}, 
                      PlotStyle -> {RGBColor[0, 0, 0], 
                          RGBColor[0, 1, 1], RGBColor[1, 0, 0], 
                          RGBColor[0, 1, 0]}, 
                      hide], 
DisplayTogether[
ListPlot[dataP, 
                        PlotRange -> {All, 
                            All}], 
Plot[
                        funcP[x], {x, 1, Length[dataP]}, 
                        DisplayFunction -> $DisplayFunction, 
                        PlotStyle -> 
                          RGBColor[1, 0, 0]]
, 
                      DisplayFunction -> Identity]
}, 
                  ImageSize -> 1200]]
], 
            If[m == 100, Break[]]
}, {m, 1, 
            Length[LowBound]}], 

NewFullIntegral = {DataMod[\([1]\)]}, 
Do[
AppendTo[
            NewFullIntegral, 
NewFullIntegral[\([\(-1\)]\)] + DataMod[\([m]\)]
]
, {m, 1, 
            Length[DataMod]}], 

level = {}, 
Do[
If[
            NewFullIntegral[\([m]\)] - 
                NewFullIntegral[\([m - 1]\)] ≤ 
              Max[NewFullIntegral]/
                  Length[LowBound]\ 0.003
, 
            AppendTo[
              level, {m, NewFullIntegral[\([m]\)]}]]
, {m, 
            2, HiBound[\([\(-1\), 1]\)] + 
              50}], 

levels = {}, 
levelpos = {}, 
temp = {level[\([1]\)]}, 
Do[
If[
            level[\([m, 2]\)] - temp[\([\(-1\), 2]\)] ≤ 
              Max[NewFullIntegral]/Length[LowBound]\ 0.2
, 
            AppendTo[temp, 
              level[\([m]\)]]
, {AppendTo[levels, temp], 
              AppendTo[levelpos, level[\([m - 1, 1]\)]], 
              temp = {level[\([m]\)]}}
]
, {m, 2, Length[level]}], 
AppendTo[levels, 
          temp], 
levels = 
          ReplacePart[levels, Take[levels[\([1]\)], \(-20\)], 
            1], 
AppendTo[levelpos, 
          level[\([\(-1\), 
              1]\)]], 

GrepLo[x_] := 
          If[Length[x] > 30, Take[x, 30], x], 
GrepHi[x_] := 
          If[Length[x] > 30, Take[x, \(-30\)], 
            x], 
LoLevel = 
          Map[GrepLo, levels], 
HiLevel = 
          Map[GrepHi, levels], 
CalcAverage[x_] := 
          N[Mean[\(Transpose[x]\)[\([2]\)]]], 
CalcError[
            x_] := N[
            StandardDeviation[\(Transpose[
                  x]\)[\([2]\)]]], 
Intensities = 
          Drop[Map[CalcAverage, LoLevel], 1] - 
            Drop[Map[CalcAverage, 
                HiLevel], \(-1\)], 
Error = 
          Sqrt[Drop[Map[CalcError, LoLevel], 1]\^2 + 
              Drop[Map[CalcError, HiLevel], \(-1\)]\^2], 

If[
          debug ≥ 
            2, {
\(ListPlot[Transpose[{dataM, dataH}], 
              PlotLabel -> "Bunch intensities"]\)\(,\)
}], 
If[
          debug ≥ 
            2, {
Export["/afs/cern.ch/user/a/afabich/public/MERIT/pic6.eps", 
Show[
                GraphicsArray[{
DisplayTogether[
ListPlot[data, 
                        PlotRange -> {{820, 2200}, {\(-500\), 500}}, 
                        PlotLabel -> "Error plot"], 
ListPlot[DataMod, 
                        PlotStyle -> 
                          RGBColor[1, 0, 0]]
, 
                      DisplayFunction -> 
                        Identity], 
DisplayTogether[
ListPlot[data, 
                        PlotRange -> {{820, 1050}, {\(-500\), 500}}, 
                        PlotLabel -> "Error plot"], 
ListPlot[DataMod, 
                        PlotStyle -> 
                          RGBColor[1, 0, 0]]
, 
                      DisplayFunction -> Identity]
, 
                    DisplayTogether[
ListPlot[data, 
                        PlotRange -> {{LowBound[\([\(-1\), 1]\)] - 100, 
                              HiBound[\([\(-1\), 1]\)] + 20}, {\(-500\), 
                              1500}}, 
                        PlotLabel -> "Error plot"], 
ListPlot[DataMod, 
                        PlotStyle -> 
                          RGBColor[1, 0, 0]]
, 
                      DisplayFunction -> Identity]
}, 
                  ImageSize -> 
                    900]]
]}], 

If[debug ≥ 1, 
          Show[GraphicsArray[{
MultipleListPlot[
                  NewFullIntegral, 
                  level, 
Flatten[HiLevel, 1], 
                  Flatten[LoLevel, 1], 
Join[
                    Transpose[{\(Transpose[HiBound]\)[\([1]\)], 
                        Drop[Map[CalcAverage, LoLevel], 
                          1]}], 
Transpose[{\(Transpose[
                            LowBound]\)[\([1]\)], 
                        Drop[Map[CalcAverage, 
                            HiLevel], \(-1\)]}]]
, 
                  PlotRange -> {{850, HiBound[\([\(-1\), 1]\)] + 200}, 
                      All}, PlotJoined -> {True, False, False, False, 
                      False}, 
                  SymbolShape -> {None, PlotSymbol[Triangle], 
                      PlotSymbol[Triangle], PlotSymbol[Triangle], 
                      PlotSymbol[Box]}, 
                  SymbolStyle -> {RGBColor[0, 0, 0], RGBColor[1, 0, 0], 
                      RGBColor[0, 0, 1], RGBColor[0, 0, 1], 
                      RGBColor[0, 1, 0]}, 
                  hide], 
MultipleListPlot[NewFullIntegral, 
                  level, 
Flatten[HiLevel, 1], 
                  Flatten[LoLevel, 1], 
Join[
                    Transpose[{\(Transpose[HiBound]\)[\([1]\)], 
                        Drop[Map[CalcAverage, LoLevel], 
                          1]}], 
Transpose[{\(Transpose[
                            LowBound]\)[\([1]\)], 
                        Drop[Map[CalcAverage, 
                            HiLevel], \(-1\)]}]]
, 
                  PlotRange -> {{LowBound[\([\(-1\), 1]\)] - 200, 
                        HiBound[\([\(-1\), 1]\)] + 100}, All}, 
                  PlotJoined -> {True, False, False, False, False}, 
                  SymbolShape -> {None, PlotSymbol[Triangle], 
                      PlotSymbol[Triangle], PlotSymbol[Triangle], 
                      PlotSymbol[Box]}, 
                  SymbolStyle -> {RGBColor[0, 0, 0], RGBColor[1, 0, 0], 
                      RGBColor[0, 0, 1], RGBColor[0, 0, 1], 
                      RGBColor[0, 1, 0]}, hide]
}, 
              ImageSize -> 
                1100]]
], 

result = 
          Join[{RunList[\([k, 1]\)]}, 
            Take[RunList[\([k]\)], {4, 9}], {Length[
                LowBound]}, 
Intensities, 
            Table[0, {16 - Length[LowBound]}], 
N[
              IntegerPart[Error/Intensities\ 100\ 100]/100], 
            Table[0, {16 - Length[LowBound]}], {If[Max[data] > 7900, 1, 
                0]}
], 
If[
          result[\([8]\)] > 16 || 
            RunList[\([k, 10]\)] ≠ result[\([8]\)] || 
            Length[result] ≠ 41, {
result = 
              Join[{result[\([1]\)]}, Table[0, {40}]], 
            Print["FAULTY RUN ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"]
}], 
PosRep = 
          Position[\(Transpose[report2]\)[\([1]\)], 
            RunList[\([k, 1]\)]], 
If[PosRep == {}, 
          AppendTo[report2, result], 
          report2 = 
            ReplacePart[report2, result, 
              First[First[
                  PosRep]]]], 
 (*\ \(Print["Errors: ", 
            N[IntegerPart[Error/Intensities\ 100\ 100]/
                100]]\)\(,\)*) 
Print["Maximum error: ", Max[N[IntegerPart[Error/Intensities\ 100\ 100]/
                100]]]
, 
        Export["/afs/cern.ch/user/a/afabich/public/MERIT/transformer8.dat", 
report2
], 
Export["/afs/cern.ch/user/a/afabich/public/MERIT/runmod/" <> ToString[RunList[\([k, 1]\)]] <> ".dat", 
          N[DataMod]]
}, {k, 105, 
        131}]
\)
Print["++++++"]
Print["The report contains ", Dimensions[
          report2][[1]], " entries for beam runs. Does this \
correspond with the requested evaluation?"]

AllRuns = {};
step = 5;
\(Do[{
t = m, 
data = 
          ReadList["/nfs/cs-ccr-nfs4/data/slopsdata/2007/MD_DATA/CPS_MERIT/MERIT_libera/" <> 
ToString[
                RunList[\([t, 4]\)]] <> "-" <> 
              ToString[RunList[\([t, 5]\)]] <> "-" <> 
              ToString[RunList[\([t, 6]\)]] <> "_" <> 
              ToString[RunList[\([t, 7]\)]] <> "-" <> 
              ToString[RunList[\([t, 8]\)]] <> "-" <> 
              ToString[RunList[\([t, 9]\)]] <> "_MEAS_result.txt", 
            Number], 
data = 
          N[data/Max[data]] + 
            0.2\ \((m - 1)\)/step, 
shift = 
          Last[Last[
              Position[
                Take[data, 
                  950], _?\((# > 0.2 &)\)]]], 
data = 
          Drop[data, 1600 - shift], 
AppendTo[
          AllRuns, 
data
]
}, {m, 1, 100, step}]\)
MultipleListPlot[AllRuns, PlotJoined -> True, 
      PlotStyle -> {RGBColor[0, 0, 0], RGBColor[1, 0, 0], 
          RGBColor[0, 1, 0], RGBColor[0, 0, 1]}, 
      PlotRange -> {{900, 1200}, All}, 
      ImageSize -> 1000]