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

Last change on this file since 835 was 835, checked in by srkline, 11 years ago

moving the Polarization routines to a temporary "beta" menu location on the macros menu, numbering the panels.

Created a loader for the polarization routines that will load the SANS reduction if needed.

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