EventLoaderEUDAQ.cpp 4.1 KB
Newer Older
1
#include "EventLoaderEUDAQ.h"
2
3
4
5
6
7
8
#include "eudaq/PluginManager.hh"

#include <algorithm>

using namespace corryvreckan;
using namespace std;

9
EventLoaderEUDAQ::EventLoaderEUDAQ(Configuration config, std::vector<std::shared_ptr<Detector>> detectors)
10
    : Module(std::move(config), std::move(detectors)), m_longID(true) {
11

12
    m_filename = m_config.getPath("file_name", true);
13
14
15
    m_longID = m_config.get<bool>("long_detector_id", true);
}

16
void EventLoaderEUDAQ::initialise() {
17
18
19
20
21

    // Create new file reader:
    try {
        reader = new eudaq::FileReader(m_filename, "");
    } catch(...) {
22
        throw ModuleError("Unable to read input file \"" + m_filename + "\"");
23
24
25
    }
}

26
StatusCode EventLoaderEUDAQ::run(std::shared_ptr<Clipboard> clipboard) {
27
28
29
30
31

    // Read next event from EUDAQ reader:
    const eudaq::DetectorEvent& evt = reader->Event();
    LOG(TRACE) << evt;

32
33
34
35
36
37
38
    // Convert timestamp to nanoseconds, using
    // TLU frequency: 48.001e6 Hz
    // TLU frequency multiplier: 8
    // FIXME cross-check that this conversion is correct
    auto timestamp = Units::get(static_cast<double>(evt.GetTimestamp()) / (48.001e6 * 8), "s");
    LOG(DEBUG) << "EUDAQ event " << evt.GetEventNumber() << " at " << Units::display(timestamp, {"ns", "us"});

39
40
41
42
43
44
    if(evt.IsBORE()) {
        // Process begin-of-run
        LOG(DEBUG) << "Found BORE";
        try {
            eudaq::PluginManager::Initialize(evt);
        } catch(const eudaq::Exception&) {
45
            throw ModuleError("Unknown event types encountered");
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
        }
    } else if(evt.IsEORE()) {
        LOG(INFO) << "Found EORE";
    } else {
        eudaq::StandardEvent sevt = eudaq::PluginManager::ConvertToStandard(evt);

        for(size_t iplane = 0; iplane < sevt.NumPlanes(); ++iplane) {
            // Get EUDAQ StandardPlane
            const eudaq::StandardPlane& plane = sevt.GetPlane(iplane);

            // Build Corryvreckan detector ID and check if this detector should be read:
            std::string detectorID =
                (m_longID ? (plane.Sensor() + "_") : "plane") + std::to_string(plane.ID()); // : (plane.ID()));

            if(!has_detector(detectorID)) {
                LOG(DEBUG) << "Skipping unknown detector " << detectorID;
                continue;
            }

            auto detector = get_detector(detectorID);

            // Make a new container for the data
68
            auto deviceData = std::make_shared<PixelVector>();
69
70
71
            for(unsigned int ipix = 0; ipix < plane.HitPixels(); ++ipix) {
                auto col = static_cast<int>(plane.GetX(ipix));
                auto row = static_cast<int>(plane.GetY(ipix));
72

73
                if(col >= detector->nPixels().X() || row >= detector->nPixels().Y()) {
74
                    LOG(WARNING) << "Pixel address " << col << ", " << row << " is outside of pixel matrix.";
75
76
                }

77
78
79
80
81
82
                // Check if this pixel is masked
                if(detector->masked(col, row)) {
                    LOG(TRACE) << "Detector " << detectorID << ": pixel " << col << "," << row << " masked";
                    continue;
                }

83
84
85
                // when calibration is not available, set charge = tot, timestamp not available -> set to 0
                Pixel* pixel =
                    new Pixel(detectorID, col, row, static_cast<int>(plane.GetPixel(ipix)), plane.GetPixel(ipix), 0.);
86
87
88

                // Pixel gets timestamp of trigger assigned:
                pixel->timestamp(timestamp);
89
90
91
92
                deviceData->push_back(pixel);
            }

            // Store on clipboard
93
            clipboard->putData(deviceData, detectorID);
94
95
96
        }
    }

97
98
99
    // Store event time on clipboard for subsequent modules
    // FIXME assumes trigger in center of two Mimosa26 frames:
    auto frame_length = Units::get(115.2, "us");
100
    clipboard->putEvent(std::make_shared<Event>(timestamp - frame_length, timestamp + frame_length));
101
102
103
104

    // Advance to next event if possible, otherwise end this run:
    if(!reader->NextEvent()) {
        LOG(INFO) << "No more events in data stream.";
105
        return StatusCode::Failure;
106
107
108
    };

    // Return value telling analysis to keep running
109
    return StatusCode::Success;
110
}