source: sans/Dev/trunk/NCNR_User_Procedures/Reduction/VSANS/V_VSANS_Event_Testing.ipf @ 1041

Last change on this file since 1041 was 1041, checked in by srkline, 6 years ago

Added the angle dependent transmission correction to the data correction in the raw_to_work step, in 2D

added a testing file that can generate fake event data, read, write, and decode it. Read is based on GBLoadWave. Hoepfully I'll not need to write an XOP. manipulation of the 64 bit words are done with simple bit shifts and logic.

also added are a number of error checking routines to improve behavior when wave, folders, etc. are missing.

File size: 7.3 KB
Line 
1#pragma TextEncoding = "MacRoman"
2#pragma rtGlobals=3             // Use modern global access method and strict wave access.
3
4
5//
6// for the event mode data with the proposed 64 bit structure, I may be able to use Igor for everything.
7//
8// Skipping the appropriate bits of the header (after I read them in) may be possible with
9// either LoadWave (treating the entire wave as 64 bit, unsigned), loading chunks from clipboard?
10// -- see in LoadWave, the suggestions for "Loading Large Waves"
11//
12// or using FBinRead, again, filling a wave, or a chunk of the data, as needed
13//
14// Then - since the data has sequential timing, not dependent on rollovers, I can
15// chunk the data for parallel processing, and piece it back together
16// after all of the decoding is done.
17//
18//
19// I don't know if it's possible to use a STRUCT  definition for each 64 bit word so that I could address
20// the bytes directly - since that may not work properly in Igor, and I'm not sure how to address the 6 byte section
21// -possibly with a uchar(6) definition?
22//
23//
24//      (from WM help) You can define an array of structures as a field in a structure:
25//              Structure mystruct
26//                      STRUCT Point pt[100]                    // Allowed as a sub-structure
27//              EndStructure
28//
29// -- which may be of use to read "blocks" of datq from a file using FBinRead
30//
31// -- right now, I'm having issues with being able to "cast" or convert uint64 values to STRUCT
32// (I don't know how to read from the struct if I can't fill it??)
33//
34// TODO:
35//
36// (5/2017)
37// The basic bits of reading work, but will need to be customized to be able to accomodate file names in/out
38// and especially the number of disabled tubes (although as long as I have the offset, it shouldn't be that
39// big of an issue.
40//
41// -- don't see the struct idea working out. only in read c-code if needed
42//
43// -- need to add detector binning to the decoding, to place the counts within the correct panels
44// -- not sure how this will work with JointHistogram Operation
45// (split to separate "streams" of values for each detector panel?
46//
47
48
49//
50Structure eventWord
51        uchar eventTime[6]
52        uchar location
53        uchar tube
54endStructure
55
56
57
58//
59//
60Function V_testBitShift()
61
62//      // /l=64 bit, /U=unsigned
63//      Make/L/U/N=100 eventWave
64//      eventWave = 0
65       
66        // for each 64-bit value:
67        // byte 1: tube index [0,191]
68        // byte 2: pixel value [0,127]
69        // bytes 3-8 (= 6 bytes): time stamp in resolution unit
70       
71        int64 i64_num,b1,b2,b3,b4,b5,b6,b7,b8
72        int64 i64_ticks,i64_start
73       
74        b1=255
75        b3=255
76        b5=255
77        b7=255
78        b2=0
79        b4=0
80        b6=0
81        b8=0
82       
83        b7 = b7 << 8
84        b6 = b6 << 16
85        b5 = b5 << 24
86        b4 = b4 << 32
87        b3 = b3 << 40
88        b2 = b2 << 48
89        b1 = b1 << 56
90       
91        i64_num = b1+b2+b3+b4+b5+b6+b7+b8
92        printf "%64b\r",i64_num
93       
94        return(0)
95End
96
97Function V_MakeFakeEvents()
98
99//      // /l=64 bit, /U=unsigned
100        Make/O/L/U/N=10 smallEventWave
101        smallEventWave = 0
102       
103        // for each 64-bit value:
104        // byte 1: tube index [0,191]
105        // byte 2: pixel value [0,127]
106        // bytes 3-8 (= 6 bytes): time stamp in resolution unit
107       
108        uint64 i64_num,b1,b2,b3,b4,b5,b6,b7,b8
109        uint64 i64_ticks,i64_start
110       
111        b1 = 47
112        b2 = 123
113        i64_ticks = 123456789
114
115
116        b2 = b2 << 48
117        b1 = b1 << 56
118       
119        i64_num = b1+b2+i64_ticks
120        printf "%64b\r",i64_num
121        print i64_num
122       
123        smallEventWave[0] = i64_num
124       
125        return(0)
126End
127
128Function V_decodeFakeEvent()
129
130        WAVE w = smallEventWave
131        uint64 val,b1,b2,btime
132        val = w[0]
133       
134//      printf "%64b\r",w[0]            //wrong (drops the last Å 9 bits)
135        printf "%64b\r",val                     //correct, assign value to 64bit variable
136//      print w[0]                              //wrong
137        print val                               // correct
138       
139        b1 = (val >> 56 ) & 0xFF                        // = 255, last byte, after shifting
140        b2 = (val >> 48 ) & 0xFF
141       
142        btime = val & 0xFFFFFFFFFFFF    // = really big number, last 6 bytes
143       
144        print b1
145        print b2
146        print btime
147
148
149//      //test as struct
150//      Print "as STRUCT"
151//     
152//      STRUCT eventWord s
153//     
154//      s = w[0]
155//     
156//      print s.tube
157//      print s.location
158//      print s.eventTime
159       
160
161               
162        return(0)
163End
164
165
166Function V_MakeFakeEventWave()
167
168        Variable num,ii
169
170        num = 1000
171       
172//      // /l=64 bit, /U=unsigned
173        Make/O/L/U/N=(num) eventWave
174        eventWave = 0
175       
176        // for each 64-bit value:
177        // byte 1: tube index [0,191]
178        // byte 2: pixel value [0,127]
179        // bytes 3-8 (= 6 bytes): time stamp in resolution unit
180       
181        uint64 i64_num,b1,b2,b3,b4,b5,b6,b7,b8
182        uint64 i64_ticks,i64_start
183       
184        i64_start = ticks
185        for(ii=0;ii<num;ii+=1)
186                sleep/T 6                       // 6 ticks, approx 0.1 s (without the delay, the loop is too fast)
187                b1 = trunc(abs(enoise(192)))            //since truncated, need 192 as highest random to give 191 after trunc
188                b2 = trunc(abs(enoise(128)))            // same here, to get results [0,127]
189               
190                i64_ticks = ticks-i64_start
191               
192                b2 = b2 << 48
193                b1 = b1 << 56
194       
195                i64_num = b1+b2+i64_ticks
196                eventWave[ii] = i64_num
197        endfor
198       
199        return(0)
200End
201
202
203Function V_decodeFakeEventWave()
204
205        WAVE w = eventWave
206        uint64 val,b1,b2,btime
207        val = w[0]
208       
209//      printf "%64b\r",w[0]            //wrong (drops the last Å 9 bits)
210//      printf "%64b\r",val                     //correct, assign value to 64bit variable
211//      print w[0]                              //wrong
212//      print val                               // correct
213       
214        Variable num,ii
215        num=numpnts(w)
216       
217        Make/O/L/U/N=(num) eventTime
218        Make/O/U/B/N=(num) tube,location                //8 bit unsigned
219       
220        for(ii=0;ii<num;ii+=1)
221                val = w[ii]
222               
223                b1 = (val >> 56 ) & 0xFF                        // = 255, last two bytes, after shifting
224                b2 = (val >> 48 ) & 0xFF
225       
226                btime = val & 0xFFFFFFFFFFFF    // = really big number, last 6 bytes
227
228                tube[ii] = b1
229                location[ii] = b2
230                eventTime[ii] = btime
231               
232        endfor
233               
234        return(0)
235End
236
237
238Function V_writeFakeEventFile(fname)
239        String fname
240
241        WAVE w = eventWave
242        Variable refnum
243       
244        String vsansStr="VSANS"
245        Variable revision = 11
246        Variable offset = 22            // no disabled tubes
247        Variable time1 = 2017
248        Variable time2 = 0525
249        Variable time3 = 1122
250        Variable time4 = 3344           // these 4 time pieces are supposed to be 8 bytes total
251        String detStr = "M"
252        Variable volt = 1500
253        Variable resol = 1000
254       
255       
256        Open refnum as fname
257
258        FBinWrite refnum, vsansStr
259        FBinWrite/F=2/U refnum, revision
260        FBinWrite/F=2/U refnum, offset
261        FBinWrite/F=2/U refnum, time1
262        FBinWrite/F=2/U refnum, time2
263        FBinWrite/F=2/U refnum, time3
264        FBinWrite/F=2/U refnum, time4
265        FBinWrite refnum, detStr
266        FBinWrite/F=2/U refnum, volt
267        FBinWrite/F=2/U refnum, resol
268
269        FGetPos refnum
270        Print V_filePos
271       
272        FBinWrite refnum, w
273       
274        close refnum
275       
276        return(0)
277End
278
279//
280// use GBLoadWave to do the reading, then I can do the decoding
281//
282Function V_readFakeEventFile()
283
284        String fname
285// this reads in uint64 data, to a unit64 wave, skipping 22 bytes       
286//      GBLoadWave/B/T={192,192}/W=1/S=22
287        Variable num,refnum
288
289// so to read:
290//
291// - get the file name
292//      - read the header (all of it, since I need parts of it) (maybe read as a struct? but I don't know the size!)
293// - move to EOF and close
294//
295// - Use GBLoadWave to read it in
296
297        String vsansStr=""
298        Variable revision
299        Variable offset         // no disabled tubes
300        Variable time1
301        Variable time2
302        Variable time3
303        Variable time4          // these 4 time pieces are supposed to be 8 bytes total
304        String detStr=""
305        Variable volt
306        Variable resol
307
308        vsansStr = PadString(vsansStr,5,0x20)           //pad to 5 bytes
309        detStr = PadString(detStr,1,0x20)                               //pad to 1 byte
310        Open/R refnum
311        fname = S_fileName
312
313        FBinRead refnum, vsansStr
314        FBinRead/F=2/U refnum, revision
315        FBinRead/F=2/U refnum, offset
316        FBinRead/F=2/U refnum, time1
317        FBinRead/F=2/U refnum, time2
318        FBinRead/F=2/U refnum, time3
319        FBinRead/F=2/U refnum, time4
320        FBinRead refnum, detStr
321        FBinRead/F=2/U refnum, volt
322        FBinRead/F=2/U refnum, resol
323
324        FStatus refnum
325        FSetPos refnum, V_logEOF
326       
327        Close refnum
328       
329// number of data bytes
330        num = V_logEOF-offset
331        Print num/8
332       
333        GBLoadWave/B/T={192,192}/W=1/S=22 fname
334       
335       
336        return(0)
337End
Note: See TracBrowser for help on using the repository browser.