EventLoaderCLICpix.cpp 4.79 KB
Newer Older
Simon Spannagel's avatar
Simon Spannagel committed
1
#include "EventLoaderCLICpix.h"
2

3
using namespace corryvreckan;
4
using namespace std;
5

Simon Spannagel's avatar
Simon Spannagel committed
6
7
8
EventLoaderCLICpix::EventLoaderCLICpix(Configuration config, std::shared_ptr<Detector> detector)
    : Module(std::move(config), detector) {
    m_detector = detector;
Simon Spannagel's avatar
Simon Spannagel committed
9
    m_filename = "";
10
11
}

Simon Spannagel's avatar
Simon Spannagel committed
12
void EventLoaderCLICpix::initialise() {
Simon Spannagel's avatar
Simon Spannagel committed
13
    // File structure is RunX/CLICpix/RunX.dat
14

Simon Spannagel's avatar
Simon Spannagel committed
15
    // Take input directory from global parameters
16
    string inputDirectory = m_config.getPath("input_directory");
17

Simon Spannagel's avatar
Simon Spannagel committed
18
19
    // Open the root directory
    DIR* directory = opendir(inputDirectory.c_str());
20
    if(directory == nullptr) {
Simon Spannagel's avatar
Simon Spannagel committed
21
22
23
24
25
26
        LOG(ERROR) << "Directory " << inputDirectory << " does not exist";
        return;
    }
    dirent* entry;

    // Read the entries in the folder
27
    while((entry = readdir(directory))) {
Simon Spannagel's avatar
Simon Spannagel committed
28
29
30
31
32
        // Check for the data file
        string filename = inputDirectory + "/" + entry->d_name;
        if(filename.find(".dat") != string::npos) {
            m_filename = filename;
        }
33
    }
34

Simon Spannagel's avatar
Simon Spannagel committed
35
36
37
    // If no data was loaded, give a warning
    if(m_filename.length() == 0)
        LOG(WARNING) << "No data file was found for CLICpix in " << inputDirectory;
38

Simon Spannagel's avatar
Simon Spannagel committed
39
40
    // Open the data file for later
    m_file.open(m_filename.c_str());
41

Simon Spannagel's avatar
Simon Spannagel committed
42
43
44
45
    // Make histograms for debugging
    hHitMap = new TH2F("hitMap", "hitMap", 64, 0, 64, 64, 0, 64);
    hPixelToT = new TH1F("pixelToT", "pixelToT", 20, 0, 20);
    hShutterLength = new TH1F("shutterLength", "shutterLength", 3000, 0, 0.3);
46
    hPixelMultiplicity = new TH1F("pixelMultiplicity", "Pixel Multiplicity; # pixels; # events", 4100, 0, 4100);
47
48
}

49
StatusCode EventLoaderCLICpix::run(std::shared_ptr<Clipboard> clipboard) {
50

Simon Spannagel's avatar
Simon Spannagel committed
51
52
53
    // If have reached the end of file, close it and exit program running
    if(m_file.eof()) {
        m_file.close();
54
        return StatusCode::Failure;
Simon Spannagel's avatar
Simon Spannagel committed
55
    }
56

Simon Spannagel's avatar
Simon Spannagel committed
57
58
59
60
    // Otherwise load a new frame

    // Pixel container, shutter information
    Pixels* pixels = new Pixels();
61
    double shutterStartTime = 0, shutterStopTime = 0;
Simon Spannagel's avatar
Simon Spannagel committed
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
    string data;

    int npixels = 0;
    // Read file and load data
    while(getline(m_file, data)) {

        //    LOG(TRACE) <<"Data: "<<data;

        // If line is empty then we have finished this event, stop looping
        if(data.length() < 5)
            break;

        // Check if this is a header/shutter/power info
        if(data.find("PWR_RISE") != string::npos || data.find("PWR_FALL") != string::npos)
            continue;
        if(data.find("SHT_RISE") != string::npos) {
            // Read the shutter start time
            long int timeInt;
            string name;
            istringstream header(data);
            header >> name >> timeInt;
83
            shutterStartTime = static_cast<double>(timeInt) / (0.04);
84
            LOG(TRACE) << "Shutter rise time: " << Units::display(shutterStartTime, {"ns", "us", "s"});
Simon Spannagel's avatar
Simon Spannagel committed
85
86
87
88
89
90
91
92
            continue;
        }
        if(data.find("SHT_FALL") != string::npos) {
            // Read the shutter stop time
            long int timeInt;
            string name;
            istringstream header(data);
            header >> name >> timeInt;
93
            shutterStopTime = static_cast<double>(timeInt) / (0.04);
94
            LOG(TRACE) << "Shutter fall time: " << Units::display(shutterStopTime, {"ns", "us", "s"});
Simon Spannagel's avatar
Simon Spannagel committed
95
96
97
98
99
100
101
102
103
104
105
106
            continue;
        }

        // Otherwise load data
        int row, col, counter, tot(0);
        LOG(TRACE) << "Pixel data: " << data;
        istringstream pixelData(data);
        pixelData >> col >> row >> counter >> tot;
        tot++;
        row = 63 - row;
        LOG(TRACE) << "New pixel: " << col << "," << row << " with tot " << tot;

107
108
109
110
        if(col >= m_detector->nPixels().X() || row >= m_detector->nPixels().Y()) {
            LOG(WARNING) << "Pixel address " << col << ", " << row << " is outside of pixel matrix.";
        }

Simon Spannagel's avatar
Simon Spannagel committed
111
        // If this pixel is masked, do not save it
Simon Spannagel's avatar
Simon Spannagel committed
112
        if(m_detector->masked(col, row))
Simon Spannagel's avatar
Simon Spannagel committed
113
            continue;
114
115
116

        // when calibration is not available, set charge = tot
        Pixel* pixel = new Pixel(m_detector->name(), col, row, tot, tot, 0);
Simon Spannagel's avatar
Simon Spannagel committed
117
118
119
120
121
        pixels->push_back(pixel);
        npixels++;
        hHitMap->Fill(col, row);
        hPixelToT->Fill(tot);
    }
122

Simon Spannagel's avatar
Simon Spannagel committed
123
    // Now set the event time so that the Timepix3 data is loaded correctly
Simon Spannagel's avatar
Simon Spannagel committed
124
    clipboard->put_event(std::make_shared<Event>(shutterStartTime, shutterStopTime));
125

Simon Spannagel's avatar
Simon Spannagel committed
126
127
128
    LOG(TRACE) << "Loaded " << npixels << " pixels";
    // Put the data on the clipboard
    if(pixels->size() > 0)
Simon Spannagel's avatar
Simon Spannagel committed
129
        clipboard->put(m_detector->name(), "pixels", reinterpret_cast<Objects*>(pixels));
130

Simon Spannagel's avatar
Simon Spannagel committed
131
    // Fill histograms
132
    hPixelMultiplicity->Fill(npixels);
Simon Spannagel's avatar
Simon Spannagel committed
133
    hShutterLength->Fill(shutterStopTime - shutterStartTime);
134

Simon Spannagel's avatar
Simon Spannagel committed
135
    // Return value telling analysis to keep running
136
    return StatusCode::Success;
137
138
}

Simon Spannagel's avatar
Simon Spannagel committed
139
void EventLoaderCLICpix::finalise() {
140

Simon Spannagel's avatar
Simon Spannagel committed
141
    LOG(DEBUG) << "Analysed " << m_eventNumber << " events";
142
}