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

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

minor changes + typo in Model Docs.

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