1 | #include <valarray>
|
---|
2 |
|
---|
3 | #include <boost/filesystem.hpp>
|
---|
4 |
|
---|
5 | #include "Dim.h"
|
---|
6 | #include "Event.h"
|
---|
7 | #include "Shell.h"
|
---|
8 | #include "StateMachineDim.h"
|
---|
9 | #include "Connection.h"
|
---|
10 | #include "Configuration.h"
|
---|
11 | #include "Console.h"
|
---|
12 |
|
---|
13 | #include "tools.h"
|
---|
14 |
|
---|
15 | #include "zfits.h"
|
---|
16 |
|
---|
17 | namespace ba = boost::asio;
|
---|
18 | namespace bs = boost::system;
|
---|
19 |
|
---|
20 | using namespace std;
|
---|
21 |
|
---|
22 | // ------------------------------------------------------------------------
|
---|
23 |
|
---|
24 | #include "DimDescriptionService.h"
|
---|
25 | #include "DimState.h"
|
---|
26 |
|
---|
27 | // ------------------------------------------------------------------------
|
---|
28 |
|
---|
29 | void SetupConfiguration(Configuration &conf)
|
---|
30 | {
|
---|
31 | po::options_description control("Program options");
|
---|
32 | control.add_options()
|
---|
33 | ("target,t", var<string>()->required(), "")
|
---|
34 | ("event,e", var<uint32_t>(), "")
|
---|
35 | ;
|
---|
36 |
|
---|
37 | po::positional_options_description p;
|
---|
38 | p.add("target", 1); // The first positional options
|
---|
39 | p.add("event", 2); // The second positional options
|
---|
40 |
|
---|
41 | conf.AddOptions(control);
|
---|
42 | conf.SetArgumentPositions(p);
|
---|
43 | }
|
---|
44 |
|
---|
45 | /*
|
---|
46 | Extract usage clause(s) [if any] for SYNOPSIS.
|
---|
47 | Translators: "Usage" and "or" here are patterns (regular expressions) which
|
---|
48 | are used to match the usage synopsis in program output. An example from cp
|
---|
49 | (GNU coreutils) which contains both strings:
|
---|
50 | Usage: cp [OPTION]... [-T] SOURCE DEST
|
---|
51 | or: cp [OPTION]... SOURCE... DIRECTORY
|
---|
52 | or: cp [OPTION]... -t DIRECTORY SOURCE...
|
---|
53 | */
|
---|
54 | void PrintUsage()
|
---|
55 | {
|
---|
56 | cout <<
|
---|
57 | "Retrieve an event from a file in binary representation.\n"
|
---|
58 | "\n"
|
---|
59 | "Usage: getevent [-c type] [OPTIONS]\n"
|
---|
60 | " or: getevent [OPTIONS]\n";
|
---|
61 | cout << endl;
|
---|
62 | }
|
---|
63 |
|
---|
64 | void PrintHelp()
|
---|
65 | {
|
---|
66 | //Main::PrintHelp<StateMachineEventServer>();
|
---|
67 |
|
---|
68 | /* Additional help text which is printed after the configuration
|
---|
69 | options goes here */
|
---|
70 |
|
---|
71 | /*
|
---|
72 | cout << "bla bla bla" << endl << endl;
|
---|
73 | cout << endl;
|
---|
74 | cout << "Environment:" << endl;
|
---|
75 | cout << "environment" << endl;
|
---|
76 | cout << endl;
|
---|
77 | cout << "Examples:" << endl;
|
---|
78 | cout << "test exam" << endl;
|
---|
79 | cout << endl;
|
---|
80 | cout << "Files:" << endl;
|
---|
81 | cout << "files" << endl;
|
---|
82 | cout << endl;
|
---|
83 | */
|
---|
84 | }
|
---|
85 |
|
---|
86 | /*
|
---|
87 | boost::filesystem::recursive_directory_iterator createRIterator(boost::filesystem::path path)
|
---|
88 | {
|
---|
89 | try
|
---|
90 | {
|
---|
91 | return boost::filesystem::recursive_directory_iterator(path);
|
---|
92 | }
|
---|
93 | catch(boost::filesystem::filesystem_error& fex)
|
---|
94 | {
|
---|
95 | std::cout << fex.what() << std::endl;
|
---|
96 | return boost::filesystem::recursive_directory_iterator();
|
---|
97 | }
|
---|
98 | }
|
---|
99 |
|
---|
100 | void dump(boost::filesystem::path path, int level)
|
---|
101 | {
|
---|
102 | try
|
---|
103 | {
|
---|
104 | std::cout << (boost::filesystem::is_directory(path) ? 'D' : ' ') << ' ';
|
---|
105 | std::cout << (boost::filesystem::is_symlink(path) ? 'L' : ' ') << ' ';
|
---|
106 |
|
---|
107 | for(int i = 0; i < level; ++i)
|
---|
108 | std::cout << ' ';
|
---|
109 |
|
---|
110 | std::cout << path.filename() << std::endl;
|
---|
111 | }
|
---|
112 | catch(boost::filesystem::filesystem_error& fex)
|
---|
113 | {
|
---|
114 | std::cout << fex.what() << std::endl;
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | void plainListTree(boost::filesystem::path path) // 1.
|
---|
119 | {
|
---|
120 | dump(path, 0);
|
---|
121 |
|
---|
122 | boost::filesystem::recursive_directory_iterator it = createRIterator(path);
|
---|
123 | boost::filesystem::recursive_directory_iterator end;
|
---|
124 |
|
---|
125 | while(it != end) // 2.
|
---|
126 | {
|
---|
127 | dump(*it, it.level()); // 3.
|
---|
128 |
|
---|
129 | if (boost::filesystem::is_directory(*it) && boost::filesystem::is_symlink(*it)) // 4.
|
---|
130 | it.no_push();
|
---|
131 |
|
---|
132 | try
|
---|
133 | {
|
---|
134 | ++it; // 5.
|
---|
135 | }
|
---|
136 | catch(std::exception& ex)
|
---|
137 | {
|
---|
138 | std::cout << ex.what() << std::endl;
|
---|
139 | it.no_push(); // 6.
|
---|
140 | try { ++it; } catch(...) { std::cout << "!!" << std::endl; return; } // 7.
|
---|
141 | }
|
---|
142 | }
|
---|
143 | }
|
---|
144 | */
|
---|
145 |
|
---|
146 | int main(int argc, const char* argv[])
|
---|
147 | {
|
---|
148 | Configuration conf(argv[0]);
|
---|
149 | conf.SetPrintUsage(PrintUsage);
|
---|
150 | SetupConfiguration(conf);
|
---|
151 |
|
---|
152 | if (!conf.DoParse(argc, argv, PrintHelp))
|
---|
153 | return 127;
|
---|
154 |
|
---|
155 | const string name = conf.Get<string>("target");
|
---|
156 | /*
|
---|
157 | if (!conf.Has("event"))
|
---|
158 | {
|
---|
159 | plainListTree(name);
|
---|
160 | return 0;
|
---|
161 | }
|
---|
162 | */
|
---|
163 | const uint32_t event = conf.Has("event") ? conf.Get<uint32_t>("event") : 0;
|
---|
164 |
|
---|
165 | zfits file(name);
|
---|
166 | if (!file)
|
---|
167 | {
|
---|
168 | cerr << name << ": " << strerror(errno) << endl;
|
---|
169 | return 1;
|
---|
170 | }
|
---|
171 |
|
---|
172 | // Php can only read 32bit ints
|
---|
173 | const uint32_t nRows = file.GetNumRows();
|
---|
174 |
|
---|
175 | const uint32_t nRoi = file.GetUInt("NROI");
|
---|
176 | const uint32_t nPix = file.GetUInt("NPIX");
|
---|
177 |
|
---|
178 | const bool isMC = file.HasKey("ISMC") && file.GetStr("ISMC")=="T";
|
---|
179 |
|
---|
180 | // Is drs calibration file?
|
---|
181 | const int8_t step = file.HasKey("STEP") ? file.GetUInt("STEP") : -1;
|
---|
182 |
|
---|
183 | const string strbeg = isMC ? "DATE" : "RUN"+to_string(step)+"-BEG";
|
---|
184 | const string strend = isMC ? "DATE" : "RUN"+to_string(step)+"-END";
|
---|
185 |
|
---|
186 | const double start = step==-1 && !isMC ? file.GetUInt("TSTARTI")+file.GetFloat("TSTARTF") : Time(file.GetStr(strbeg)).UnixDate();
|
---|
187 | const double stop = step==-1 && !isMC ? file.GetUInt("TSTOPI")+file.GetFloat("TSTOPF") : Time(file.GetStr(strend)).UnixDate();
|
---|
188 |
|
---|
189 | const bool isDrsCalib = file.HasKey("DRSCALIB") && file.GetStr("DRSCALIB")=="T";
|
---|
190 | const int16_t drsStep = isDrsCalib ? file.GetUInt("DRSSTEP") : step;
|
---|
191 | const string runType = step==-1 ? file.GetStr("RUNTYPE") : "";
|
---|
192 | const uint16_t scale = file.HasKey("SCALE") ? file.GetUInt("SCALE") : (step==-1?0:10);
|
---|
193 |
|
---|
194 | vector<char> run(80);
|
---|
195 | vector<int16_t> data(nRoi*nPix);
|
---|
196 | vector<float> mean(nRoi*nPix);
|
---|
197 | vector<uint32_t> unixTimeUTC(2);
|
---|
198 |
|
---|
199 | //uint32_t boardTime[40];
|
---|
200 | uint32_t eventNum;
|
---|
201 | //uint32_t numBoards;
|
---|
202 | //uint16_t startCellData[1440];
|
---|
203 | //uint16_t startCellTimeMarker[160];
|
---|
204 | //uint32_t triggerNum;
|
---|
205 | uint16_t triggerType;
|
---|
206 |
|
---|
207 | if (step==-1)
|
---|
208 | {
|
---|
209 | file.SetRefAddress("EventNum", eventNum);
|
---|
210 | //file.SetRefAddress("TriggerNum", triggerNum);
|
---|
211 | file.SetRefAddress("TriggerType", triggerType);
|
---|
212 | if (!isMC)
|
---|
213 | file.SetVecAddress("UnixTimeUTC", unixTimeUTC);
|
---|
214 | }
|
---|
215 |
|
---|
216 | float energy=0, impact, phi, theta;
|
---|
217 | if (isMC)
|
---|
218 | {
|
---|
219 | try
|
---|
220 | {
|
---|
221 | file.SetRefAddress("MMcEvtBasic.fEnergy", energy);
|
---|
222 | file.SetRefAddress("MMcEvtBasic.fImpact", impact);
|
---|
223 | file.SetRefAddress("MMcEvtBasic.fTelescopeTheta", theta);
|
---|
224 | file.SetRefAddress("MMcEvtBasic.fTelescopePhi", phi);
|
---|
225 | }
|
---|
226 | catch (const std::runtime_error &)
|
---|
227 | {
|
---|
228 | }
|
---|
229 | }
|
---|
230 |
|
---|
231 | switch (step)
|
---|
232 | {
|
---|
233 | case 0: file.SetVecAddress("BaselineMean", mean); strcpy( run.data(), "DRS (pedestal 1024)"); break;
|
---|
234 | case 1: file.SetVecAddress("GainMean", mean); strcpy( run.data(), "DRS (gain)"); break;
|
---|
235 | case 2: file.SetVecAddress("TriggerOffsetMean", mean); strcpy( run.data(), "DRS (pedestal roi)"); break;
|
---|
236 | default: file.SetVecAddress("Data", data); strncpy(run.data(), runType.c_str(), 79); break;
|
---|
237 | }
|
---|
238 |
|
---|
239 | if (!file.GetRow(step==-1 ? event : 0))
|
---|
240 | return 2;
|
---|
241 |
|
---|
242 | if (step!=-1)
|
---|
243 | for (uint32_t i=0; i<nRoi*nPix; i++)
|
---|
244 | data[i] = round(mean[i]*10);
|
---|
245 |
|
---|
246 | cout.write(run.data(), 80);
|
---|
247 | cout.write((char*)&start, sizeof(double));
|
---|
248 | cout.write((char*)&stop, sizeof(double));
|
---|
249 | cout.write((char*)&drsStep, sizeof(drsStep));
|
---|
250 | cout.write((char*)&nRows, sizeof(nRows));
|
---|
251 | cout.write((char*)&scale, sizeof(scale));
|
---|
252 |
|
---|
253 | cout.write((char*)&nRoi, sizeof(nRoi));
|
---|
254 | cout.write((char*)&nPix, sizeof(nPix));
|
---|
255 | cout.write((char*)&eventNum, sizeof(eventNum));
|
---|
256 | //cout.write((char*)&triggerNum, sizeof(triggerNum));
|
---|
257 | cout.write((char*)&triggerType, sizeof(triggerType));
|
---|
258 | cout.write((char*)unixTimeUTC.data(), sizeof(uint32_t)*2);
|
---|
259 | cout.write((char*)data.data(), sizeof(int16_t)*nRoi*nPix);
|
---|
260 |
|
---|
261 | if (energy!=0)
|
---|
262 | {
|
---|
263 | cout.write((char*)&energy, sizeof(float));
|
---|
264 | cout.write((char*)&impact, sizeof(float));
|
---|
265 | cout.write((char*)&phi, sizeof(float));
|
---|
266 | cout.write((char*)&theta, sizeof(float));
|
---|
267 | }
|
---|
268 |
|
---|
269 | return 0;
|
---|
270 | }
|
---|