source: sans/Dev/trunk/NCNR_User_Procedures/Reduction/Polarization/Pol_PolarizationPanels.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: 38.7 KB
Line 
1#pragma rtGlobals=1             // Use modern global access method.
2
3
4// TODO:
5// - on the decay panel. need to be able to manually enter a date that is to or an offset
6//              number of hours. currently it takes the first file as t=0, which is often not correct
7//
8
9
10// Polarized Beam Reduction Procedures
11//
12//
13// input panels to set and calculate polarization parameters necessary for the
14// matrix corrections to the cross sections
15//
16// -1- Fundamental Cell Parameters -- these are constants, generally not editable. (this file)
17// -2- Decay Parameters -- these are fitted values based on transmission mearurements (this file)
18// -3- Flipper Panel is in its own procedure (FlipperPanel.ipf)
19// -4- PolCor_Panel is in PolarizationCorrection.ipf
20
21//
22// Driven by 4 panels to get the necessary information from the users
23// -1- Fundamental cell parameters: root:Packages:NIST:Polarization:Cells
24//              - default cell parameters are loaded. More cell definitions can be added as more cells are used
25//              - changes are saved per experiment
26//              - important parameters are held in global key=value strings gCell_<name>
27//              - cell names and parameters are used by the 2nd panel for calculation of the Decay constant
28//
29// -2- Decay Constant Panel
30//              - decay constant determined for each cell.
31//              - popping the cell name makes 2 waves, Decay_<cellname> and DecayCalc_<cellname>
32//              - Decay_ is the wave presented in the table. Specified transmission run numbers are entered
33//                      and "Calc Sel Row" does the calculation of mu and Pcell (for all rows, actually)
34//              - DimLabels are used for the arrays to make the column identity more readable than simply an index
35//              - time=0 is taken from the first file
36//              - Calculations are based on the count rate of the file, corrected for monitor and attenuation
37//              - alerts are posted for files in any row that are not at the same attenuation or SDD
38//              - if "include" column is == 1, that row will be included in the fit of the decay data
39//              - excluded points are plotted in red
40//              - results of the fit are printed on the panel, and written to the wave note of the Decay_ wave
41//                      (not DecayCalc_) for use in the next panel
42//              - manual entry of all of the parameters in the wave note is allowed.
43//
44//
45// -3- Flipper Panel (not in this procedure - in Pol_FlipperPanel.ipf)
46//              - calculates the flipper and supermirror efficiencies for a given "condition"
47//              - this "condition" may have contributions from multiple cells
48//              - start by entering a condition name
49//              - Waves Cond_<condition> and CondCalc_<condition>, and CondCell are created
50//              - DimLabels are used for the arrays to make the column identity more readable than simply an index
51//              - Enter the name of the cell in the first column (the cell must be defined and decay calculated)
52//              - enter transmission run numbers as specified in the table
53//              - Do Average will calculate the Psm and PsmPfl values (and errors) and average if more than
54//                      one row of data is present (and included)
55//              - results are printed on the panel, and written to the wave note of Cond_<condition>
56//              - results are used in the calculation of the polarization matrix
57//              - (the cell name is not entered using a contextual menu, since this is difficult for a subwindow)
58//              - (the wave note no longer needs the cell name)
59//
60// -4- PolCor_Panel (not in this procedure - in Pol_PolarizationCorrection.ipf)
61//              - gets all of the parameters from the user to do the polariztion correction, then the "normal" SANS reduction
62//              - up to 10 files can be added together for each of the different spin states (more than this??)
63//              - one polarization condition is set for everything with the popup @ the top
64//              - two-column list boxes for each state hold the run number and the cell name
65//              - the same list boxes are duplicated (hidden) for the SAM/EMP/BGD tabs as needed
66//              - on loading of the data, the 2-letter spin state is tagged onto the loaded waves (all of them)
67//              - displayed data is simply re-pointed to the desired data
68//              - on loading, the raw files are added together as ususal, normalized to monitor counts. Then each contribution
69//                      of the file to the polarization matrix is added (scaling each by mon/1e8)
70//              - loaded data and PolMatrix are stored in the ususal SAM, EMP, BGD folders.
71//              - Polarization correction is done with one click (one per tab). "_pc" tags are added to the resulting names,
72//                      and copies of all of the associated waves are again copied (wasteful), but makes switching display very easy
73//              - Once all of the polariztion correction is done, then the UU_pc (etc.) data can be reduced as usual (xx_pc = 4 passes)
74//              - protocol is built as ususal, from this panel only (since the SAM, EMP, and BGD need to be switched, rather than loaded
75//              - protocols can be saved/recalled.
76//              - reduction will always ask for a protocol rather than using what's on the panel.
77//              - closing the panel will save the state (except the protocol). NOT initializing when re-opening will restore the
78//                      state of the entered runs and the popups of conditions.
79//
80//
81//
82//
83
84
85
86
87//
88// Panel -1-
89//
90// Fundamental He cell parameters. Most of these are pre-defined, so that they are supplied as a
91// static table, only edited as parameters are refined.
92//
93// work with this as kwString
94// cell=nameStr
95// lambda=num
96// Te=num
97// err_Te=num
98// mu=nnum
99// err_mu=num
100//
101//
102// for this panel, the cell parameters are stored as kw strings
103// all of the strings start w/ "gCell_"
104//
105Macro ShowCellParamPanel()
106       
107        // init folders
108        // ASK before initializing cell constants
109        // open the panel
110        DoWindow/F CellParamPanel
111        if(V_flag == 0)
112                InitPolarizationFolders()
113                DoAlert 1,"Do you want to use default parameters?"
114                if(V_flag == 1)
115                        InitPolarizationGlobals()
116                endif
117                Make_HeCell_ParamWaves()
118                DrawCellParamPanel()
119        endif
120
121end
122
123// setup the data folder, etc
124//
125//
126Function InitPolarizationFolders()
127
128        NewDataFolder/O root:Packages:NIST:Polarization
129        NewDataFolder/O root:Packages:NIST:Polarization:Cells                   //holds the cell constants and waves
130       
131        SetDataFolder root:
132        return(0)
133End
134
135//
136// add more cells here as they are defined
137//
138Function InitPolarizationGlobals()
139
140        SetDataFolder root:Packages:NIST:Polarization:Cells
141       
142        // cell constants
143        String/G gCell_Maverick = "cell=Maverick,lambda=5.0,Te=0.87,err_Te=0.01,mu=3.184,err_mu=0.2,"
144        String/G gCell_Burgundy = "cell=Burgundy,lambda=5.0,Te=0.86,err_Te=0.01,mu=3.138,err_mu=0.15,"
145        String/G gCell_Olaf = "cell=Olaf,lambda=7.5,Te=0.86,err_Te=0.005,mu=2.97,err_mu=0.18,"
146       
147       
148        SetDataFolder root:
149        return(0)
150End
151
152
153// parse strings to fill in waves
154//
155//
156Function Make_HeCell_ParamWaves()
157
158        SetDataFolder root:Packages:NIST:Polarization:Cells
159       
160        String listStr,item
161        Variable num,ii
162       
163        // get a list of the strings
164        listStr=StringList("gCell_*",";")
165        num=ItemsInList(listStr,";")
166        print listStr
167       
168        Make/O/T/N=0 CellName
169        Make/O/N=0 lambda,Te,err_Te,mu,err_mu
170       
171        // parse the strings to fill the table
172        for(ii=0;ii<num;ii+=1)
173                item = StringFromList(ii, listStr,";")
174                SVAR gStr = $item
175                InsertPoints  ii, 1, CellName,lambda,Te,err_Te,mu,err_mu
176                CellName[ii] = StringByKey("cell", gStr, "=", ",", 0)
177                lambda[ii] = NumberByKey("lambda", gStr, "=", ",", 0)
178                Te[ii] = NumberByKey("Te", gStr, "=", ",", 0)
179                err_Te[ii] = NumberByKey("err_Te", gStr, "=", ",", 0)
180                mu[ii] = NumberByKey("mu", gStr, "=", ",", 0)
181                err_mu[ii] = NumberByKey("err_mu", gStr, "=", ",", 0)
182               
183        endfor
184
185       
186        SetDataFolder root:
187        return(0)
188End
189
190// take the waves from the table and write these back to the string, only for the current experiment
191//
192Function Save_HeCell_ParamWaves()
193
194        SetDataFolder root:Packages:NIST:Polarization:Cells
195       
196        String listStr,item,dummyStr
197        Variable num,ii
198       
199        // get a list of the strings
200        listStr=StringList("gCell_*",";")
201        num=ItemsInList(listStr,";")
202        KillStrings/Z listStr
203       
204        Wave/T CellName
205        Wave lambda,Te,err_Te,mu,err_mu
206       
207        dummyStr = "cell=Maverick,lambda=5.0,Te=0.87,err_Te=0.01,mu=3.184,err_mu=0.2,"
208       
209        num=numpnts(CellName)
210       
211        // parse the table to fill the Strings
212        for(ii=0;ii<num;ii+=1)
213                item = "gCell_"+CellName[ii]
214                String/G $item = dummyStr
215                SVAR kwListStr = $item
216               
217                kwListStr = ReplaceStringByKey("cell", kwListStr, CellName[ii], "=", ",", 0)
218                kwListStr = ReplaceNumberByKey("lambda", kwListStr, lambda[ii], "=", ",", 0)
219                kwListStr = ReplaceNumberByKey("Te", kwListStr, Te[ii], "=", ",", 0)
220                kwListStr = ReplaceNumberByKey("err_Te", kwListStr, err_Te[ii], "=", ",", 0)
221                kwListStr = ReplaceNumberByKey("mu", kwListStr, mu[ii], "=", ",", 0)
222                kwListStr = ReplaceNumberByKey("err_mu", kwListStr, err_mu[ii], "=", ",", 0)
223               
224        endfor
225
226        SetDataFolder root:
227        return(0)
228End
229
230
231// makes the panel after the waves are generated
232//
233// allow edits of the cells to update the string values
234// add a button to "revert" (with warning)
235// add a button to add new cells (insert a new row in the table, then update)
236//
237Function DrawCellParamPanel()
238
239        SetDataFolder root:Packages:NIST:Polarization:Cells
240       
241        PauseUpdate; Silent 1           // building window...
242        NewPanel /W=(775,44,1375,377)/N=CellParamPanel/K=1 as "Fundamental Cell Parameters"
243        ModifyPanel cbRGB=(65535,49151,55704)
244        ModifyPanel fixedSize=1
245
246//      ShowTools/A
247        Button button_0,pos={10,10},size={90,20},proc=AddCellButtonProc,title="Add Cell"
248        Button button_1,pos={118,10},size={130,20},proc=SaveCellParButtonProc,title="Save Parameters"
249        Button button_2,pos={265,10},size={130,20},proc=RevertCellParButtonProc,title="Revert Parameters"
250        Button button_3,pos={420,10},size={35,20},proc=CellHelpParButtonProc,title="?"
251
252       
253        Edit/W=(14,55,582,318)/HOST=#
254        ModifyTable width(Point)=0
255        RenameWindow #,T0
256
257        WAVE/T CellName
258        WAVE lambda,Te,err_Te,mu,err_mu
259
260        AppendtoTable/W=# CellName,lambda,Te,err_Te,mu,err_mu
261        SetActiveSubwindow ##
262
263        SetDataFolder root:
264        return(0)
265End
266
267Function CellHelpParButtonProc(ba) : ButtonControl
268        STRUCT WMButtonAction &ba
269
270        switch( ba.eventCode )
271                case 2: // mouse up
272                        // click code here
273                        DoAlert 0,"Help for Cell Param Panel not written yet"
274                        break
275                case -1: // control being killed
276                        break
277        endswitch
278
279        return 0
280End
281
282
283Function AddCellButtonProc(ba) : ButtonControl
284        STRUCT WMButtonAction &ba
285
286        switch( ba.eventCode )
287                case 2: // mouse up
288                        // click code here
289                        SetDataFolder root:Packages:NIST:Polarization:Cells
290
291                        WAVE/T CellName
292                        WAVE lambda,Te,err_Te,mu,err_mu
293                        Variable ii= numpnts(CellName)
294                       
295                        InsertPoints  ii, 1, CellName,lambda,Te,err_Te,mu,err_mu
296                       
297                        SetDataFolder root:
298                        break
299                case -1: // control being killed
300                        break
301        endswitch
302
303        return 0
304End
305
306Function SaveCellParButtonProc(ba) : ButtonControl
307        STRUCT WMButtonAction &ba
308
309        switch( ba.eventCode )
310                case 2: // mouse up
311                        // click code here
312                        Save_HeCell_ParamWaves()
313                        break
314                case -1: // control being killed
315                        break
316        endswitch
317
318        return 0
319End
320
321Function RevertCellParButtonProc(ba) : ButtonControl
322        STRUCT WMButtonAction &ba
323
324        switch( ba.eventCode )
325                case 2: // mouse up
326                        // click code here
327                        InitPolarizationGlobals()
328                        Make_HeCell_ParamWaves()
329                        break
330                case -1: // control being killed
331                        break
332        endswitch
333
334        return 0
335End
336
337// END PROCEDURES for He cell parameters
338/////////////////////////////////
339
340
341
342
343
344
345// Decay parameters for each cell. Results are stored in a wave note for each cell
346//
347//      muP=
348//      err_muP=
349// P0=
350//      err_P0=         ? is this needed?
351//      T0=
352// gamma=
353//      err_gamma=
354//
355// str = "muP=2,err_muP=0,P0=0.6,err_P0=0,T0=asdf,gamma=200,err_gamma=0,"
356//
357//
358// for this panel, the cell parameters are stored as kw strings
359// all of the strings start w/ "gDecay_"
360//
361Macro ShowCellDecayPanel()
362       
363        // init folders
364        // ASK before initializing cell constants
365        // open the panel
366        DoWindow/F DecayPanel
367        if(V_flag == 0)
368                InitPolarizationFolders()
369                InitDecayGlobals()
370                DecayParamPanel()
371        endif
372end
373
374Function InitDecayGlobals()
375
376        SetDataFolder root:Packages:NIST:Polarization:Cells
377       
378        String/G gMuPo = "muPo"
379        String/G gPo = "Po"
380        String/G gGamma = "gamma"
381        String/G gT0 = "today's the day"
382       
383       
384        SetDataFolder root:
385        return(0)
386End
387
388
389
390
391// makes the panel for the decay parameter and gamma fitting
392//
393Function DecayParamPanel()
394
395        SetDataFolder root:Packages:NIST:Polarization:Cells
396       
397        PauseUpdate; Silent 1           // building window...
398        NewPanel /W=(759,44,1572,713)/N=DecayPanel/K=1 as "Cell Decay Parameters"
399        ModifyPanel cbRGB=(32768,54615,65535)
400//      Button button_3,pos={505,16},size={35,20},proc=DecayHelpParButtonProc,title="?"
401        PopupMenu popup_0,pos={32,18},size={49,20},title="Cell",proc=DecayPanelPopMenuProc
402        PopupMenu popup_0,mode=1,value= #"D_CellNameList()"
403       
404        Button button_0,pos={584,365},size={70,20},proc=DecayFitButtonProc,title="Do Fit"
405       
406        GroupBox group_0,pos={550,399},size={230,149},title="FIT RESULTS",fSize=10
407        GroupBox group_0,fStyle=1
408        SetVariable setvar_0,pos={560,428},size={200,13},title="muPo of 3He"
409        SetVariable setvar_0,fStyle=1,limits={0,0,0},barmisc={0,1000}
410        SetVariable setvar_0,value= root:Packages:NIST:Polarization:Cells:gMuPo
411        SetVariable setvar_1,pos={560,460},size={200,13},title="Po of 3He"
412        SetVariable setvar_1,fStyle=1,limits={0,0,0},barmisc={0,1000}
413        SetVariable setvar_1,value= root:Packages:NIST:Polarization:Cells:gPo
414        SetVariable setvar_2,pos={560,518},size={200,13},title="Gamma (h)",fStyle=1
415        SetVariable setvar_2,limits={0,0,0},barmisc={0,1000}
416        SetVariable setvar_2,value= root:Packages:NIST:Polarization:Cells:gGamma
417        SetVariable setvar_3,pos={560,488},size={200,15},title="T0",fStyle=1
418        SetVariable setvar_3,limits={0,0,0},value= root:Packages:NIST:Polarization:Cells:gT0
419       
420
421        Button button_1,pos={579,294},size={120,20},proc=CalcRowParamButton,title="Calc Sel Row"
422        Button button_2,pos={307,18},size={110,20},proc=ClearDecayWavesButton,title="Clear Table"
423        Button button_3,pos={579,333},size={120,20},proc=ShowCalcRowButton,title="Show Calc"
424        Button button_4,pos={440,18},size={110,20},proc=ClearDecayWavesRowButton,title="Clear Row"
425        Button button_5,pos={620,18},size={40,20},proc=DecayHelpParButtonProc,title="?"
426        Button button_6,pos={620,620},size={100,20},proc=WindowSnapshotButton,title="Snapshot"
427        Button button_7,pos={620,580},size={130,20},proc=ManualEnterDecayButton,title="Manual Entry"
428
429
430
431        // table
432        Edit/W=(14,55,794,275)/HOST=#
433        ModifyTable format=1,width=0
434        RenameWindow #,T0
435        SetActiveSubwindow ##
436       
437        // graph
438        Display/W=(15,291,540,652)/HOST=#  //root:yy vs root:xx
439        ModifyGraph frameStyle=2
440        ModifyGraph mode=4
441        ModifyGraph marker=19
442        ModifyGraph rgb=(0,0,0)
443        ModifyGraph msize=2
444
445        Legend
446//      ModifyGraph log(left)=1
447//      ErrorBars yy OFF
448        RenameWindow #,G0
449        SetActiveSubwindow ##
450
451        SetDataFolder root:
452        return(0)
453End
454
455// allows manual entry of Decay values
456//
457// see DecayFitButtonProc
458//
459Function ManualEnterDecayButton(ba) : ButtonControl
460        STRUCT WMButtonAction &ba
461
462        Variable selRow,err=0
463        String fname, t0str, condStr,noteStr,t1Str,cellStr
464
465        switch( ba.eventCode )
466                case 2: // mouse up
467                        // click code here
468                        Variable gamma_val,err_gamma,muPo, err_muPo, Po, err_Po, runNum
469
470                        ControlInfo/W=DecayPanel popup_0
471                        cellStr = S_Value
472                               
473//                      SetDataFolder root:Packages:NIST:Polarization:Cells:
474                       
475                        WAVE decay=$("root:Packages:NIST:Polarization:Cells:Decay_"+cellStr)            //the one that is displayed
476//                      WAVE calc=$("root:Packages:NIST:Polarization:Cells:DecayCalc_"+cellStr)         //with the results
477
478
479                        Prompt Po, "Enter Po: "         
480                        Prompt err_Po, "Enter err_Po: "         
481                        Prompt muPo, "Enter muPo: "             
482                        Prompt err_muPo, "Enter err_muPo: "             
483                        Prompt gamma_val, "Enter gamma: "               
484                        Prompt err_gamma, "Enter err_gamma: "
485                        Prompt runNum,"Run number for time=0 of decay" 
486                        DoPrompt "Enter Cell Decay Parameters", Po, err_Po, muPo, err_muPo, gamma_val, err_gamma, runNum
487                        if (V_Flag)
488                                return -1                                                               // User canceled
489                        endif
490                       
491                        fname = FindFileFromRunNumber(runNum)
492                        t0str = getFileCreationDate(fname)
493                                       
494//              for the wave note
495                        noteStr = note(decay)
496                        noteStr = ReplaceNumberByKey("muP", noteStr, MuPo ,"=", ",", 0)
497                        noteStr = ReplaceNumberByKey("P0", noteStr, Po ,"=", ",", 0)
498                        noteStr = ReplaceNumberByKey("err_muP", noteStr, err_muPo ,"=", ",", 0)
499                        noteStr = ReplaceNumberByKey("err_P0", noteStr, err_Po ,"=", ",", 0)
500                        noteStr = ReplaceNumberByKey("gamma", noteStr, gamma_val ,"=", ",", 0)
501                        noteStr = ReplaceNumberByKey("err_gamma", noteStr, err_gamma ,"=", ",", 0)
502                        noteStr = ReplaceStringByKey("T0", noteStr, t0Str  ,"=", ",", 0)
503                        // replace the string
504                        Note/K decay
505                        Note decay, noteStr
506
507                        // for the panel display
508                        SVAR gGamma  = root:Packages:NIST:Polarization:Cells:gGamma
509                        SVAR gMuPo = root:Packages:NIST:Polarization:Cells:gMuPo
510                        SVAR gPo  = root:Packages:NIST:Polarization:Cells:gPo           
511                        SVAR gT0 = root:Packages:NIST:Polarization:Cells:gT0
512                       
513                        gT0 = t0Str             //for display
514                        sprintf gMuPo, "%g +/- %g",muPo, err_muPo
515                        sprintf gPo, "%g +/- %g",Po,err_Po
516                        sprintf gGamma, "%g +/- %g",gamma_val,err_gamma
517
518                       
519                        break
520                case -1: // control being killed
521                        break
522        endswitch
523
524        return 0
525End
526
527
528Function DecayPanelPopMenuProc(pa) : PopupMenuControl
529        STRUCT WMPopupAction &pa
530
531        switch( pa.eventCode )
532                case 2: // mouse up
533                        Variable popNum = pa.popNum
534                        String popStr = pa.popStr
535                       
536                        SetDataFolder root:Packages:NIST:Polarization:Cells
537
538                        // based on the selected string, display the right set of inputs
539//                      Print "now I need to display the right set of waves (2D text?) for ",popStr
540
541                        // for the given cell name, if the wave(s) exist, declare them
542                        if(exists("Decay_"+popStr) == 1)
543                                WAVE decay = $("Decay_"+popStr)
544                        else
545                                // if not, make it, and the space for the results of the calculation
546                                MakeDecayResultWaves(popStr)
547                                WAVE decay = $("root:Packages:NIST:Polarization:Cells:Decay_"+popStr)
548                        endif                   
549                        // append matrix, clearing the old one first
550                        SetDataFolder root:Packages:NIST:Polarization:Cells
551
552                        KillWindow DecayPanel#T0
553                        Edit/W=(14,55,794,275)/HOST=DecayPanel
554                        RenameWindow #,T0
555                        AppendtoTable/W=DecayPanel#T0 decay.ld                  //show the labels
556                        ModifyTable width(Point)=0
557                        ModifyTable width(decay.l)=20
558                       
559                        SetActiveSubwindow ##
560       
561                        SetDataFolder root:
562                       
563                        break
564                case -1: // control being killed
565                        break
566        endswitch
567
568        return 0
569End
570
571Function MakeDecayResultWaves(popStr)
572        String popStr
573
574        SetDataFolder root:Packages:NIST:Polarization:Cells
575
576        Make/O/D/N=(1,8) $("Decay_"+popStr)
577        WAVE decay = $("Decay_"+popStr)
578        // set the column labels
579        SetDimLabel 1,0,Trans_He_In,decay
580        SetDimLabel 1,1,Trans_He_Out,decay
581        SetDimLabel 1,2,Blocked,decay
582        SetDimLabel 1,3,mu_star,decay
583        SetDimLabel 1,4,Pol_Cell,decay
584        SetDimLabel 1,5,T_Major,decay
585        SetDimLabel 1,6,Include,decay                   //for a mask wave, non-zero is used in the fit
586        SetDimLabel 1,7,elapsed_hr,decay
587        decay[0][6] = 1                 //default to include the point
588       
589        // generate the dummy wave note now, change as needed
590        Note decay, "muP=2,err_muP=0,P0=0.6,err_P0=0,T0=asdf,gamma=200,err_gamma=0,"
591       
592        // to hold the results of the calculation
593        Make/O/D/N=(1,14) $("DecayCalc_"+popStr)
594        WAVE decayCalc = $("DecayCalc_"+popStr)
595        SetDimLabel 1,0,CR_Trans_He_In,decayCalc
596        SetDimLabel 1,1,err_CR_Trans_He_In,decayCalc
597        SetDimLabel 1,2,CR_Trans_He_Out,decayCalc
598        SetDimLabel 1,3,err_CR_Trans_He_Out,decayCalc
599        SetDimLabel 1,4,CR_Blocked,decayCalc
600        SetDimLabel 1,5,err_CR_Blocked,decayCalc
601        SetDimLabel 1,6,muPo,decayCalc
602        SetDimLabel 1,7,err_muPo,decayCalc
603        SetDimLabel 1,8,Po,decayCalc
604        SetDimLabel 1,9,err_Po,decayCalc
605        SetDimLabel 1,10,Tmaj,decayCalc
606        SetDimLabel 1,11,err_Tmaj,decayCalc
607        SetDimLabel 1,12,gamm,decayCalc
608        SetDimLabel 1,13,err_gamm,decayCalc     
609
610        SetDataFolder root:
611
612        return(0)
613End
614
615
616// since the "Decay_" table can be edited directly to increase the number of rows, it is tough
617// to increase the number of rows in the "DecayCalc_" wave and keep them in sync.
618//
619// --so make sure that the sizes match, and do them all
620//
621Function CalcRowParamButton(ba) : ButtonControl
622        STRUCT WMButtonAction &ba
623
624        Variable selRow,err=0
625        String fname, t0str, cellStr,noteStr,t1Str
626
627        switch( ba.eventCode )
628                case 2: // mouse up
629                        // click code here
630                        Variable cr1,cr2,cr3,err_cr1,err_cr2,err_cr3
631                        Variable muPo,err_muPo,Po,err_Po,Pcell,err_Pcell,Tmaj,err_Tmaj
632                        //Variable Te,err_Te,mu,err_mu
633                               
634                        ControlInfo/W=DecayPanel popup_0
635                        cellStr = S_Value
636                        WAVE w=$("root:Packages:NIST:Polarization:Cells:Decay_"+cellStr)                //the one that is displayed
637                        WAVE calc=$("root:Packages:NIST:Polarization:Cells:DecayCalc_"+cellStr)         // behind the scenes
638                       
639                        Variable numRows,ncalc,diff
640                        numRows = DimSize(w,0)          //rows in the displayed table
641                        ncalc = DimSize(calc,0)
642                       
643                        // add rows to the DecayCalc_ matrix as needed
644                        if(numRows != ncalc)
645                                if(ncalc > numRows)
646                                        DoAlert 0,"The DecayCalc_ is larger than displayed. Seek help."
647                                        err = 1
648                                        return(err)
649                                else
650                                        diff = numRows - ncalc
651                                        InsertPoints/M=0 ncalc, diff, calc
652                                endif
653                        endif
654                       
655                       
656//                      GetSelection table, DecayPanel#T0, 1
657//                      selRow = V_startRow
658
659                        Variable sum_muP, err_avg_muP, sum_Po, err_avg_Po, avg_muP, avg_Po
660                        sum_muP = 0
661                        sum_Po = 0
662                        err_avg_muP = 0
663                        err_avg_Po = 0
664                       
665                        for(selRow=0;selRow<numRows;selRow+=1)
666                                Print "calculate the row ",selRow
667
668                                if(selRow == 0)
669                                        //find T0
670                                        fname = FindFileFromRunNumber(w[0][%Trans_He_In])
671                                        t0str = getFileCreationDate(fname)
672                                        SVAR gT0 = root:Packages:NIST:Polarization:Cells:gT0
673                                        gT0 = t0Str             //for display
674                                        noteStr = note(w)
675                                        noteStr = ReplaceStringByKey("T0", noteStr, gT0  ,"=", ",", 0)
676                                        Note/K w
677                                        Note w, noteStr
678                                        Print t0str
679                                        w[selRow][%elapsed_hr] = 0                      //by definition
680                                endif
681                               
682                                // parse the rows, report errors (there, not here), exit if any found
683                                err = ParseDecayRow(w,selRow)
684                                if(err)
685                                        return 0
686                                endif
687                               
688                                // do the calculations:
689                                // 1 for each file, return the count rate and err_CR (normalize to atten or not)
690       
691                                Print "************The Blocked CR *is* rescaled to zero attenuators -- "
692                                cr1 = TotalCR_FromRun(w[selRow][%Trans_He_In],err_cr1,0)
693                                cr2 = TotalCR_FromRun(w[selRow][%Trans_He_Out],err_cr2,0)
694//                              cr3 = TotalCR_FromRun(w[selRow][%Blocked],err_cr3,1)                    //blocked beam is NOT normalized to zero attenuators
695                                cr3 = TotalCR_FromRun(w[selRow][%Blocked],err_cr3,0)                    //blocked beam is NOT normalized to zero attenuators
696                               
697                                calc[selRow][%CR_Trans_He_In] = cr1
698                                calc[selRow][%CR_Trans_He_Out] = cr2
699                                calc[selRow][%CR_Blocked] = cr3
700                                calc[selRow][%err_cr_Trans_He_In] = err_cr1
701                                calc[selRow][%err_cr_Trans_He_Out] = err_cr2
702                                calc[selRow][%err_cr_Blocked] = err_cr3
703       
704       
705                                // 2 find the mu and Te values for cellStr
706                                SVAR gCellKW = $("root:Packages:NIST:Polarization:Cells:gCell_"+cellStr)
707                                //(moved to a separate function, just pass the string)
708        //                      Te = NumberByKey("Te", gCellKW, "=", ",", 0)
709        //                      err_Te = NumberByKey("err_Te", gCellKW, "=", ",", 0)
710        //                      mu = NumberByKey("mu", gCellKW, "=", ",", 0)
711        //                      err_mu = NumberByKey("err_mu", gCellKW, "=", ",", 0)
712        //                     
713                                // 3 Calc muPo and error
714                                muPo = Calc_muPo(calc,gCellKW,selRow,err_muPo)
715                                calc[selRow][%muPo] = muPo
716                                calc[selRow][%err_muPo] = err_muPo
717                                w[selRow][%mu_star] = muPo
718                               
719                                // 3.5 calc Polarization of cell (no value or error stored in calc wave?)
720                                PCell = Calc_PCell(muPo,err_muPo,err_PCell)
721        //                      PCell = Calc_PCell(2,err_muPo,err_PCell)
722                                w[selRow][%Pol_Cell] = PCell
723       
724                                // 4 calc Po and error
725                                Po = Calc_Po(gCellKW,muPo,err_muPo,err_Po)
726        //                      Po = Calc_Po(gCellKW,2,err_muPo,err_Po)
727                                calc[selRow][%Po] = Po
728                                calc[selRow][%err_Po] = err_Po
729                               
730                                // 5 calc Tmaj and error
731                                Tmaj = Calc_Tmaj(gCellKW,Po,err_Po,err_Tmaj)
732                                calc[selRow][%Tmaj] = Tmaj
733                                calc[selRow][%err_Tmaj] = err_Tmaj
734                                w[selRow][%T_major] = Tmaj
735                               
736                                // elapsed hours
737                                fname = FindFileFromRunNumber(w[selRow][%Trans_He_In])
738                                t1str = getFileCreationDate(fname)
739                                w[selRow][%elapsed_hr] = ElapsedHours(t0Str,t1Str)
740                               
741                                // running average of muP and Po
742                                sum_muP += muPo
743                                sum_Po += Po
744                                err_avg_muP += err_muPo^2
745                                err_avg_Po += err_Po^2
746                               
747                        endfor          //loop over rows
748                       
749                        // now get a running average of muP, Po, and the errors
750                        avg_muP = sum_muP/numRows
751                        avg_Po = sum_Po/numRows
752                        err_avg_muP = sqrt(err_avg_muP) / numRows
753                        err_avg_Po = sqrt(err_avg_Po) / numRows
754                       
755//                      str = "muP=2,err_muP=0,P0=0.6,err_P0=0,T0=asdf,gamma=200,err_gamma=0,"
756
757                        // Don't put the average values into the wave note, but rather the results of the fit
758//                      noteStr = note(w)
759//                      noteStr = ReplaceNumberByKey("muP", noteStr, avg_muP ,"=", ",", 0)
760//                      noteStr = ReplaceNumberByKey("P0", noteStr, avg_Po ,"=", ",", 0)
761//                      noteStr = ReplaceNumberByKey("err_muP", noteStr, err_avg_muP ,"=", ",", 0)
762//                      noteStr = ReplaceNumberByKey("err_P0", noteStr, err_avg_Po ,"=", ",", 0)
763//                     
764//                      // replace the string
765//                      Note/K w
766//                      Note w, noteStr
767                                       
768                        Printf "Average muP = %g +/- %g (%g%)\r",avg_muP,err_avg_muP,err_avg_muP/avg_muP*100
769                        Printf "Average Po = %g +/- %g (%g%)\r",avg_Po,err_avg_Po,err_avg_Po/avg_Po*100
770                       
771                        //update the global values for display (not these, but after the fit)
772//                      Print " -- need to add the error to the display on the panel    "
773//                      NVAR gMuPo = root:Packages:NIST:Polarization:Cells:gMuPo
774//                      NVAR gPo  = root:Packages:NIST:Polarization:Cells:gPo
775//                      gMuPo = avg_muP
776//                      gPo = avg_Po
777                       
778                        break
779                case -1: // control being killed
780                        break
781        endswitch
782
783        return 0
784End
785
786
787// calculate Tmaj and its error
788Function Calc_Tmaj(cellStr,Po,err_Po,err_Tmaj)
789        String cellStr
790        Variable Po,err_Po,&err_Tmaj
791       
792        Variable Tmaj,arg
793        Variable Te,err_Te,mu,err_mu
794// cell constants       
795        Te = NumberByKey("Te", cellStr, "=", ",", 0)
796        err_Te = NumberByKey("err_Te", cellStr, "=", ",", 0)
797        mu = NumberByKey("mu", cellStr, "=", ",", 0)
798        err_mu = NumberByKey("err_mu", cellStr, "=", ",", 0)
799       
800        Tmaj = Te*exp(-mu*(1-Po))
801       
802        //the error
803        err_Tmaj = (Tmaj/Te)^2*err_Te^2 + (Tmaj*(1-Po))^2*err_mu^2 + (Tmaj*mu)^2*err_Po^2
804        err_Tmaj = sqrt(err_Tmaj)
805       
806        Printf "Tmaj = %g +/- %g (%g%)\r",Tmaj,err_Tmaj,err_Tmaj/Tmaj*100
807
808       
809        return(Tmaj)
810End
811
812
813// calculate PCell and its error
814//
815//
816Function Calc_PCell(muPo,err_muPo,err_PCell)
817        Variable muPo,err_muPo,&err_PCell
818       
819        Variable PCell,arg
820
821        PCell = tanh(muPo)
822       
823        // error (single term, sqrt already done)
824        err_Pcell = (1 - (tanh(muPo))^2) * err_muPo
825       
826        Printf "Pcell = %g +/- %g (%g%)\r",Pcell,err_Pcell,err_Pcell/PCell*100
827
828        return(PCell)
829End
830
831// calculate Po and its error
832Function Calc_Po(cellStr,muPo,err_muPo,err_Po)
833        String cellStr
834        Variable muPo,err_muPo,&err_Po
835       
836        Variable Po,tmp
837        Variable mu,err_mu
838// cell constants       
839        mu = NumberByKey("mu", cellStr, "=", ",", 0)
840        err_mu = NumberByKey("err_mu", cellStr, "=", ",", 0)
841       
842        Po = muPo/mu
843       
844        tmp = (err_muPo/muPo)^2 + (err_mu/mu)^2
845        err_Po = Po * sqrt(tmp)
846       
847        Printf "Po = %g +/- %g (%g%)\r",Po,err_Po,err_Po/Po*100
848        return(Po)
849End
850
851// calculate muPo and its error
852Function Calc_muPo(calc,cellStr,selRow,err_muPo)
853        Wave calc
854        String cellStr
855        Variable selRow,&err_muPo
856       
857        Variable muPo,arg
858        Variable Te,err_Te,mu,err_mu
859// cell constants       
860        Te = NumberByKey("Te", cellStr, "=", ",", 0)
861        err_Te = NumberByKey("err_Te", cellStr, "=", ",", 0)
862        mu = NumberByKey("mu", cellStr, "=", ",", 0)
863        err_mu = NumberByKey("err_mu", cellStr, "=", ",", 0)
864       
865        Variable cr1,cr2,cr3,err_cr1,err_cr2,err_cr3
866        // cr1 is He in, 2 is He out, 3 is blocked
867        cr1      =      calc[selRow][%CR_Trans_He_In]
868        cr2 =   calc[selRow][%CR_Trans_He_Out]
869        cr3 =   calc[selRow][%CR_Blocked]
870        err_cr1 =       calc[selRow][%err_cr_Trans_He_In]
871        err_cr2 =       calc[selRow][%err_cr_Trans_He_Out]
872        err_cr3 =       calc[selRow][%err_cr_Blocked]
873       
874        muPo = acosh( (cr1 - cr3)/(cr2 - cr3) * (1/(Te*exp(-mu))) )
875       
876        Variable arg_err, tmp1, tmp2
877        // the error is a big mess to calculate, since it's messy argument inside acosh
878        arg = (cr1 - cr3)/(cr2 - cr3) * (1/(Te*exp(-mu)))
879        tmp2 =  (1/sqrt(arg+1)/sqrt(arg-1))^2                                   // derivative of acosh(arg) (squared)
880       
881        // calculate the error of the argument first, then the error of acosh(arg)
882        // there are 5 partial derivatives
883        arg_err = tmp2 * ( arg/(cr1 - cr3) * err_cr1 )^2                //CR in
884        arg_err += tmp2 * ( arg/(cr2 - cr3) * err_cr2 )^2       //CR out
885        arg_err += tmp2 * ((-arg/(cr1 - cr3) +  arg/(cr2 - cr3) )* err_cr3 )^2//CR bkg
886        arg_err += tmp2 * ( -arg/Te * err_Te )^2                                        //Te
887        arg_err += tmp2 * ( arg * err_mu )^2                                            //mu  (probably the dominant relative error)
888       
889        err_muPo = sqrt(arg_err)
890       
891       
892        return(muPo)
893End
894
895
896//Function testCR(num)
897//      Variable num
898//      Variable err_cr
899//     
900//      Variable noNorm=0
901//      Variable cr = TotalCR_FromRun(num,err_cr,noNorm)
902//      printf "CR = %g +/- %g (%g%)\r",cr,err_cr,err_cr/cr*100
903//      return(0)
904//End
905
906// calculate the total detector CR ane its error.
907//
908// the result is automatically normalized to 10^8 monitor counts, and to zero attenuators
909//
910// if noNorm = 1, then the normalization to attenuators is not done
911//
912Function TotalCR_FromRun(num,err_cr,noNorm)
913        Variable num,&err_cr,noNorm
914
915        String fname="",instr="",tmpStr=""
916        Variable cr,cts,err_cts,ctTime,monCts,attenNo,lambda,attenTrans,atten_err
917       
918        fname = FindFileFromRunNumber(num)
919        cts = getDetCount(fname)
920        err_cts = sqrt(cts)
921       
922        ctTime = getCountTime(fname)
923        monCts = getMonitorCount(fname)
924        attenNo = getAttenNumber(fname)
925        instr = getAcctName(fname)              //this is 11 characters
926        lambda = getWavelength(fname)
927        attenTrans = AttenuationFactor(instr,lambda,AttenNo,atten_err)
928       
929        if(noNorm==1)                   //don't normalize to attenuation
930                attenTrans=1
931                atten_err=0
932        endif
933        cr = cts/ctTime*1e8/monCts/attenTrans
934        err_cr = cr * sqrt(err_cts^2/cts^2 + atten_err^2/attenTrans^2)
935       
936        printf "CR = %g +/- %g (%g%)\r",cr,err_cr,err_cr/cr*100
937
938               
939        return(cr)
940end
941
942
943// input is VAX date and time string, t1 later than t0
944//
945Function ElapsedHours(t0Str,t1Str)
946        String t0Str,t1Str
947       
948        Variable t0,t1,elapsed
949       
950        t0 = ConvertVAXDateTime2Secs(t0Str)             //seconds
951        t1 = ConvertVAXDateTime2Secs(t1Str)
952       
953        elapsed = t1-t0
954        elapsed /= 3600                 //convert to hours
955       
956        return(elapsed)
957End
958
959// bring up a table with the calculation results
960Function ShowCalcRowButton(ba) : ButtonControl
961        STRUCT WMButtonAction &ba
962
963        switch( ba.eventCode )
964                case 2: // mouse up
965                        // click code here
966                        ControlInfo/W=DecayPanel popup_0
967                        String cellStr = S_Value
968                        WAVE calc=$("root:Packages:NIST:Polarization:Cells:DecayCalc_"+cellStr)         //the one that is displayed
969                        edit calc.ld
970                                               
971                        break
972                case -1: // control being killed
973                        break
974        endswitch
975
976        return 0
977End
978
979
980Function DecayFitButtonProc(ba) : ButtonControl
981        STRUCT WMButtonAction &ba
982
983        String cellStr=""
984        Variable num
985
986        switch( ba.eventCode )
987                case 2: // mouse up
988                        // click code here
989
990                        ControlInfo/W=DecayPanel popup_0
991                        cellStr = S_Value
992                       
993                        SetDataFolder root:Packages:NIST:Polarization:Cells:
994                       
995                        WAVE decay=$("Decay_"+cellStr)          //the one that is displayed
996                        WAVE calc=$("DecayCalc_"+cellStr)               //the one that is displayed
997                       
998//                      make temp copies for the fit and plot, extra for mask
999                        num = DimSize(calc,0)
1000                        Make/O/D/N=(num)                tmp_Mask,tmp_hr,tmp_muP,tmp_err_muP,tmp_muP2
1001                       
1002                        tmp_Mask = decay[p][%Include]
1003                        tmp_hr = decay[p][%elapsed_hr]
1004                        tmp_muP = calc[p][%muPo]
1005                        tmp_muP2 = tmp_muP
1006                        tmp_err_muP = calc[p][%err_muPo]
1007                       
1008                        tmp_muP2 = (tmp_Mask == 1) ? NaN : tmp_muP2                     //only excluded points will plot
1009                       
1010                        // clear old data, and plot the new
1011                        //
1012                        CheckDisplayed/W=DecayPanel#G0 tmp_muP,tmp_muP2,fit_tmp_muP
1013                        // if both present, bit 0 + bit 1 = 3
1014                        if(V_flag & 2^0)                        //check bit 0
1015                                RemoveFromGraph/W=DecayPanel#G0 tmp_muP
1016                        endif
1017                        if(V_flag & 2^1)
1018                                RemoveFromGraph/W=DecayPanel#G0 tmp_muP2
1019                        endif
1020                        if(V_flag & 2^2)
1021                                RemoveFromGraph/W=DecayPanel#G0 fit_tmp_muP
1022                        endif
1023                       
1024                        AppendToGraph/W=DecayPanel#G0 tmp_muP vs tmp_hr
1025                        AppendToGraph/W=DecayPanel#G0 tmp_muP2 vs tmp_hr
1026
1027                        ModifyGraph/W=DecayPanel#G0 log(left)=1
1028                        ModifyGraph/W=DecayPanel#G0 frameStyle=2
1029                        ModifyGraph/W=DecayPanel#G0 mode=3
1030                        ModifyGraph/W=DecayPanel#G0 marker=19
1031                        ModifyGraph/W=DecayPanel#G0 rgb(tmp_muP)=(1,16019,65535),rgb(tmp_muP2)=(65535,0,0)
1032                        ModifyGraph/W=DecayPanel#G0 msize=3
1033                        ErrorBars/W=DecayPanel#G0 tmp_muP,Y wave=(tmp_err_muP,tmp_err_muP)
1034                       
1035                        Label/W=DecayPanel#G0 left "mu*P"
1036                        Label/W=DecayPanel#G0 bottom "time (h)"
1037                       
1038// do the fit
1039//       as long as the constant X0 doesn't stray from zero, exp_XOffset is OK, otherwise I'll need to switch to the exp function
1040
1041                        SetActiveSubwindow DecayPanel#G0                        //to get the automatic fit to show up on the graph
1042
1043//                      Make/O/D/N=3 fitCoef={0,5,0.05}
1044//                      CurveFit/H="100"/M=2/W=0/TBOX=(0x310) exp_XOffset, kwCWave=fitCoef, tmp_muP /X=tmp_hr /D /I=1 /W=tmp_err_muP /M=tmp_Mask
1045
1046
1047                        Make/O/D/N=3 fitCoef={0,5,0.05}
1048                        CurveFit/H="100"/M=2/W=0/TBOX=(0x310) exp, kwCWave=fitCoef, tmp_muP /X=tmp_hr /D /I=1 /W=tmp_err_muP /M=tmp_Mask
1049                       
1050
1051                        SetActiveSubwindow ##
1052                       
1053                       
1054// then report and save the results
1055//                      the exp function => y = y0 + A*exp(-Bx)
1056//                      W_coef[0] = y0 should be close to zero (or fixed at zero)
1057//                      W_coef[1] = A should be close to the initial muP value
1058//                      W_coef[2] = B is 1/Gamma
1059//                      WAVE W_coef=W_coef
1060                        WAVE W_sigma=W_sigma
1061                        Variable gamma_val,err_gamma,muPo, err_muPo, Po, err_Po
1062                        String noteStr=""
1063       
1064                        SVAR gCellKW = $("root:Packages:NIST:Polarization:Cells:gCell_"+cellStr)       
1065                        SVAR gGamma  = root:Packages:NIST:Polarization:Cells:gGamma
1066                        SVAR gMuPo = root:Packages:NIST:Polarization:Cells:gMuPo
1067                        SVAR gPo  = root:Packages:NIST:Polarization:Cells:gPo
1068                       
1069                        muPo = fitCoef[1]
1070                        err_muPo = W_sigma[1]
1071                       
1072                        Po = Calc_Po(gCellKW,muPo,err_muPo,err_Po)
1073
1074                        // if exp_XOffset used
1075//                      gGamma = fitCoef[2]
1076//                      err_Gamma = W_sigma[2]
1077
1078                        // calculating the error using exp is the inverse of coef[2]:
1079                        gamma_val  = 1/fitCoef[2]
1080                        err_gamma = W_sigma[2]/(fitCoef[2])^2
1081               
1082                       
1083//              for the wave note
1084                        noteStr = note(decay)
1085                        noteStr = ReplaceNumberByKey("muP", noteStr, MuPo ,"=", ",", 0)
1086                        noteStr = ReplaceNumberByKey("P0", noteStr, Po ,"=", ",", 0)
1087                        noteStr = ReplaceNumberByKey("err_muP", noteStr, err_muPo ,"=", ",", 0)
1088                        noteStr = ReplaceNumberByKey("err_P0", noteStr, err_Po ,"=", ",", 0)
1089                        noteStr = ReplaceNumberByKey("gamma", noteStr, gamma_val ,"=", ",", 0)
1090                        noteStr = ReplaceNumberByKey("err_gamma", noteStr, err_gamma ,"=", ",", 0)
1091
1092                        // replace the string
1093                        Note/K decay
1094                        Note decay, noteStr
1095                       
1096                       
1097                        // for the panel display
1098                        sprintf gMuPo, "%g +/- %g",fitCoef[1],W_sigma[1]
1099                        sprintf gPo, "%g +/- %g",Po,err_Po
1100                        sprintf gGamma, "%g +/- %g",gamma_val,err_gamma
1101
1102                       
1103                       
1104                        SetDataFolder root:
1105                       
1106                        break
1107                case -1: // control being killed
1108                        break
1109        endswitch
1110
1111        return 0
1112End
1113
1114
1115// clear just the row
1116//
1117Function ClearDecayWavesRowButton(ba) : ButtonControl
1118        STRUCT WMButtonAction &ba
1119
1120        String popStr=""
1121        Variable selRow
1122       
1123        switch( ba.eventCode )
1124                case 2: // mouse up
1125                        // click code here
1126                        DoAlert 1,"Clear the selected row?"
1127                        if(V_flag !=1)
1128                                return(0)
1129                        endif
1130                       
1131                        SetDataFolder root:Packages:NIST:Polarization:Cells
1132
1133                        ControlInfo/W=DecayPanel popup_0
1134                        popStr = S_Value
1135                       
1136                        Wave decay = $("Decay_"+popStr)
1137                        Wave calc = $("DecayCalc_"+popStr)
1138
1139                        // Delete just those points
1140                                               
1141                        GetSelection table, DecayPanel#T0, 1
1142                        selRow = V_startRow
1143                        DeletePoints selRow,1,decay,calc                       
1144                       
1145                        // clear the graph and the results                     
1146                        SVAR gMuPo = root:Packages:NIST:Polarization:Cells:gMuPo
1147                        SVAR gPo  = root:Packages:NIST:Polarization:Cells:gPo
1148                        SVAR gGamma  = root:Packages:NIST:Polarization:Cells:gGamma
1149                        SVAR gT0  = root:Packages:NIST:Polarization:Cells:gT0
1150                        gMuPo = "0"
1151                        gPo = "0"
1152                        gGamma = "0"
1153                        gT0 = "recalculate"
1154                       
1155                        SetDataFolder root:
1156                        break
1157                case -1: // control being killed
1158                        break
1159        endswitch
1160
1161        return 0
1162End
1163
1164
1165
1166// for this, do I want to clear everything, or just a selected row??
1167//
1168//
1169Function ClearDecayWavesButton(ba) : ButtonControl
1170        STRUCT WMButtonAction &ba
1171
1172        String popStr=""
1173       
1174        switch( ba.eventCode )
1175                case 2: // mouse up
1176                        // click code here
1177                        DoAlert 1,"Clear all of the decay waves for the selected cell?"
1178                        if(V_flag !=1)
1179                                return(0)
1180                        endif
1181                       
1182                        SetDataFolder root:Packages:NIST:Polarization:Cells
1183
1184                        ControlInfo/W=DecayPanel popup_0
1185                        popStr = S_Value
1186                       
1187                        Wave decay = $("Decay_"+popStr)
1188                        Wave calc = $("DecayCalc_"+popStr)
1189                       
1190//                      re-initialize the decay waves, so it appears as a blank, initialized table
1191
1192                        MakeDecayResultWaves(popStr)
1193                        decay = 0
1194                        calc = 0
1195       
1196                        // clear the graph and the results?     
1197                       
1198                       
1199                                       
1200                        SVAR gMuPo = root:Packages:NIST:Polarization:Cells:gMuPo
1201                        SVAR gPo  = root:Packages:NIST:Polarization:Cells:gPo
1202                        SVAR gGamma  = root:Packages:NIST:Polarization:Cells:gGamma
1203                        SVAR gT0  = root:Packages:NIST:Polarization:Cells:gT0
1204                        gMuPo = "0"
1205                        gPo = "0"
1206                        gGamma = "0"
1207                        gT0 = "recalculate"
1208                       
1209                       
1210                        SetDataFolder root:
1211                        break
1212                case -1: // control being killed
1213                        break
1214        endswitch
1215
1216        return 0
1217End
1218
1219
1220Function DecayHelpParButtonProc(ba) : ButtonControl
1221        STRUCT WMButtonAction &ba
1222
1223        switch( ba.eventCode )
1224                case 2: // mouse up
1225                        // click code here
1226                        DoAlert 0,"Help for Cell Decay Panel not written yet"
1227                        break
1228                case -1: // control being killed
1229                        break
1230        endswitch
1231
1232        return 0
1233End
1234
1235// E=-5 is a PNG, =8 is PDF
1236// there are other options to do EPS, embed fonts, etc.
1237//
1238Function WindowSnapshotButton(ba) : ButtonControl
1239        STRUCT WMButtonAction &ba
1240
1241        switch( ba.eventCode )
1242                case 2: // mouse up
1243                        // click code here
1244                        SavePICT /E=-5/SNAP=1
1245                        break
1246                case -1: // control being killed
1247                        break
1248        endswitch
1249
1250        return 0
1251End
1252
1253// null condition is not right. if the loop fails, then the
1254// retStr will be ";;;;", not zero length. What's the proper test?
1255// Does it matter? the list of default gCell_sss should already be there.
1256//
1257Function/S D_CellNameList()
1258
1259        String retStr="",listStr,item
1260        Variable num,ii
1261       
1262        SetDataFolder root:Packages:NIST:Polarization:Cells
1263
1264        // get a list of the cell strings
1265        listStr=StringList("gCell_*",";")
1266        num=ItemsInList(listStr,";")
1267//      print listStr
1268       
1269        // parse the strings to fill the table
1270        for(ii=0;ii<num;ii+=1)
1271                item = StringFromList(ii, listStr,";")
1272                SVAR gStr = $item
1273                retStr += StringByKey("cell", gStr, "=", ",", 0) + ";"
1274        endfor
1275       
1276        if(strlen(retStr) == 0)
1277                retStr = "no cells defined;"
1278        endif
1279       
1280        SetDataFolder root:             
1281        return(retStr)
1282End
1283
1284
1285// parse the row to be sure that:
1286//
1287// - files are valid numbers
1288// - files are all at same SDD
1289// - files are all with same attenuation (just print a warning to cmd)
1290// - due to ICE FP values, I need to do a fuzzy comparison
1291//
1292//
1293Function ParseDecayRow(w,selRow)
1294        Wave w
1295        Variable selRow
1296       
1297        Variable err=0, atten1,atten2,atten3,sdd1,sdd2,sdd3,tol
1298        String fname=""
1299        tol = 0.01              //SDDs within 1%
1300       
1301        // are all file numbers valid?
1302        fname = FindFileFromRunNumber(w[selRow][%Trans_He_In])
1303        if(cmpstr(fname,"")==0)
1304                DoAlert 0,"Trans_He_In run "+num2str(w[selRow][%Trans_He_In])+" is not a valid run number"
1305                err = 1
1306        else
1307                atten1 = getAttenNumber(fname)
1308                sdd1 = getSDD(fname)
1309        endif
1310       
1311        fname = FindFileFromRunNumber(w[selRow][%Trans_He_Out])
1312        if(cmpstr(fname,"")==0)
1313                DoAlert 0,"Trans_He_Out run "+num2str(w[selRow][%Trans_He_Out])+" is not a valid run number"
1314                err = 1
1315        else
1316                atten2 = getAttenNumber(fname)
1317                sdd2 = getSDD(fname)
1318        endif
1319       
1320        fname = FindFileFromRunNumber(w[selRow][%Blocked])
1321        if(cmpstr(fname,"")==0)
1322                DoAlert 0,"Blocked run "+num2str(w[selRow][%Blocked])+" is not a valid run number"
1323                err = 1
1324        else
1325                atten3 = getAttenNumber(fname)
1326                sdd3 = getSDD(fname)
1327        endif
1328       
1329       
1330        if( (abs(sdd1 - sdd2) > tol) || (abs(sdd2 - sdd3) > tol) || (abs(sdd1 - sdd3) > tol) )
1331                DoAlert 0,"Files in row "+num2str(selRow)+" are not all at the same detector distance"
1332                err = 1
1333        endif
1334       
1335        if( (atten1 != atten2) || (atten2 != atten3) || (atten1 != atten3) )
1336                DoAlert 0,"Files in row "+num2str(selRow)+" are not all collected with the same attenuation. Just so you know."
1337                err = 0
1338        endif
1339       
1340        return(err)
1341end
1342
1343
1344////////////////////////////////////////////
Note: See TracBrowser for help on using the repository browser.