CLICpixEventLoader.C 4.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
#include "CLICpixEventLoader.h"

CLICpixEventLoader::CLICpixEventLoader(bool debugging)
: Algorithm("CLICpixEventLoader"){
  debug = debugging;
  m_filename = "";
}


void CLICpixEventLoader::initialise(Parameters* par){
11
  
12
13
14
  parameters = par;
  
  // File structure is RunX/CLICpix/RunX.dat
15
  
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
  // Take input directory from global parameters
  string inputDirectory = parameters->inputDirectory + "/CLICpix";
  
  // Open the root directory
  DIR* directory = opendir(inputDirectory.c_str());
  if (directory == NULL){tcout<<"Directory "<<inputDirectory<<" does not exist"<<endl; return;}
  dirent* entry; dirent* file;
  
  // Read the entries in the folder
  while (entry = readdir(directory)){
    // Check for the data file
    string filename = inputDirectory + "/" + entry->d_name;
    if(filename.find(".dat") != string::npos){
      m_filename = filename;
    }
  }
  
  // If no data was loaded, give a warning
  if(m_filename.length() == 0) tcout<<"Warning: no data file was found for CLICpix in "<<inputDirectory<<endl;
  
  // Open the data file for later
  m_file.open(m_filename.c_str());
38
39
40
41
42
43
  
  // 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);
  hPixelsPerFrame = new TH1F("pixelsPerFrame","pixelsPerFrame",4100,0,4100);
44
45
46
47
48
}


StatusCode CLICpixEventLoader::run(Clipboard* clipboard){
  
49
//  tcout<<"Running"<<endl;
50
51
52
53
54
55
56
57
58
59
60
61
  // Assume that the CLICpix is the DUT (if running this algorithm
  string detectorID = parameters->DUT;
		
  // If have reached the end of file, close it and exit program running
  if(m_file.eof()){
    m_file.close();
    return Failure;
  }
  
  // Otherwise load a new frame
  
  // Pixel container, shutter information
62
  Pixels* pixels = new Pixels();
63
64
65
  long double shutterStartTime, shutterStopTime;
  string data;
  
66
  int npixels=0;
67
  // Read file and load data
68
69
70
  while(getline(m_file,data)){
    
//    tcout<<"Data: "<<data<<endl;
71
72
    
    // If line is empty then we have finished this event, stop looping
73
    if(data.length() < 5) break;
74
75
76
77
78
79
80
81
    
    // 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;
82
83
      shutterStartTime = (double)timeInt/(40000000.);
//      tcout<<"Shutter rise time: "<<shutterStartTime<<endl;
84
85
86
87
88
89
90
      continue;
    }
    if(data.find("SHT_FALL") != string::npos){
      // Read the shutter stop time
      long int timeInt; string name;
      istringstream header(data);
      header >> name >> timeInt;
91
92
      shutterStopTime = (double)timeInt/(40000000.);
//      tcout<<"Shutter fall time: "<<shutterStopTime<<endl;
93
94
95
96
      continue;
    }
    
    // Otherwise load data
97
    int row, col, counter, tot(0);
98
    long int time;
99
//    tcout<<"Pixel data: "<<data<<endl;
100
    istringstream pixelData(data);
101
102
103
104
    pixelData >> col >> row >> counter >> tot;
    tot++;
    row=63-row;
//    tcout<<"New pixe: "<<col<<","<<row<<" with tot "<<tot<<endl;
105
106
107

    // If this pixel is masked, do not save it
    if(parameters->detector[detectorID]->masked(col,row)) continue;
108
    Pixel* pixel = new Pixel(detectorID,row,col,tot,0);
109
    pixels->push_back(pixel);
110
111
112
113
    npixels++;
    hHitMap->Fill(col,row);
    hPixelToT->Fill(tot);
    
114
115
116
117
118
  }
  
  // Now set the event time so that the Timepix3 data is loaded correctly
  parameters->currentTime = shutterStartTime;
  parameters->eventLength = (shutterStopTime-shutterStartTime);
119
120
  
//  tcout<<"Loaded "<<npixels<<" pixels"<<endl;
121
  // Put the data on the clipboard
122
123
124
125
126
127
  if(pixels->size() > 0) clipboard->put(detectorID,"pixels",(TestBeamObjects*)pixels);
  
  // Fill histograms
  hPixelsPerFrame->Fill(npixels);
  hShutterLength->Fill(shutterStopTime-shutterStartTime);
  
128
129
130
131
132
133
134
135
136
  // Return value telling analysis to keep running
  return Success;
}

void CLICpixEventLoader::finalise(){
  
  if(debug) tcout<<"Analysed "<<m_eventNumber<<" events"<<endl;
  
}