forked from eutelescope/eutelescope
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEUTelSucimaImagerReader.cc
209 lines (170 loc) · 6.66 KB
/
EUTelSucimaImagerReader.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Author Antonio Bulgheroni, INFN <mailto:antonio.bulgheroni@gmail.com>
// Version $Id$
/*
* This source code is part of the Eutelescope package of Marlin.
* You are free to use this source files for your own development as
* long as it stays in a public research context. You are not
* allowed to use it for commercial purpose. You must put this
* header with author names in all development based on this file.
*
*/
// personal includes
#include "EUTelSucimaImagerReader.h"
#include "EUTELESCOPE.h"
#include "EUTelRunHeaderImpl.h"
#include "EUTelEventImpl.h"
// marlin includes
#include "marlin/Processor.h"
#include "marlin/DataSourceProcessor.h"
#include "marlin/ProcessorMgr.h"
// lcio includes
#include <IMPL/LCEventImpl.h>
#include <IMPL/LCCollectionVec.h>
#include <IMPL/TrackerRawDataImpl.h>
#include <UTIL/CellIDEncoder.h>
#include <UTIL/LCTime.h>
// #include <UTIL/LCTOOLS.h>
// system includes
#include <fstream>
#include <memory>
#include <cstdlib>
using namespace std;
using namespace marlin;
using namespace eutelescope;
EUTelSucimaImagerReader::EUTelSucimaImagerReader ():DataSourceProcessor ("EUTelSucimaImagerReader") {
_description =
"Reads SUCIMA Imager ASCII data files and creates LCEvent with TrackerRawData collection.\n"
"Make sure to not specify any LCIOInputFiles in the steering in order to read SUCIMA Imager files.";
registerProcessorParameter ("SUCIMAImagerFileName", "Input file",
_fileName, std::string ("input.dat"));
registerProcessorParameter ("NoOfXPixel", "Number of pixels along X",
_noOfXPixel, static_cast < int >(512));
registerProcessorParameter ("NoOfYPixel", "Number of pixels along Y",
_noOfYPixel, static_cast < int >(512));
}
EUTelSucimaImagerReader * EUTelSucimaImagerReader::newProcessor () {
return new EUTelSucimaImagerReader;
}
void EUTelSucimaImagerReader::init () {
printParameters ();
}
void EUTelSucimaImagerReader::readDataSource (int numEvents) {
ifstream inputFile;
inputFile.exceptions (ifstream::failbit | ifstream::badbit);
// try to open the input file....
try {
inputFile.open (_fileName.c_str (), ios::in);
} catch (exception & e) {
message<ERROR5> ( log() << "Problem opening file " << _fileName << ". Exiting." );
exit (-1);
}
int eventNumber = 0;
int runNumber = 0;
while (true) {
if (numEvents > 0 && eventNumber + 1 > numEvents) {
break;
}
EUTelEventImpl *event = new EUTelEventImpl;
event->setDetectorName("MIMOSA");
event->setEventType(kDE);
LCTime * now = new LCTime;
event->setTimeStamp(now->timeStamp());
delete now;
LCCollectionVec *rawData = new LCCollectionVec (LCIO::TRACKERRAWDATA);
if (eventNumber % 10 == 0) message<MESSAGE5> ( log() << "Converting event " << eventNumber );
if (isFirstEvent ()) {
// in the case it is the first run so we need to process and
// write out the run header.
auto_ptr<IMPL::LCRunHeaderImpl> lcHeader ( new IMPL::LCRunHeaderImpl );
auto_ptr<EUTelRunHeaderImpl> runHeader ( new EUTelRunHeaderImpl (lcHeader.get()) );
runHeader->addProcessor( type() );
runHeader->lcRunHeader()->setDescription(" Events read from SUCIMA Imager ASCII input file: " + _fileName);
runHeader->lcRunHeader()->setRunNumber (runNumber);
runHeader->setHeaderVersion (0.0001);
runHeader->setDataType (EUTELESCOPE::CONVDATA);
runHeader->setDateTime ();
runHeader->setDAQHWName (EUTELESCOPE::SUCIMAIMAGER);
runHeader->setDAQHWVersion (0.0001);
runHeader->setDAQSWName (EUTELESCOPE::SUCIMAIMAGER);
runHeader->setDAQSWVersion (0.0001);
runHeader->addIntermediateFile (_fileName);
runHeader->addProcessor (_processorName);
// this is a mistake here only for testing....
runHeader->setNoOfEvent(100);
//////////////////////////////////////////////
runHeader->setNoOfDetector(1);
runHeader->setMinX(IntVec(1, 0));
runHeader->setMaxX(IntVec(1, _noOfXPixel - 1));
runHeader->setMinY(IntVec(1, 0));
runHeader->setMaxY(IntVec(1, _noOfYPixel - 1));
runHeader->lcRunHeader()->setDetectorName("MIMOSA");
// UTIL::LCTOOLS::dumpRunHeader(runHeader);
// process the run header
ProcessorMgr::instance ()->processRunHeader ( static_cast<lcio::LCRunHeader*> ( lcHeader.release()) );
// prepare the short buffet
_buffer = new short[_noOfXPixel * _noOfYPixel];
// end of first event
_isFirstEvent = false;
}
int nVal = 0;
int xPixel, yPixel;
try {
for (yPixel = 0; yPixel < _noOfYPixel; yPixel++) {
for (xPixel = 0; xPixel < _noOfXPixel; xPixel++) {
inputFile >> _buffer[nVal++];
}
}
} catch (exception & e) {
if (!inputFile.eof ())
cerr << "A read exception occurred : " << e.what () << endl;
if ((xPixel == 0) && (yPixel == 0)) {
// that's normal
// we are reading the last empty line.
// break here
break;
} else {
// that's strange. it might be that the last event was not complete
// break here
message<ERROR5> ( log() << "Event " << eventNumber << " finished un-expectedly. " );
message<ERROR5> ( log() << "Consider to check the input file, or limit the conversion to "
<< eventNumber - 1 << " events" << endl << "Sorry to quit!" ) ;
exit(-1);
}
}
event->setRunNumber (runNumber);
event->setEventNumber (eventNumber++);
// prepare a collection to store the raw-data
TrackerRawDataImpl *rawMatrix = new TrackerRawDataImpl;
CellIDEncoder < TrackerRawDataImpl > idEncoder (EUTELESCOPE::MATRIXDEFAULTENCODING, rawData);
idEncoder["sensorID"] = 0;
idEncoder["xMin"] = 0;
idEncoder["xMax"] = _noOfXPixel - 1;
idEncoder["yMin"] = 0;
idEncoder["yMax"] = _noOfYPixel - 1;
idEncoder.setCellID (rawMatrix);
nVal = 0;
for (int yPixel = 0; yPixel < _noOfYPixel; yPixel++) {
for (int xPixel = 0; xPixel < _noOfXPixel; xPixel++) {
rawMatrix->adcValues ().push_back (_buffer[nVal++]);
}
}
rawData->push_back (rawMatrix);
event->addCollection (rawData, "rawdata");
ProcessorMgr::instance ()->processEvent (static_cast<LCEventImpl*> (event));
delete event;
}
EUTelEventImpl *event = new EUTelEventImpl;
event->setDetectorName("MIMOSA");
LCTime * now = new LCTime;
event->setTimeStamp(now->timeStamp());
delete now;
event->setRunNumber (runNumber);
event->setEventNumber (eventNumber++);
event->setEventType(kEORE);
ProcessorMgr::instance ()->processEvent (static_cast<LCEventImpl*> (event));
delete event;
}
void EUTelSucimaImagerReader::end () {
delete[]_buffer;
message<MESSAGE5> ("Successfully finished") ;
}