DRS4 Forum
  DRS4 Discussion Forum  Not logged in ELOG logo
Message ID: 916     Entry time: Thu Mar 27 15:53:10 2025
Author: Justin Tabbett 
Subject: Noisy counts with adapted drs_exam.cpp 

Greetings, 

I have adapted the drs_exam.cpp to allow for a user input number of channels and trigger levels.

The program mostly works well, however there are counts which form a noise peak, imposed on the regular channel response.

To illustrate, I acquired 10,000 counts (measuring peak to peak) with the drsosc, and with my adapted script, with two channels and OR trigger logic.

 

Is there something missing in my code that could explain the cause of this noise peak? I have attached the .cpp file.

Many thanks,

Justin

Attachment 1: Channel_1_2.png  37 kB  | Show | Hide all | Show all
Attachment 2: drs_exam.cpp  5 kB  | Hide | Hide all | Show all
/********************************************************************\

  Name:         drs_exam.cpp
  Created by:   Stefan Ritt

  Contents:     Simple example application to read out a DRS4
                evaluation board

  $Id: drs_exam.cpp 21308 2014-04-11 14:50:16Z ritt $

\********************************************************************/

#include <math.h>

#ifdef _MSC_VER

#include <windows.h>

#elif defined(OS_LINUX)

#define O_BINARY 0

#include <unistd.h>
#include <ctype.h>
#include <sys/ioctl.h>
#include <errno.h>

#define DIR_SEPARATOR '/'

#endif

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "strlcpy.h"
#include "DRS.h"

#include <math.h>

#include <cstdio>

#include <fcntl.h>
#include <time.h>
#include <sys/stat.h>
#include <assert.h>
#include <iostream>
#include <chrono>
#include <vector>
#include <algorithm>
#include <iostream>
#include <conio.h>  // For _kbhit() and _getch() functions (Windows only)

/*------------------------------------------------------------------*/

int main()
{
    int i, j, k, l, m, n, p, numC, nBoards, i_start, i_end, csel;
    char str[256], param[10][100];
    DRS* drs;
    DRSBoard* b;
    float time_array[8][1024];
    float wave_array[8][1024];
    FILE* f;
    FILE* fph;

    /* do initial scan */
    drs = new DRS();

    /* show any found board(s) */
    for (i = 0; i < drs->GetNumberOfBoards(); i++) {
        b = drs->GetBoard(i);
        printf("Found DRS4 evaluation board, serial #%d, firmware revision %d\n",
            b->GetBoardSerialNumber(), b->GetFirmwareVersion());
    }

    /* exit if no board found */
    nBoards = drs->GetNumberOfBoards();
    if (nBoards == 0) {
        printf("No DRS4 evaluation board found\n");
        return 0;
    }

    /* continue working with first board only */
    b = drs->GetBoard(0);

    /* initialize board */
    b->Init();

    /* set sampling frequency */
    b->SetFrequency(5, true);

    /* enable transparent mode needed for analog trigger */
    b->SetTranspMode(1);

    /* set input range to -0.5V ... +0.5V */
    b->SetInputRange(0);

    /* use following line to turn on the internal 100 MHz clock connected to all channels  */
    b->EnableTcal(1);

    printf("Number of channels: ");
    fgets(str, sizeof(str), stdin);
    csel = atoi(str); //Number of channels
    printf("DRS4 configured for %d channels\n", atoi(str));
    
    b->EnableTrigger(1, 0);
    b->SetTriggerSource(15); // 15 is an OR on CH1-4, 3 is OR on CH1-2, apparently 768 is the AND value for CH1 and CH2 
    b->SetTriggerPolarity(true);        // positive edge -> false, negative edge -> true

    std::vector<int> avn = { 1, 2, 3, 4 };  // Available number of channels: 0, 2, 4, 6
    std::vector<double> triglist = { 0, 0, 0, 0 }; //Store trigger thresholds
    for (i = 0;i < csel;i++) {
        printf("Channel %d trigger level (V): ", avn[i]);
        fgets(str, sizeof(str), stdin);
        b->SetIndividualTriggerLevel((avn[i] - 1), atof(str));
        triglist[i] = atof(str) * -1000; //store as mV
        printf("Trigger level set to %1.3lf Volt\n", atof(str));
    }
    b->SetTriggerDelayNs(0);             // zero ns trigger delay

    fph = fopen("fph.txt", "w");
    if (fph == NULL) {
        perror("ERROR: Cannot open file \"fph.txt\"");
        return 1;
    }
    
    numC = 0; //Event counter

    for (i = 0;i < csel;i++) {
        fprintf(fph, "%7.1f,", triglist[i]);
    }

    fprintf(fph, "%d\n", 0);
    printf("hi\n");
    while (true) {
        // Check if a key is pressed
        if (_kbhit()) {
            char ch = _getch();  // Read the pressed key without waiting for Enter
            if (ch == 'q')       // If 'q' is pressed, exit the loop
                break;
        }

        k = 0;
        l = 0;
        m = 0;
        n = 0;

        std::vector<double> outh(csel, 0); //Create output for pulse heights as 0s
        printf("Before startdomino\n");
        b->StartDomino();

        /* wait for trigger */
        printf("Before fflushstdout\n");
        fflush(stdout);
        while (b->IsBusy());

        /*Take time stamp*/

        auto now = std::chrono::system_clock::now();
        auto duration = std::chrono::duration<double>(now.time_since_epoch());
        double timeT = duration.count();
        
        /*Read waveforms*/

        b->TransferWaves(0, 8);

        for (p = 0;p < csel;p++) {
            b->GetWave(0, p * 2, wave_array[p]); // this reads channel i*2 to array index i
            float minVali = wave_array[p][0], maxVali = wave_array[p][0]; //creates floats for min and max values
            for (i = 0; i < 1024; i++) {
                // find the min and max values from each channel
                if (wave_array[p][i] < minVali) minVali = wave_array[p][i];
                if (wave_array[p][i] > maxVali) maxVali = wave_array[p][i];
            }
            float heighti = maxVali - minVali;
            outh[p] = heighti;
        }

        for (double value : outh) {
            fprintf(fph, "%7.1f,", value);
        }

        fprintf(fph, "%.6f\n", timeT);
        printf("%d\n", numC);

      
        if (numC % 1000 == 0) {
            fflush(fph);
        }
        numC++;



    }

    fclose(fph);
    std::getchar();

    /* delete DRS object -> close USB connection */
    delete drs;
}
ELOG V3.1.5-3fb85fa6