source: sans/Dev/trunk/NCNR_User_Procedures/Reduction/Polarization/Pol_FlipperPanel.ipf @ 824

Last change on this file since 824 was 824, checked in by srkline, 12 years ago

Changed 2-yukawa to have better names for the parameters, a macro to plot g(r), and to return S(q)=1000 if the calculation fails, to be more obvious.

Added a Corrlation matrix function (in Wrapper.ipf), but it's not linked anywhere yet, since I don't know yet how best to display it.

Simplified the function to list .ASC files

Added comments to the polarization panel procedures.

File size: 24.3 KB
Line 
1#pragma rtGlobals=1             // Use modern global access method.
2
3// input panels to set and calculate polarization parameters necessary for the
4// matrix corrections to the cross sections
5//
6//
7// -3-Flipper efficiency
8
9
10//
11// TODO:
12// X- add a way to manually enter the P values into a "blank" condition, in case that the users
13// calculate the values in a different way. This should be as simple as a dialog to enter values and
14// change the wave note (and displayed strings).
15//
16//
17// Polarization parameters for each condition. Results are stored in a wave note for each condition
18//
19//
20// str = "P_sm_f=2,err_P_sm_f=0,P_sm=0.6,err_P_sm=0,T0=asdf,Cell=asdf,"
21//
22// two waves per condition "Cond_Name_Cell" and "CondCalc_Name_Cell"
23//
24Macro ShowFlipperPanel()
25       
26        // init folders
27        // ASK before initializing cell constants
28        // open the panel
29        DoWindow/F FlipperPanel
30        if(V_flag == 0)
31                InitPolarizationFolders()
32                InitFlipperGlobals()
33                DrawFlipperPanel()
34        endif
35end
36
37Function InitFlipperGlobals()
38
39        SetDataFolder root:Packages:NIST:Polarization:Cells
40       
41        String/G gPsmPf = "Psm*Pf"
42        String/G gPsm = "Psm"
43       
44        SetDataFolder root:
45        return(0)
46End
47
48
49//
50// makes the panel for the calculation of flipper and supermirror efficiencies
51//
52Function DrawFlipperPanel()
53
54        SetDataFolder root:Packages:NIST:Polarization:Cells
55       
56        PauseUpdate; Silent 1           // building window...
57        NewPanel /W=(1023,44,1832,526)/N=FlipperPanel/K=1 as "Flipper and Supermirror"
58        ModifyPanel cbRGB=(1,52428,26586)
59       
60        PopupMenu popup_0,pos={32,18},size={49,20},title="Field Condition",proc=FlipperPanelPopMenuProc
61        PopupMenu popup_0,mode=1,value= #"D_ConditionNameList()"
62       
63        Button button_0,pos={42,310},size={100,20},proc=FlipperAverageButtonProc,title="Do Average"
64       
65        GroupBox group_0,pos={39,350},size={335,103},title="AVERAGED RESULTS",fSize=10
66        GroupBox group_0,fStyle=1
67        SetVariable setvar_0,pos={49,385},size={250,15},title="Sam_depol*Psm*Pf"
68        SetVariable setvar_0,fStyle=1
69        SetVariable setvar_0,limits={0,0,0},value= root:Packages:NIST:Polarization:Cells:gPsmPf
70        SetVariable setvar_1,pos={49,417},size={250,15},title="Sam_depol*Psm",fStyle=1
71        SetVariable setvar_1,limits={0,0,0},value= root:Packages:NIST:Polarization:Cells:gPsm
72//      SetVariable setvar_2,pos={560,518},size={200,13},title="Gamma (h)",fStyle=1
73//      SetVariable setvar_2,limits={0,0,0},barmisc={0,1000}
74//      SetVariable setvar_2,value= root:Packages:NIST:Polarization:Cells:gGamma
75//      SetVariable setvar_3,pos={560,488},size={200,15},title="T0",fStyle=1
76//      SetVariable setvar_3,limits={0,0,0},value= root:Packages:NIST:Polarization:Cells:gT0
77       
78
79        Button button_1,pos={320,17},size={120,20},proc=AddFlipperConditionButton,title="Add Condition"
80        Button button_2,pos={403,295},size={110,20},proc=ClearAllFlipperWavesButton,title="Clear Table"
81        Button button_3,pos={183,310},size={120,20},proc=ShowFlipperCalcButton,title="Show Calc"
82        Button button_4,pos={540,295},size={110,20},proc=ClearFlipperRowButton,title="Clear Row"
83        Button button_5,pos={620,18},size={30,20},proc=FlipperHelpParButtonProc,title="?"
84        Button button_6,pos={488,418},size={100,20},proc=WindowSnapshotButton,title="Snapshot"
85        Button button_7,pos={488,380},size={130,20},proc=ManualEnterPfPsmButton,title="Manual Entry"
86
87        // table
88        Edit/W=(14,55,794,275)/HOST=#
89        ModifyTable format=1,width=0
90        RenameWindow #,T0
91        SetActiveSubwindow ##
92
93        SetDataFolder root:
94        return(0)
95End
96
97Function AddFlipperConditionButton(ba) : ButtonControl
98        STRUCT WMButtonAction &ba
99
100        switch( ba.eventCode )
101                case 2: // mouse up
102                        // click code here
103                       
104                        // get the new name for the condition and the name of the cell used
105                        // the cell must already be defined
106                        String condStr, cellStr
107                        Prompt condStr,"Condition, <12 characters, NO UNDERSCORES"
108                        Prompt cellStr,"Cell",popup,D_CellNameList()
109                        DoPrompt "Add new condition",condStr, cellStr
110                        if(V_Flag==1)
111                                return 0                                                                        // user canceled
112                        endif
113                       
114                        if(strlen(condStr) > 12)
115                                condStr = condStr[0,11]
116                                Print "Condition String trimmed to ",condStr
117                        endif
118                       
119                        condStr = ReplaceString("_", condStr, "", 0, inf)
120                       
121                        String popStr
122                        popStr = condStr+"_"+cellStr
123                       
124                        MakeFlipperResultWaves(popStr)
125                       
126                        break
127                case -1: // control being killed
128                        break
129        endswitch
130
131        return 0
132End
133
134Function FlipperPanelPopMenuProc(pa) : PopupMenuControl
135        STRUCT WMPopupAction &pa
136
137        switch( pa.eventCode )
138                case 2: // mouse up
139                        Variable popNum = pa.popNum
140                        String popStr = pa.popStr
141                       
142                        SetDataFolder root:Packages:NIST:Polarization:Cells
143
144                        // based on the selected string, display the right set of inputs
145//                      Print "now I need to display the right set of waves (2D text?) for ",popStr
146                       
147                        if(cmpstr(popStr,"no conditions defined") == 0)
148                                SetDataFolder root:
149                                return(0)
150                        endif
151                       
152                       
153                        // for the given cell name, if the wave(s) exist, declare them
154                        if(exists(popStr) == 1)
155                                WAVE cond = $(popStr)
156                        else
157                                // if not, report an error                             
158                                DoAlert 0,"The Cond_ waves should exist, this is an error"
159                               
160                                SetDataFolder root:
161                                return(0)
162                                //MakeFlipperResultWaves(popStr)
163                                //WAVE cond = $("root:Packages:NIST:Polarization:Cells:Cond_"+popStr)
164                        endif                   
165                        // append matrix, clearing the old one first
166                        SetDataFolder root:Packages:NIST:Polarization:Cells
167
168                        KillWindow FlipperPanel#T0
169                        Edit/W=(14,55,794,275)/HOST=FlipperPanel
170                        RenameWindow #,T0
171                        AppendtoTable/W=FlipperPanel#T0 cond.ld                 //show the labels
172                        ModifyTable width(Point)=0
173                        ModifyTable width(cond.l)=20
174                       
175                        SetActiveSubwindow ##
176       
177                        SetDataFolder root:
178                       
179                        break
180                case -1: // control being killed
181                        break
182        endswitch
183
184        return 0
185End
186
187// waves are:
188// "Cond_"+popStr
189// and "CondCalc_"+popStr
190
191Function MakeFlipperResultWaves(popStr)
192        String popStr
193
194        SetDataFolder root:Packages:NIST:Polarization:Cells
195
196        Make/O/D/N=(1,8) $("Cond_"+popStr)
197        WAVE cond = $("Cond_"+popStr)
198        // set the column labels
199        SetDimLabel 1,0,UU_Trans,cond
200        SetDimLabel 1,1,DU_Trans,cond
201        SetDimLabel 1,2,DD_Trans,cond
202        SetDimLabel 1,3,UD_Trans,cond
203        SetDimLabel 1,4,Blocked,cond
204        SetDimLabel 1,5,Pol_SM_FL,cond
205        SetDimLabel 1,6,Pol_SM,cond                     //for a mask wave, non-zero is used in the fit
206        SetDimLabel 1,7,Include,cond
207        cond[0][7] = 1                  //default to include the point
208       
209        // generate the dummy wave note now, change as needed
210        String cellStr = StringFromList(1, popStr,"_")
211        String testStr = "P_sm_f=2,err_P_sm_f=0,P_sm=0.6,err_P_sm=0,T0=asdf,Cell=asdf,"
212        testStr = ReplaceStringByKey("Cell", testStr, cellStr ,"=", ",", 0)
213        Note cond, testStr
214
215        // to hold the results of the calculation
216        Make/O/D/N=(1,14) $("CondCalc_"+popStr)
217        WAVE CondCalc = $("CondCalc_"+popStr)
218        SetDimLabel 1,0,CR_UU,CondCalc
219        SetDimLabel 1,1,err_CR_UU,CondCalc
220        SetDimLabel 1,2,CR_DU,CondCalc
221        SetDimLabel 1,3,err_CR_DU,CondCalc
222        SetDimLabel 1,4,CR_DD,CondCalc
223        SetDimLabel 1,5,err_CR_DD,CondCalc
224        SetDimLabel 1,6,CR_UD,CondCalc
225        SetDimLabel 1,7,err_CR_UD,CondCalc
226        SetDimLabel 1,8,CR_Blocked,CondCalc
227        SetDimLabel 1,9,err_CR_Blocked,CondCalc
228        SetDimLabel 1,10,P_sm_f,CondCalc
229        SetDimLabel 1,11,err_P_sm_f,CondCalc
230        SetDimLabel 1,12,P_sm,CondCalc
231        SetDimLabel 1,13,err_P_sm,CondCalc     
232
233        SetDataFolder root:
234
235        return(0)
236End
237
238
239// allows manual entry of Psm and Pf values
240//
241Function ManualEnterPfPsmButton(ba) : ButtonControl
242        STRUCT WMButtonAction &ba
243
244        Variable selRow,err=0
245        String fname, t0str, condStr,noteStr,t1Str,cellStr
246
247        switch( ba.eventCode )
248                case 2: // mouse up
249                        // click code here
250                        Variable cr1,cr2,cr3,cr4,cr5,err_cr1,err_cr2,err_cr3,err_cr4,err_cr5
251                        Variable PsmPf, err_PsmPf, Psm, err_Psm
252                               
253                        ControlInfo/W=FlipperPanel popup_0
254                        condStr = S_Value
255                        WAVE w=$("root:Packages:NIST:Polarization:Cells:"+condStr)              //the one that is displayed
256                        WAVE calc=$("root:Packages:NIST:Polarization:Cells:CondCalc_"+condStr[5,strlen(condStr)-1])             //the one that holds results
257                       
258
259                        Prompt PsmPf, "Enter PsmPf: "           
260                        Prompt err_PsmPf, "Enter err_PsmPf: "           
261                        Prompt Psm, "Enter Psm: "               
262                        Prompt err_Psm, "Enter err_Psm: "               
263                        DoPrompt "Enter Supermirror and Flipper Parameters", PsmPf, err_PsmPf, Psm, err_Psm
264                        if (V_Flag)
265                                return -1                                                               // User canceled
266                        endif
267                       
268//      this is the format of the note that is attached to the "Cond_" wave             
269//      String testStr = "P_sm_f=2,err_P_sm_f=0,P_sm=0.6,err_P_sm=0,T0=asdf,Cell=asdf,"
270// the "Cell" value was filled in when the Condition was created
271       
272       
273// Put the average values into the wave note and display on the panel
274                        noteStr = note(w)
275                        noteStr = ReplaceNumberByKey("P_sm_f", noteStr, PsmPf ,"=", ",", 0)
276                        noteStr = ReplaceNumberByKey("P_sm", noteStr, Psm ,"=", ",", 0)
277                        noteStr = ReplaceNumberByKey("err_P_sm_f", noteStr, err_PsmPf ,"=", ",", 0)
278                        noteStr = ReplaceNumberByKey("err_P_sm", noteStr, err_Psm ,"=", ",", 0)
279                       
280                        // replace the string
281                        Note/K w
282                        Note w, noteStr
283                                       
284                        //update the global values for display 
285                        SVAR gPsmPf = root:Packages:NIST:Polarization:Cells:gPsmPf
286                        SVAR gPsm = root:Packages:NIST:Polarization:Cells:gPsm
287                        sprintf gPsmPf, "%g +/- %g",PsmPf,err_PsmPf
288                        sprintf gPsm, "%g +/- %g",Psm,err_Psm
289                       
290                        break
291                case -1: // control being killed
292                        break
293        endswitch
294
295        return 0
296End
297
298
299
300
301// just recalculate everything, every time
302//
303Function FlipperAverageButtonProc(ba) : ButtonControl
304        STRUCT WMButtonAction &ba
305
306        Variable selRow,err=0
307        String fname, t0str, condStr,noteStr,t1Str,cellStr
308
309        switch( ba.eventCode )
310                case 2: // mouse up
311                        // click code here
312                        Variable cr1,cr2,cr3,cr4,cr5,err_cr1,err_cr2,err_cr3,err_cr4,err_cr5
313                        Variable PsmPf, err_PsmPf, Psm, err_Psm
314                               
315                        ControlInfo/W=FlipperPanel popup_0
316                        condStr = S_Value
317                        WAVE w=$("root:Packages:NIST:Polarization:Cells:"+condStr)              //the one that is displayed
318                        WAVE calc=$("root:Packages:NIST:Polarization:Cells:CondCalc_"+condStr[5,strlen(condStr)-1])             //the one that holds results
319                       
320                        Variable numRows,ncalc,diff
321                        numRows = DimSize(w,0)          //rows in the displayed table
322                        ncalc = DimSize(calc,0)
323                       
324                        // add rows to the ConcCalc_ matrix as needed
325                        if(numRows != ncalc)
326                                if(ncalc > numRows)
327                                        DoAlert 0,"The DecayCalc_ is larger than displayed. Seek help."
328                                        err = 1
329                                        return(err)
330                                else
331                                        diff = numRows - ncalc
332                                        InsertPoints/M=0 ncalc, diff, calc
333                                endif
334                        endif
335                       
336                        noteStr=note(w)
337                        cellStr = StringByKey("Cell", noteStr, "=", ",", 0)
338                        Wave decay = $("root:Packages:NIST:Polarization:Cells:Decay_"+cellStr) 
339                        noteStr=note(decay)
340                        t0Str = StringByKey("T0", noteStr, "=", ",", 0)
341//                      Print "CellStr, T0 = ",cellStr, t0Str
342
343                        Variable sum_PsmPf, err_sum_PsmPf, sum_Psm, err_sum_Psm
344                        sum_PsmPf = 0
345                        err_sum_PsmPf = 0
346                        sum_Psm = 0
347                        err_sum_Psm = 0
348                       
349                        for(selRow=0;selRow<numRows;selRow+=1)
350                                Print "calculate the row ",selRow
351
352                                // parse the rows, report errors (there, not here), exit if any found
353                                err = ParseFlipperRow(w,selRow)
354                                if(err)
355                                        return 0
356                                endif
357                               
358                                // do the calculations:
359       
360                                Print "The Blocked CR is not rescaled to zero attenuators"
361                                cr1 = TotalCR_FromRun(w[selRow][%UU_Trans],err_cr1,0)
362                                cr2 = TotalCR_FromRun(w[selRow][%DU_Trans],err_cr2,0)
363                                cr3 = TotalCR_FromRun(w[selRow][%DD_Trans],err_cr3,0)   
364                                cr4 = TotalCR_FromRun(w[selRow][%UD_Trans],err_cr4,0)
365                                cr5 = TotalCR_FromRun(w[selRow][%Blocked],err_cr5,1)            //blocked beam is NOT normalized to zero attenuators
366
367                                calc[selRow][%cr_UU] = cr1
368                                calc[selRow][%cr_DU] = cr2
369                                calc[selRow][%cr_DD] = cr3
370                                calc[selRow][%cr_UD] = cr4
371                                calc[selRow][%cr_Blocked] = cr5
372                                calc[selRow][%err_cr_UU] = err_cr1
373                                calc[selRow][%err_cr_DU] = err_cr2
374                                calc[selRow][%err_cr_DD] = err_cr3
375                                calc[selRow][%err_cr_UD] = err_cr4
376                                calc[selRow][%err_cr_Blocked] = err_cr5
377       
378                                // Calc PsmPf, and assign the values
379                                PsmPf = Calc_PsmPf(w,calc,noteStr,selRow,err_PsmPf)
380                                calc[selRow][%P_sm_f] = PsmPf
381                                calc[selRow][%err_P_sm_f] = err_PsmPf
382                                w[selRow][%Pol_SM_FL] = PsmPf
383                               
384                                // Calc Psm, and assign the values
385                                Psm = Calc_Psm(w,calc,noteStr,selRow,err_Psm)
386                                calc[selRow][%P_sm] = Psm
387                                calc[selRow][%err_P_sm] = err_Psm
388                                w[selRow][%Pol_SM] = Psm
389
390                                // running average of PsmPf and Psm
391                                sum_PsmPf += PsmPf
392                                err_sum_PsmPf += err_PsmPf^2
393                                sum_Psm += Psm
394                                err_sum_Psm += err_Psm^2
395                               
396                        endfor          //loop over rows
397                       
398                        // now get a running average of muP, Po, and the errors
399                        PsmPf = sum_PsmPf/numRows
400                        Psm = sum_Psm/numRows
401                        err_PsmPf = sqrt(err_sum_PsmPf) / numRows
402                        err_Psm = sqrt(err_sum_Psm) / numRows
403                       
404//      this is the format of the note that is attached to the "Cond_" wave             
405//      String testStr = "P_sm_f=2,err_P_sm_f=0,P_sm=0.6,err_P_sm=0,T0=asdf,Cell=asdf,"
406// the "Cell" value was filled in when the Condition was created
407       
408       
409// Put the average values into the wave note and display on the panel
410                        noteStr = note(w)
411                        noteStr = ReplaceNumberByKey("P_sm_f", noteStr, PsmPf ,"=", ",", 0)
412                        noteStr = ReplaceNumberByKey("P_sm", noteStr, Psm ,"=", ",", 0)
413                        noteStr = ReplaceNumberByKey("err_P_sm_f", noteStr, err_PsmPf ,"=", ",", 0)
414                        noteStr = ReplaceNumberByKey("err_P_sm", noteStr, err_Psm ,"=", ",", 0)
415                       
416                        // replace the string
417                        Note/K w
418                        Note w, noteStr
419                                       
420                        //update the global values for display 
421                        SVAR gPsmPf = root:Packages:NIST:Polarization:Cells:gPsmPf
422                        SVAR gPsm = root:Packages:NIST:Polarization:Cells:gPsm
423                        sprintf gPsmPf, "%g +/- %g",PsmPf,err_PsmPf
424                        sprintf gPsm, "%g +/- %g",Psm,err_Psm
425                       
426                        break
427                case -1: // control being killed
428                        break
429        endswitch
430
431        return 0
432End
433
434// gCellKW passed in has gamma, muPo, etc. needed for PCell(t) calculation
435//
436// these are not the equations that Kaythrn gave to me, but rather
437// equations 14 and 15 from the SS handout. These replacement equations
438// are in terms of relative transmissions, so I can use count rates
439// otherwise, I don't know how to calculate a transmission -- I don't know what
440// the "empty beam" condition is...
441//
442//  this is only using the measurement at t1 for the calculation!
443//
444Function Calc_PsmPf(w,calc,gCellKW,selRow,err_PsmPf)
445        WAVE w,calc
446        String gCellKW
447        Variable selRow,&err_PsmPf
448       
449        // DD is cr3, DU is cr2, Blocked is cr5
450        String t0Str,t1Str,t2Str,fname
451        Variable PsmPf,t1,t2,PCell_t1,PCell_t2,err_PCell_t1,err_PCell_t2
452        Variable muPo,err_muPo,gam,err_gam
453        Variable crDD, crDU,err_crDD,err_crDU,crBB, err_crBB
454       
455        t0Str = StringByKey("T0", gCellKW, "=", ",", 0)
456        muPo = NumberByKey("muP", gCellKW, "=", ",", 0)
457        err_muPo = NumberByKey("err_muP", gCellKW, "=", ",", 0)
458        gam = NumberByKey("gamma", gCellKW, "=", ",", 0)
459        err_gam = NumberByKey("err_gamma", gCellKW, "=", ",", 0)
460
461        fname = FindFileFromRunNumber(w[selRow][%UU_Trans])
462        t1str = getFileCreationDate(fname)
463        t1 = ElapsedHours(t0Str,t1Str)
464       
465        fname = FindFileFromRunNumber(w[selRow][%DU_Trans])
466        t2str = getFileCreationDate(fname)
467        t2 = ElapsedHours(t0Str,t2Str)
468
469        PCell_t1 = Calc_PCell_atT(muPo,err_muPo,gam,err_gam,t1,err_PCell_t1)
470        PCell_t2 = Calc_PCell_atT(muPo,err_muPo,gam,err_gam,t2,err_PCell_t2)
471       
472        // DD is cr3, DU is cr2, Blocked is cr5
473        crDD = calc[selRow][%cr_DD]
474        crDU = calc[selRow][%cr_DU]
475        crBB = calc[selRow][%cr_Blocked]
476        err_crDD = calc[selRow][%err_cr_DD]
477        err_crDU = calc[selRow][%err_cr_DU]
478        err_crBB = calc[selRow][%err_cr_Blocked]
479       
480        // this really needs transmissions
481//      PsmPf = (crDD - crDU)/(PCell_t1 + PCell_t2)
482       
483        // eqn (15) from the SS handout
484        Variable tmp,dfdx
485        tmp = (crDD - crBB)/(crDU - crBB)
486       
487        PsmPf = (tmp - 1)/(Pcell_t1*(1+tmp))
488       
489        dfdx = 1/(Pcell_t1*(1+tmp)) - (tmp-1)*Pcell_t1/(Pcell_t1^2*(1+tmp)^2)
490       
491        err_PsmPf = ( (tmp-1)/((1+tmp)*Pcell_t1^2) *err_Pcell_t1 )^2
492        err_PsmPf += ( dfdx / (crDU-crBB) * err_crDD)^2
493        err_PsmPf += ( dfdx*(crDD-crBB)/(crDU-crBB)^2 * err_crDU)^2
494        err_PsmPf += ( dfdx*(-tmp/(crDD-crBB) + tmp/(crDU-crBB)) * err_crBB)^2
495       
496        err_PsmPf = sqrt(err_PsmPf)
497        Printf "At t1=%g  PsmPf = %g +/- %g (%g%)\r",t1,PsmPf,err_PsmPf,err_PsmPf/PsmPf*100
498
499
500        return(PsmPf)
501end
502
503
504// gCellKW passed in has gamma, muPo, etc. needed for PCell(t) calculation
505//
506// these are not the equations that Kaythrn gave to me, but rather
507// equations 14 and 15 from the SS handout. These replacement equations
508// are in terms of relative transmissions, so I can use count rates
509// otherwise, I don't know how to calculate a transmission -- I don't know what
510// the "empty beam" condition is...
511//
512//  this is only using the measurement at t1 for the calculation!
513//
514Function Calc_Psm(w,calc,gCellKW,selRow,err_Psm)
515        WAVE w,calc
516        String gCellKW
517        Variable selRow,&err_Psm
518       
519        // UU is cr1, UD is cr4, Blocked is cr5
520        String t0Str,t1Str,t2Str,fname
521        Variable Psm,t1,t2,PCell_t1,PCell_t2,err_PCell_t1,err_PCell_t2
522        Variable muPo,err_muPo,gam,err_gam
523        Variable crUU, crUD,err_crUU,err_crUD,crBB, err_crBB
524       
525        t0Str = StringByKey("T0", gCellKW, "=", ",", 0)
526        muPo = NumberByKey("muP", gCellKW, "=", ",", 0)
527        err_muPo = NumberByKey("err_muP", gCellKW, "=", ",", 0)
528        gam = NumberByKey("gamma", gCellKW, "=", ",", 0)
529        err_gam = NumberByKey("err_gamma", gCellKW, "=", ",", 0)
530
531        fname = FindFileFromRunNumber(w[selRow][%UU_Trans])
532        t1str = getFileCreationDate(fname)
533        t1 = ElapsedHours(t0Str,t1Str)
534       
535        fname = FindFileFromRunNumber(w[selRow][%DU_Trans])
536        t2str = getFileCreationDate(fname)
537        t2 = ElapsedHours(t0Str,t2Str)
538
539        PCell_t1 = Calc_PCell_atT(muPo,err_muPo,gam,err_gam,t1,err_PCell_t1)
540        PCell_t2 = Calc_PCell_atT(muPo,err_muPo,gam,err_gam,t2,err_PCell_t2)
541       
542        // UU is cr1, UD is cr4, Blocked is cr5
543        crUU = calc[selRow][%cr_UU]
544        crUD = calc[selRow][%cr_UD]
545        crBB = calc[selRow][%cr_Blocked]
546        err_crUU = calc[selRow][%err_cr_UU]
547        err_crUD = calc[selRow][%err_cr_UD]
548        err_crBB = calc[selRow][%err_cr_Blocked]
549       
550        // this really needs transmissions
551       
552        // eqn (14) from the SS handout
553        Variable tmp,dfdx
554        tmp = (crUU - crBB)/(crUD - crBB)
555       
556        Psm = (tmp - 1)/(Pcell_t1*(1+tmp))
557       
558        dfdx = 1/(Pcell_t1*(1+tmp)) - (tmp-1)*Pcell_t1/(Pcell_t1^2*(1+tmp)^2)
559       
560        err_Psm = ( (tmp-1)/((1+tmp)*Pcell_t1^2) *err_Pcell_t1 )^2
561        err_Psm += ( dfdx / (crUD-crBB) * err_crUU)^2
562        err_Psm += ( dfdx*(crUU-crBB)/(crUD-crBB)^2 * err_crUD)^2
563        err_Psm += ( dfdx*(-tmp/(crUU-crBB) + tmp/(crUD-crBB)) * err_crBB)^2
564       
565        err_Psm = sqrt(err_Psm)
566        Printf "At t1=%g  Psm = %g +/- %g (%g%)\r",t1,Psm,err_Psm,err_Psm/Psm*100
567
568
569        return(Psm)
570end
571
572
573
574// t2 is in hours, muP0 is at t0
575//
576Function Calc_PCell_atT(muPo,err_muPo,gam,err_gam,t2,err_PCell)
577        Variable muPo,err_muPo,gam,err_gam,t2,&err_PCell
578
579
580        Variable Pcell
581
582
583        PCell = tanh(muPo * exp(-t2/gam))
584       
585        Variable arg,tmp2
586        arg = PCell
587        tmp2 = (1-tanh(arg)^2)^2
588        err_PCell = tmp2 * (exp(-t2/gam) * err_muPo)^2                  //dominant term (10x larger)
589        err_PCell += tmp2 * (arg*t2/gam/gam * err_gam)^2
590       
591        err_PCell = sqrt(err_Pcell)
592       
593        Printf "At t=%g  Pcell = %g +/- %g (%g%)\r",t2,Pcell,err_Pcell,err_Pcell/PCell*100
594
595
596
597        return(PCell)
598End
599
600// bring up a table with the calculation results
601Function ShowFlipperCalcButton(ba) : ButtonControl
602        STRUCT WMButtonAction &ba
603
604        switch( ba.eventCode )
605                case 2: // mouse up
606                        // click code here
607                        ControlInfo/W=FlipperPanel popup_0
608                        String condStr = S_Value
609                        condStr = condStr[5,strlen(condStr)-1]          // trim off "Calc_" from the beginning of the string
610                        WAVE calc=$("root:Packages:NIST:Polarization:Cells:CondCalc_"+condStr)         
611                        edit calc.ld
612                                               
613                        break
614                case -1: // control being killed
615                        break
616        endswitch
617
618        return 0
619End
620
621
622
623// clear just the row
624//
625Function ClearFlipperRowButton(ba) : ButtonControl
626        STRUCT WMButtonAction &ba
627
628        String popStr=""
629        Variable selRow
630       
631        switch( ba.eventCode )
632                case 2: // mouse up
633                        // click code here
634                        DoAlert 1,"Clear the selected row?"
635                        if(V_flag !=1)
636                                return(0)
637                        endif
638                       
639                        SetDataFolder root:Packages:NIST:Polarization:Cells
640
641                        ControlInfo/W=FlipperPanel popup_0
642                        popStr = S_Value
643                       
644                        Wave decay = $("Decay_"+popStr)
645                        Wave calc = $("DecayCalc_"+popStr)
646
647                        // Delete just those points
648                                               
649                        GetSelection table, FlipperPanel#T0, 1
650                        selRow = V_startRow
651                        DeletePoints selRow,1,decay,calc                       
652                       
653                        // clear the graph and the results                     
654                        NVAR gMuPo = root:Packages:NIST:Polarization:Cells:gMuPo
655                        NVAR gPo  = root:Packages:NIST:Polarization:Cells:gPo
656                        NVAR gGamma  = root:Packages:NIST:Polarization:Cells:gGamma
657                        SVAR gT0  = root:Packages:NIST:Polarization:Cells:gT0
658                        gMuPo = 0
659                        gPo = 0
660                        gGamma = 0
661                        gT0 = "recalculate"
662                       
663                       
664                        SetDataFolder root:
665                        break
666                case -1: // control being killed
667                        break
668        endswitch
669
670        return 0
671End
672
673
674
675// for this, do I want to clear everything, or just a selected row??
676//
677//
678Function ClearAllFlipperWavesButton(ba) : ButtonControl
679        STRUCT WMButtonAction &ba
680
681        String popStr=""
682       
683        switch( ba.eventCode )
684                case 2: // mouse up
685                        // click code here
686                        DoAlert 1,"Clear all of the decay waves for the selected cell?"
687                        if(V_flag !=1)
688                                return(0)
689                        endif
690                       
691                        SetDataFolder root:Packages:NIST:Polarization:Cells
692
693                        ControlInfo/W=FlipperPanel popup_0
694                        popStr = S_Value
695                       
696                        Wave decay = $("Decay_"+popStr)
697                        Wave calc = $("DecayCalc_"+popStr)
698                       
699//                      re-initialize the decay waves, so it appears as a blank, initialized table
700
701                        MakeFlipperResultWaves(popStr)
702                        decay = 0
703                        calc = 0
704       
705                        // clear the graph and the results?     
706                       
707                       
708                                       
709                        NVAR gMuPo = root:Packages:NIST:Polarization:Cells:gMuPo
710                        NVAR gPo  = root:Packages:NIST:Polarization:Cells:gPo
711                        NVAR gGamma  = root:Packages:NIST:Polarization:Cells:gGamma
712                        SVAR gT0  = root:Packages:NIST:Polarization:Cells:gT0
713                        gMuPo = 0
714                        gPo = 0
715                        gGamma = 0
716                        gT0 = "recalculate"
717                       
718                       
719                        SetDataFolder root:
720                        break
721                case -1: // control being killed
722                        break
723        endswitch
724
725        return 0
726End
727
728
729Function FlipperHelpParButtonProc(ba) : ButtonControl
730        STRUCT WMButtonAction &ba
731
732        switch( ba.eventCode )
733                case 2: // mouse up
734                        // click code here
735                        DoAlert 0,"Help for Flipper Panel not written yet"
736                        break
737                case -1: // control being killed
738                        break
739        endswitch
740
741        return 0
742End
743
744
745
746// null condition is not right. if the loop fails, then the
747// retStr will be ";;;;", not zero length. What's the proper test?
748// Does it matter? the list of default gCell_sss should already be there.
749//
750Function/S D_ConditionNameList()
751
752        String listStr=""
753       
754        SetDataFolder root:Packages:NIST:Polarization:Cells
755
756        // get a list of the Condition waves
757        listStr=WaveList("Cond_*",";","")
758//      print listStr
759       
760        if(strlen(listStr) == 0)
761                listStr = "no conditions defined;"
762        endif
763       
764        SetDataFolder root:             
765        return(listStr)
766End
767
768
769// parse the row to be sure that:
770//
771// - files are valid numbers
772// - files are all at same SDD
773// - files are all with same attenuation (just print a warning to cmd)
774// - files all use the same cell
775// - files are all within 20 minutes of each other
776//
777//
778//      SetDimLabel 1,0,UU_Trans,cond
779//      SetDimLabel 1,1,DU_Trans,cond
780//      SetDimLabel 1,2,DD_Trans,cond
781//      SetDimLabel 1,3,UD_Trans,cond
782//      SetDimLabel 1,4,Blocked,cond
783//      SetDimLabel 1,5,Pol_SM_FL,cond
784//      SetDimLabel 1,6,Pol_SM,cond                     //for a mask wave, non-zero is used in the fit
785//      SetDimLabel 1,7,Include,cond
786//
787// There are 5 separate files now
788//
789Function ParseFlipperRow(w,selRow)
790        Wave w
791        Variable selRow
792       
793        Variable err=0
794        Variable atten1,atten2,atten3,atten4,atten5
795        Variable sdd1,sdd2,sdd3,sdd4,sdd5
796        Variable t1,t2,t3,t4,t5
797        String cell1,cell2,cell3,cell4,cell5
798       
799       
800        String fname=""
801       
802       
803        // are all file numbers valid?
804        fname = FindFileFromRunNumber(w[selRow][%UU_Trans])
805        if(cmpstr(fname,"")==0)
806                DoAlert 0,"UU_Trans run "+num2str(w[selRow][%UU_Trans])+" is not a valid run number"
807                err = 1
808        else
809                atten1 = getAttenNumber(fname)
810                sdd1 = getSDD(fname)
811        endif
812       
813        fname = FindFileFromRunNumber(w[selRow][%DU_Trans])
814        if(cmpstr(fname,"")==0)
815                DoAlert 0,"DU_Trans run "+num2str(w[selRow][%DU_Trans])+" is not a valid run number"
816                err = 1
817        else
818                atten2 = getAttenNumber(fname)
819                sdd2 = getSDD(fname)
820        endif
821       
822        fname = FindFileFromRunNumber(w[selRow][%DD_Trans])
823        if(cmpstr(fname,"")==0)
824                DoAlert 0,"DD_Trans run "+num2str(w[selRow][%DD_Trans])+" is not a valid run number"
825                err = 1
826        else
827                atten3 = getAttenNumber(fname)
828                sdd3 = getSDD(fname)
829        endif
830       
831        fname = FindFileFromRunNumber(w[selRow][%UD_Trans])
832        if(cmpstr(fname,"")==0)
833                DoAlert 0,"UD_Trans run "+num2str(w[selRow][%UD_Trans])+" is not a valid run number"
834                err = 1
835        else
836                atten4 = getAttenNumber(fname)
837                sdd4 = getSDD(fname)
838        endif
839       
840        fname = FindFileFromRunNumber(w[selRow][%Blocked])
841        if(cmpstr(fname,"")==0)
842                DoAlert 0,"Blocked run "+num2str(w[selRow][%Blocked])+" is not a valid run number"
843                err = 1
844        else
845                atten5 = getAttenNumber(fname)
846                sdd5 = getSDD(fname)
847        endif
848       
849       
850       
851        // do a check of the elapsed time from start to finish
852       
853       
854       
855       
856        if( (sdd1 != sdd2) || (sdd2 != sdd3) || (sdd1 != sdd3) )
857                DoAlert 0,"Files in row "+num2str(selRow)+" are not all at the same detector distance"
858                err = 1
859        endif
860       
861       
862       
863       
864       
865        if( (atten1 != atten2) || (atten2 != atten3) || (atten1 != atten3) )
866                DoAlert 0,"Files in row "+num2str(selRow)+" are not all collected with the same attenuation. Just so you know."
867                err = 0
868        endif
869       
870        return(err)
871end
872
873
874////////////////////////////////////////////
Note: See TracBrowser for help on using the repository browser.