Suppose, we want to to calculate the mean value and of our 16
imaginary data values coming in, and print them out to the screen. If
we denote the values belonging to a single event with
DUMMY[0]
to DUMMY[15]
, the following user task calculates
the above quantity for each event (create it with an editor - remember
the ;
at the end of each statement, and save it under the
filename dummy.hoc
):
/* procedure init. called once when this task is loaded */ proc init() { select DUMMY; /* look only for data of signature DUMMY */ } /* procedure analyze. called every time a DUMMY event is coming in */ proc analyze() { for(i = sum = 0; i < 16; i++) /* loop over all array elements */ sum += DUMMY[i]; mean = sum / 16; /* calculate the mean value */ printf("mean value is: %f\n", mean); /* print out the mean value */ }
Let's take a closer look at this file. At first glance, it looks like
C
, but some things are special. There is no main()
function, instead there are two subroutines here denoted init()
and analyze()
. For each task, the convention is that the
procedure init()
is run once whenever the task is loaded into the
YODA interpreter, whereas the procedure analyze()
is run each
time a matching event occurs in the incoming datastream.
There is also an optional procedure finish()
, which is called
each time the data processing is halted (either manually, or at the
end of the input file).
How does YODA know when to call the procedure analyze()
? The
init()
procedure contains the statement select DUMMY;
,
which does two things:
DUMMY
line in the selector database (see
previous section), and (if found) remembers that this task is to be called
whenever the DUMMY
data is found in the incoming datastream.
analyze()
in form of an array
with the same name as the selector (i.e. DUMMY
. Addressing the
elements of an array is done just as in C
, that means with
square brackets ([]
).
So the effect of this select
statement is, that each time DUMMY
data are found, analyze()
is called, and at the same time the
DUMMY[]
-array contains the actual values. For the next incoming
DUMMY
-data, the array values are updated, analyze()
is called
again, and so on.
The rest of the file is not hard to understand. Note that all
variables (and arrays) in the analysis task are of type double
,
no matter what the type of the input data (as described in the
selector database) is. So the format specification in the
printf
-statement is of floating-point type, This must even be
used for ''integer-looking'' variables as i
or j
.
Now we are ready to try this dummy-analysis.