Programs developed for measuring and analyzing data
taken from a scaler box connected to some nuclear detector
(Géza Székely 9-31 May 2002, szekely@atomki.hu)
Instruments
A 6-channel scaler [1] box developed in Atomki is connected to the PC's serial port COM1 or COM2. The box can accept + or - 5 Volt from an external detector device through 3+3 lemo connectors. 3 connectors can accept +5V TTL signals and the other 3 ones can accept -5V NIM signals.
Software environment for running the programs on a PC
The programs are running under DOS. It can be a standalone op.system, or a DOS-window inside Win9x op.system. When used on a pure DOS system, first one has to give:
> cwsdpmi /p
command in order to have the external memory mapped for the user
programs. In a DOS-window it is automatically available.
Description of the PC programs
==================================================================
PROGRAM: CNT1G.C
PURPOSE: RS232 communication with a BS2 to collect detector
data
INPUT: command line option:
s - for simulate measurement
1 or 2 - use COM1 or COM2
OUTPUT: Commands to BS2:
R - Start counter
S - Stop counter
D - Get measured count
i - Get firmware identification text
LANG: DJGPP gcc (2.95.2)
DATE: May-2002 last update: 2002.05.28.
==================================================================
This is the head of CNT1G.C in it's source list. It does not use a mouse, only keyboard buttons are used to control it's operation.
Getting started:
The "settings" menu is selected by default when the program is started, because here the most important settings have to be done before any measurement. The available items are:
Serial communication port of PC
Input channel of scaler box [0,2]
Start counter in scaler box
Stop counter in scaler box
Get firmware ID text
Determine the serial port:
The first step has to be to determine the serial port where the scaler box is
connected. This can be done with the first item of "settings" menu.
Select input channel of scaler box:
The built-in software can handle only one input channel of the box, but
one can use any of the available 6 input lemo connectors. The default
value is TTL IN0, in this case this menu item can be skipped.
Check communication with the scaler box:
The last 3 items are able to confirm, that the communication is OK.
For the start and stop commands we have to get "done" response,
while for activating the last menu item one has to get the firmware ID text
inside the scaler box. This is recently:
"CNT1N 2002-MAY-29 szekely@atomki.hu"
Setting measurement parameters:
After leaving the "settings" menu with <Esc>, the measurement parameters have to be set within the "measure" menu:
Dwell time in sec
Duration of meas. in sec
Starting display mode
Start data acquisition?
Duration of measuring one data (dwell time):
With the first menu item one can give the time needed for collecting counts
from the detector for a single data entry. The dimension is seconds and it has
to lie within [1,600] interval.
Duration of the whole measurement:
With the 2-nd menu item one can determine how long time is needed to do the
whole measurement. The dimension is also seconds and the limits are [1,28800].
However this data is not independent of the dwell time, because the number of
data for storage is also limited by 4096. This means that if you want to
measure for 8 hours (=28800 sec), the dwell time should be at least
28800/4096=7.
Setting the display mode:
The 3-rd menu item is able to toggle between table and graph displaying mode.
The same effect can be reached with the <D> hot-key any time. In table
mode the measured data are displayed as numbers in a 20-row table. In graph
mode a 2-dimensional coordinate system is drawn having the time in seconds in
the horizontal and the collected counts in the vertical axis. The vertical
axis can be varied between linear and logarithmic scale.
Start of actual measurement:
In the last item of "measure" menu one can determine whether the measurement
should start at one (YES answer) or it will be initiated later (NO answer).
If the answer here was YES, then the screen is changed immediately to the
selected display mode and the timer is started to wait for the first data from
the scaler box. In the top right corner one can see the decreasing timer
ticks uring the individual measuring intervals.

Results of a simulated measurement
What to do during the measurement?
When the measurement is going on the following tools can be used.
What to do with the measured data?
Save data:
When the measurement was stopped by hand or by itself, an immediate
question appears on the center of the screen:
Save spectrum in file: 12345678.spe
Here one can give other name of the output file which will contain all
the data in a text file in 2 columns. The first column contains the relative
time of the measurement in seconds with 2 decimals, the second column contains
the measured 16-bit counts as integer numbers between 0 and 65535.
Data in one line means that during the time elapsed since the previous time
point that many hits were collected from the detector. Even when this
immediate question is answered with pushing <Esc>, later one can save
the data by activating the "save data" top menu item.
Save screen:
If one wants to save the picture on the screen, the "save as gif" top menu
item has to be activated, or <G> hot-key used.
Save screen image as [.gif] :
The above question together with a suggested filename appears then and the screen image is saved in a compressed GIF'87 format, which can be used by several other graphical viewing program for example for printing. Only 16 colors are used in the color palette.
Online help
When this info is not available, one can always push F1 to get short instructions for handling the program. The only exception is when a measurement is on. In this case the timing could have been damaged.
Exit from the programThe last top menu item is for exiting from the program. With the <Esc> button the same effect can be reached, but sometimes it has to be pushed several times, when coming from a submenu or a running measurement.
Possible error messages during run
The most typical error message could be the following:
"Could not get 2 data bytes to D command from BS2!"
It is generated when the PC can not handle the answer of BS2 given to the "D" command. As the user program (CNT1G) is not using interrupts for the serial communication, it could happen in a complex software environment, that some external event can cause an interrupt, which takes over the CPU from the user program just between sending the "D" command and receiving the answer for it. Because of this possible interrupt it is advisable to run the code in a real measurement case without having any network software in Windows or even to run it in DOS.
Working in simulation mode
For testing purposes during the development a simulation working mode was used which can be initiated by the "s" command line parameter (> cnt1g s). In this case the program does not want to communicate with the scaler box, only imitating that. During the "measurement" it generates a typical decay data consisting of 2 components with the following parameters:
I1 = 8350, l1 = 0.0253, I2 = 1650, l2 = 0.00428
(see (1) formula in the next page). These parameters were taken from a real measurement, the corresponding half-life times coming from the T=ln(2)/ l formula are
T1 = 27.32 sec, T2 = 161.89 sec.
This working mode can be used to get practice with the program and also to check how the saved data can be analyzed with the DECAYFIT code (described below). The data file generated with the simulation mode should then give the above parameters back when analyzed with DECAYFIT.EXE.
==================================================================
PROGRAM: DECAYFIT.C
PURPOSE: decompose measured decay spectrum into 2 components
INPUT: ASCII data file containing data in 2 columns
1st column contains time in sec
2nd column is the counts measured since the prev.
meas.
OUTPUT: 1. View of the fit on the screen
2. A .gif file containing the screen image, when
"save as gif" option is activated
LANG: DJGPP gcc (2.95.2)
DATE: May-2002 last update: 2002.05.28.
==================================================================
This is the head of DECAYFIT.C in it's source list. Like in the previous case mouse is not used here either.
Starting the program
It needs a data file to be analysed, so one can give it in the command line (> decayfit p1.dat) or simply call the program and wait for the first question:
Text file of decay data [def=DECAYFIT.DAT] =
Here one can give the name of a data file or push <Enter> to accept the
default decayfit.dat file. It is useful when one wants to use the same data
several times.
After reading the given file, a 2-dimensional coordinate system appears on the screen. The time values in seconds are in the horizontal axis and the collected counts are in the vertical axis. The measured data values are shown as yellow circles.
How can we fit the data?
It is supposed, that our data is a composition of two decaying data where the half-life times are significantly differs.
I = I1exp(-l1(t-t0))+ I2exp(-l2(t-t0)) (1)
By utilizing this fact the second half of the data can be taken as the one
consisting of only the slower decay process.
I » I2exp(-l2(t-t0)) (2)
After applying the natural logarithm to (2) we have a set of linear equations
which can be solved with the least squares method. This gives us good
estimations I2 and L2 for I2 and l
2 respectively.Now we have to subtract the contribution of the
second term to the overall data and after the subtraction we can do a similar
fit to the first half of the modified data.
I - I2*exp(-L2*(t-t0)) » I1exp(-l1(t-t0)) (3)
This second least squares fit will give good estimates I1 and L1 for
I1 and l1 if the initial
condition was right. The top menu item "linear fit" is doing the above
calculations in 3 steps, and after each step it draws the calculated component
in the coordinate system. The steps are separated by requiring pushing a
button to continue the process.
To refine our linear fit one can use the second top menu item "nonlin.fit". It uses the results of the linear fit as initial parameter guesses and in an iterative way determines the best fit to the 2 pairs decaying parameters in the least squares sense. The blue curve shows the result of the nonlinear fit.

Results of the fitting process in logarithmic y-scale
It is not guaranteed that the iterative process always converges. When the initial values are not enough good, the consecutive corrections of the parameters could even increase their values. In this case the iteration typically ends with the message: "Singular Hesse matrix in nonlinear fit!".
What else can we do?
Linear and logarithmic scale
To view the linear nature of the logarithm of the measured data
especially in the beginning and at the end, the vertical axis can be set to
show not only linear but logarithmic scale, too. This option is available by
activating the "lin.scale" or "log.scale" menu item.
Save picture in GIF format
The picture on the screen, can be always saved with the "save as gif"
top menu item.
Save screen image as [.gif] :
The above question together with a suggested filename appears then and the
screen image is saved in a compressed GIF'87 format, which can be used by
several other graphical viewing program for example for printing. Only 16
colors are used in the color palette. The colors are inverted in the output
file in order to have white background.
Firmware - a code running on a microcontroller
The hart of the scaler box is a BS2 basic stamp microcontroller. It handles an i8253 chip as a 3-channel counter of the incoming hits as well as the serial communication with a PC. Programming a BS2 is possible by using a special program (STAMPW.EXE) on a PC. It is working as a limited development environment in BASIC language and a downloader. When the basic program is syntactically right, it is downloaded to the microcontroller through the same cable as used for the later communication between the user program and the imbedded basic program.
CNT1N.BS2 is the name of the basic program which communicates from the BS2 with CNT1G.EXE running on the PC. It can accept only the following commands:
- R = clear counter and start it
- S = stop counter
- D = read counter and send the 2 bytes back to the serial port
- 0 = select channel 0
- 1 = select channel 1
- 2 = select channel 2
- i = write the software identification text to the serial port
The modification of the basic program inside the microcontroller is possible only if the jumper on the ATN incoming line (pin#4 of DB9 connector) is set inside the scaler box. During the development it was always set, due to the frequent demand for downloading. At the end of the development phase it is worse to reset the jumper to avoid accidentally rewriting the program.
Software environment for modifying the programs on a PC
Win9x is needed with a directory called DJGPP [2], which contains the necessary editor and compiler for the gnu C++ language. The content of this directory is available on a CD (76 MByte). It is a freely available system and has the advantage of using all the available memory of the PC in DOS.
To install this development system it is enough to copy the content of this directory to c:\DJGPP, then do the following modifications on autoexec.bat system file: insert the following 2 lines
set DJGPP=C:\DJGPP\DJGPP.ENV
set PATH=C:\DJGPP\BIN;%PATH%
After this modification the PC should be rebooted.
To edit one of the codes one has to give the command:
> rhide cnt1g.gpr or > rhide decayfit.gpr
depending on which one has to be edited.
Rhide is a freely applicable interactive development environment which is very similar to the Turbo C environment. With Alt-C hot-key the program can be compiled and the syntactical errors can be viewed. After having a syntactically correct version one has to quit from rhide and give the following command line command:
> gcc cnt1g.c gifencod.o -o cnt1g.exe -lalleg
or
> gcc decayfit.c gifencod.o -o decayfit.exe -lalleg
This will create the executable version of cnt1g or decayfit.
References: