| 1 | #ifndef FACT_EventBuilderWrapper
|
|---|
| 2 | #define FACT_EventBuilderWrapper
|
|---|
| 3 |
|
|---|
| 4 | #include <sstream>
|
|---|
| 5 |
|
|---|
| 6 | #if BOOST_VERSION < 104400
|
|---|
| 7 | #if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 4))
|
|---|
| 8 | #undef BOOST_HAS_RVALUE_REFS
|
|---|
| 9 | #endif
|
|---|
| 10 | #endif
|
|---|
| 11 | #include <boost/thread.hpp>
|
|---|
| 12 | #include <boost/date_time/posix_time/posix_time_types.hpp>
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 | #include <CCfits/CCfits>
|
|---|
| 16 |
|
|---|
| 17 | #include "EventBuilder.h"
|
|---|
| 18 |
|
|---|
| 19 | extern "C" {
|
|---|
| 20 | extern void StartEvtBuild();
|
|---|
| 21 | extern int CloseRunFile(uint32_t runId, uint32_t closeTime, uint32_t maxEvt);
|
|---|
| 22 | }
|
|---|
| 23 |
|
|---|
| 24 | namespace ba = boost::asio;
|
|---|
| 25 | namespace bs = boost::system;
|
|---|
| 26 |
|
|---|
| 27 | using ba::ip::tcp;
|
|---|
| 28 |
|
|---|
| 29 | using namespace std;
|
|---|
| 30 |
|
|---|
| 31 | class DataFileImp : public MessageImp
|
|---|
| 32 | {
|
|---|
| 33 | uint32_t fRunId;
|
|---|
| 34 |
|
|---|
| 35 | int Write(const Time &time, const std::string &txt, int qos)
|
|---|
| 36 | {
|
|---|
| 37 | return fMsg.Write(time, txt, qos);
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | protected:
|
|---|
| 41 | MessageImp &fMsg;
|
|---|
| 42 | string fFileName;
|
|---|
| 43 |
|
|---|
| 44 | public:
|
|---|
| 45 | DataFileImp(uint32_t id, MessageImp &imp) : fRunId(id), fMsg(imp) { }
|
|---|
| 46 | virtual ~DataFileImp() { }
|
|---|
| 47 |
|
|---|
| 48 | virtual bool OpenFile(RUN_HEAD* h) = 0;
|
|---|
| 49 | virtual bool WriteEvt(EVENT *) = 0;
|
|---|
| 50 | virtual bool Close(RUN_TAIL * = 0) = 0;
|
|---|
| 51 |
|
|---|
| 52 | const string &GetFileName() const { return fFileName; }
|
|---|
| 53 |
|
|---|
| 54 | uint32_t GetRunId() const { return fRunId; }
|
|---|
| 55 |
|
|---|
| 56 | // --------------------------------------------------------------------------
|
|---|
| 57 | //
|
|---|
| 58 | //! This creates an appropriate file name for a particular run number and type
|
|---|
| 59 | //! @param runNumber the run number for which a filename is to be created
|
|---|
| 60 | //! @param runType an int describing the kind of run. 0=Data, 1=Pedestal, 2=Calibration, 3=Calibrated data
|
|---|
| 61 | //! @param extension a string containing the extension to be appened to the file name
|
|---|
| 62 | //
|
|---|
| 63 | static string FormFileName(uint32_t runid, string extension)
|
|---|
| 64 | {
|
|---|
| 65 | ostringstream name;
|
|---|
| 66 | name << Time().NightAsInt() << '_' << setfill('0') << setw(3) << runid << '.' << extension;
|
|---|
| 67 | return name.str();
|
|---|
| 68 | }
|
|---|
| 69 | };
|
|---|
| 70 |
|
|---|
| 71 | class DataFileNone : public DataFileImp
|
|---|
| 72 | {
|
|---|
| 73 | public:
|
|---|
| 74 | DataFileNone(uint32_t id, MessageImp &imp) : DataFileImp(id, imp) { }
|
|---|
| 75 |
|
|---|
| 76 | Time fTime;
|
|---|
| 77 |
|
|---|
| 78 | bool OpenFile(RUN_HEAD* h)
|
|---|
| 79 | {
|
|---|
| 80 | fFileName = "/dev/null";
|
|---|
| 81 |
|
|---|
| 82 | ostringstream str;
|
|---|
| 83 | str << this << " - "
|
|---|
| 84 | << "OPEN_FILE #" << GetRunId() << ":"
|
|---|
| 85 | << " Ver=" << h->Version
|
|---|
| 86 | << " Typ=" << h->RunType
|
|---|
| 87 | << " Nb=" << h->NBoard
|
|---|
| 88 | << " Np=" << h->NPix
|
|---|
| 89 | << " NTm=" << h->NTm
|
|---|
| 90 | << " roi=" << h->Nroi;
|
|---|
| 91 |
|
|---|
| 92 | Debug(str);
|
|---|
| 93 |
|
|---|
| 94 | fTime = Time();
|
|---|
| 95 |
|
|---|
| 96 | return true;
|
|---|
| 97 | }
|
|---|
| 98 | bool WriteEvt(EVENT *e)
|
|---|
| 99 | {
|
|---|
| 100 | const Time now;
|
|---|
| 101 | if (now-fTime<boost::posix_time::seconds(5))
|
|---|
| 102 | return true;
|
|---|
| 103 |
|
|---|
| 104 | fTime = now;
|
|---|
| 105 |
|
|---|
| 106 | ostringstream str;
|
|---|
| 107 | str << this << " - EVENT #" << e->EventNum;
|
|---|
| 108 | Debug(str);
|
|---|
| 109 |
|
|---|
| 110 | return true;
|
|---|
| 111 | }
|
|---|
| 112 | bool Close(RUN_TAIL * = 0)
|
|---|
| 113 | {
|
|---|
| 114 | ostringstream str;
|
|---|
| 115 | str << this << " - CLOSE FILE #" << GetRunId();
|
|---|
| 116 |
|
|---|
| 117 | Debug(str);
|
|---|
| 118 |
|
|---|
| 119 | return true;
|
|---|
| 120 | }
|
|---|
| 121 | };
|
|---|
| 122 |
|
|---|
| 123 | #include "DrsCalib.h"
|
|---|
| 124 |
|
|---|
| 125 | class DataFileCalib : public DataFileImp, CalibData
|
|---|
| 126 | {
|
|---|
| 127 | static vector<int32_t> fOffset;
|
|---|
| 128 | static vector<int32_t> fGain;
|
|---|
| 129 | static vector<int32_t> fTrgOff;
|
|---|
| 130 |
|
|---|
| 131 | static uint32_t fNumOffset;
|
|---|
| 132 | static uint32_t fNumGain;
|
|---|
| 133 | static uint32_t fNumTrgOff;
|
|---|
| 134 |
|
|---|
| 135 | static int fStep;
|
|---|
| 136 |
|
|---|
| 137 | public:
|
|---|
| 138 | DataFileCalib(uint32_t id, MessageImp &imp) : DataFileImp(id, imp)
|
|---|
| 139 | {
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | static void Restart() { fStep = 0; }
|
|---|
| 143 |
|
|---|
| 144 | bool OpenFile(RUN_HEAD* h)
|
|---|
| 145 | {
|
|---|
| 146 | if (fStep==4)
|
|---|
| 147 | {
|
|---|
| 148 | fMsg.Warn("DRS Calibration already finished... please restart!");
|
|---|
| 149 | return false;
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | if (h->NPix != 1440)
|
|---|
| 153 | {
|
|---|
| 154 | fMsg.Error("Number of pixels in header not 1440.");
|
|---|
| 155 | return false;
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | if (h->Nroi != 1024)
|
|---|
| 159 | {
|
|---|
| 160 | fMsg.Error("Region of interest not 1024.");
|
|---|
| 161 | return false;
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | ostringstream name;
|
|---|
| 165 | name << "drs-calib-" << fStep;
|
|---|
| 166 | fFileName = name.str();
|
|---|
| 167 |
|
|---|
| 168 | Reset();
|
|---|
| 169 | InitSize(1440, 1024);
|
|---|
| 170 |
|
|---|
| 171 | if (fStep==0)
|
|---|
| 172 | {
|
|---|
| 173 | // Default gain:
|
|---|
| 174 | // 0.575*[45590]*2.5V / 2^16 = 0.99999 V
|
|---|
| 175 | fOffset.assign(1440*1024, 0);
|
|---|
| 176 | fGain.assign (1440*1024, 2);
|
|---|
| 177 | fTrgOff.assign(1440*1024, 0);
|
|---|
| 178 |
|
|---|
| 179 | fNumOffset = 1;
|
|---|
| 180 | fNumGain = 1;
|
|---|
| 181 | fNumTrgOff = 1;
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | return true;
|
|---|
| 185 | }
|
|---|
| 186 | bool WriteEvt(EVENT *e)
|
|---|
| 187 | {
|
|---|
| 188 | // FIXME: SET StartPix to 0 if StartPix is -1
|
|---|
| 189 |
|
|---|
| 190 | if (fStep==0)
|
|---|
| 191 | {
|
|---|
| 192 | AddRel(e->Adc_Data, e->StartPix);
|
|---|
| 193 | }
|
|---|
| 194 | if (fStep==1)
|
|---|
| 195 | {
|
|---|
| 196 | AddRel(e->Adc_Data, e->StartPix, fOffset.data(), fNumOffset);
|
|---|
| 197 | }
|
|---|
| 198 | if (fStep==2)
|
|---|
| 199 | {
|
|---|
| 200 | AddAbs(e->Adc_Data, e->StartPix, fOffset.data(), fNumOffset);
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | return true;
|
|---|
| 204 | }
|
|---|
| 205 | bool Close(RUN_TAIL * = 0)
|
|---|
| 206 | {
|
|---|
| 207 | if (fStep==0)
|
|---|
| 208 | {
|
|---|
| 209 | fOffset.assign(fSum.begin(), fSum.end());
|
|---|
| 210 | fNumOffset = fNumEntries;
|
|---|
| 211 | }
|
|---|
| 212 | if (fStep==1)
|
|---|
| 213 | {
|
|---|
| 214 | fGain.assign(fSum.begin(), fSum.end());
|
|---|
| 215 | fNumGain = fNumEntries*1000;
|
|---|
| 216 | // Scale from Volt to Millivolt
|
|---|
| 217 | }
|
|---|
| 218 | if (fStep==2)
|
|---|
| 219 | {
|
|---|
| 220 | fTrgOff.assign(fSum.begin(), fSum.end());
|
|---|
| 221 | fNumTrgOff = fNumEntries;
|
|---|
| 222 | }
|
|---|
| 223 | fStep++;
|
|---|
| 224 |
|
|---|
| 225 | return true;
|
|---|
| 226 | }
|
|---|
| 227 |
|
|---|
| 228 | static void Apply(int16_t *val, const int16_t *start, uint32_t roi)
|
|---|
| 229 | {
|
|---|
| 230 | CalibData::Apply(val, start, roi,
|
|---|
| 231 | fOffset.data(), fNumOffset,
|
|---|
| 232 | fGain.data(), fNumGain,
|
|---|
| 233 | fTrgOff.data(), fNumTrgOff);
|
|---|
| 234 | }
|
|---|
| 235 |
|
|---|
| 236 | static void Apply(float *vec, int16_t *val, const int16_t *start, uint32_t roi)
|
|---|
| 237 | {
|
|---|
| 238 | CalibData::Apply(vec, val, start, roi,
|
|---|
| 239 | fOffset.data(), fNumOffset,
|
|---|
| 240 | fGain.data(), fNumGain,
|
|---|
| 241 | fTrgOff.data(), fNumTrgOff);
|
|---|
| 242 | }
|
|---|
| 243 | };
|
|---|
| 244 |
|
|---|
| 245 | int DataFileCalib::fStep = 0;
|
|---|
| 246 |
|
|---|
| 247 | vector<int32_t> DataFileCalib::fOffset(1440*1024, 0);
|
|---|
| 248 | vector<int32_t> DataFileCalib::fGain (1440*1024, 2);
|
|---|
| 249 | vector<int32_t> DataFileCalib::fTrgOff(1440*1024, 0);
|
|---|
| 250 |
|
|---|
| 251 | uint32_t DataFileCalib::fNumOffset = 1;
|
|---|
| 252 | uint32_t DataFileCalib::fNumGain = 1;
|
|---|
| 253 | uint32_t DataFileCalib::fNumTrgOff = 1;
|
|---|
| 254 |
|
|---|
| 255 |
|
|---|
| 256 | class DataFileDebug : public DataFileNone
|
|---|
| 257 | {
|
|---|
| 258 | public:
|
|---|
| 259 | DataFileDebug(uint32_t id, MessageImp &imp) : DataFileNone(id, imp) { }
|
|---|
| 260 |
|
|---|
| 261 | bool WriteEvt(EVENT *e)
|
|---|
| 262 | {
|
|---|
| 263 | cout << "WRITE_EVENT #" << GetRunId() << " (" << e->EventNum << ")" << endl;
|
|---|
| 264 | cout << " Typ=" << e->TriggerType << endl;
|
|---|
| 265 | cout << " roi=" << e->Roi << endl;
|
|---|
| 266 | cout << " trg=" << e->SoftTrig << endl;
|
|---|
| 267 | cout << " tim=" << e->PCTime << endl;
|
|---|
| 268 |
|
|---|
| 269 | return true;
|
|---|
| 270 | }
|
|---|
| 271 | };
|
|---|
| 272 |
|
|---|
| 273 | #include "FAD.h"
|
|---|
| 274 |
|
|---|
| 275 | class DataFileRaw : public DataFileImp
|
|---|
| 276 | {
|
|---|
| 277 | ofstream fOut;
|
|---|
| 278 |
|
|---|
| 279 | off_t fPosTail;
|
|---|
| 280 |
|
|---|
| 281 | uint32_t fCounter;
|
|---|
| 282 |
|
|---|
| 283 |
|
|---|
| 284 | // WRITE uint32_t 0xFAC77e1e (FACT Tele)
|
|---|
| 285 | // ===
|
|---|
| 286 | // WRITE uint32_t TYPE(>0) == 1
|
|---|
| 287 | // WRITE uint32_t ID(>0) == 0
|
|---|
| 288 | // WRITE uint32_t VERSION(>0) == 1
|
|---|
| 289 | // WRITE uint32_t LENGTH
|
|---|
| 290 | // -
|
|---|
| 291 | // WRITE uint32_t TELESCOPE ID
|
|---|
| 292 | // WRITE uint32_t RUNID
|
|---|
| 293 | // ===
|
|---|
| 294 | // WRITE uint32_t TYPE(>0) == 2
|
|---|
| 295 | // WRITE uint32_t ID(>0) == 0
|
|---|
| 296 | // WRITE uint32_t VERSION(>0) == 1
|
|---|
| 297 | // WRITE uint32_t LENGTH
|
|---|
| 298 | // -
|
|---|
| 299 | // WRITE HEADER
|
|---|
| 300 | // ===
|
|---|
| 301 | // [ 40 TIMES
|
|---|
| 302 | // WRITE uint32_t TYPE(>0) == 3
|
|---|
| 303 | // WRITE uint32_t ID(>0) == 0..39
|
|---|
| 304 | // WRITE uint32_t VERSION(>0) == 1
|
|---|
| 305 | // WRITE uint32_t LENGTH
|
|---|
| 306 | // -
|
|---|
| 307 | // WRITE BOARD-HEADER
|
|---|
| 308 | // ]
|
|---|
| 309 | // ===
|
|---|
| 310 | // WRITE uint32_t TYPE(>0) == 4
|
|---|
| 311 | // WRITE uint32_t ID(>0) == 0
|
|---|
| 312 | // WRITE uint32_t VERSION(>0) == 1
|
|---|
| 313 | // WRITE uint32_t LENGTH
|
|---|
| 314 | // -
|
|---|
| 315 | // WRITE FOOTER (empty)
|
|---|
| 316 | // ===
|
|---|
| 317 | // [ N times
|
|---|
| 318 | // WRITE uint32_t TYPE(>0) == 10
|
|---|
| 319 | // WRITE uint32_t ID(>0) == counter
|
|---|
| 320 | // WRITE uint32_t VERSION(>0) == 1
|
|---|
| 321 | // WRITE uint32_t LENGTH HEADER
|
|---|
| 322 | // -
|
|---|
| 323 | // WRITE HEADER+DATA
|
|---|
| 324 | // ]
|
|---|
| 325 | // ===
|
|---|
| 326 | // WRITE uint32_t TYPE ==0
|
|---|
| 327 | // WRITE uint32_t VERSION==0
|
|---|
| 328 | // WRITE uint32_t LENGTH ==0
|
|---|
| 329 | // ===
|
|---|
| 330 | // Go back and write footer
|
|---|
| 331 |
|
|---|
| 332 | public:
|
|---|
| 333 | DataFileRaw(uint32_t id, MessageImp &imp) : DataFileImp(id, imp), fPosTail(0) { }
|
|---|
| 334 | ~DataFileRaw() { if (fOut.is_open()) Close(); }
|
|---|
| 335 |
|
|---|
| 336 | void WriteBlockHeader(uint32_t type, uint32_t ver, uint32_t cnt, uint32_t len)
|
|---|
| 337 | {
|
|---|
| 338 | const uint32_t val[4] = { type, ver, cnt, len };
|
|---|
| 339 |
|
|---|
| 340 | fOut.write(reinterpret_cast<const char*>(val), sizeof(val));
|
|---|
| 341 | }
|
|---|
| 342 |
|
|---|
| 343 | template<typename T>
|
|---|
| 344 | void WriteValue(const T &t)
|
|---|
| 345 | {
|
|---|
| 346 | fOut.write(reinterpret_cast<const char*>(&t), sizeof(T));
|
|---|
| 347 | }
|
|---|
| 348 |
|
|---|
| 349 | enum
|
|---|
| 350 | {
|
|---|
| 351 | kEndOfFile = 0,
|
|---|
| 352 | kIdentifier = 1,
|
|---|
| 353 | kRunHeader,
|
|---|
| 354 | kBoardHeader,
|
|---|
| 355 | kRunSummary,
|
|---|
| 356 | kEvent,
|
|---|
| 357 | };
|
|---|
| 358 |
|
|---|
| 359 | bool OpenFile(RUN_HEAD *h)
|
|---|
| 360 | {
|
|---|
| 361 | const string name = FormFileName(GetRunId(), "bin");
|
|---|
| 362 | if (access(name.c_str(), F_OK)==0)
|
|---|
| 363 | {
|
|---|
| 364 | Error("File '"+name+"' already exists.");
|
|---|
| 365 | return false;
|
|---|
| 366 | }
|
|---|
| 367 |
|
|---|
| 368 | fFileName = name;
|
|---|
| 369 |
|
|---|
| 370 | errno = 0;
|
|---|
| 371 | fOut.open(name.c_str(), ios_base::out);
|
|---|
| 372 | if (!fOut)
|
|---|
| 373 | {
|
|---|
| 374 | ostringstream str;
|
|---|
| 375 | str << "ofstream::open() failed for '" << name << "': " << strerror(errno) << " [errno=" << errno << "]";
|
|---|
| 376 | Error(str);
|
|---|
| 377 |
|
|---|
| 378 | return false;
|
|---|
| 379 | }
|
|---|
| 380 |
|
|---|
| 381 | fCounter = 0;
|
|---|
| 382 |
|
|---|
| 383 | static uint32_t FACT = 0xFAC77e1e;
|
|---|
| 384 |
|
|---|
| 385 | fOut.write(reinterpret_cast<char*>(&FACT), 4);
|
|---|
| 386 |
|
|---|
| 387 | WriteBlockHeader(kIdentifier, 1, 0, 8);
|
|---|
| 388 | WriteValue(uint32_t(0));
|
|---|
| 389 | WriteValue(GetRunId());
|
|---|
| 390 |
|
|---|
| 391 | WriteBlockHeader(kRunHeader, 1, 0, sizeof(RUN_HEAD)-sizeof(PEVNT_HEADER*));
|
|---|
| 392 | fOut.write(reinterpret_cast<char*>(h), sizeof(RUN_HEAD)-sizeof(PEVNT_HEADER*));
|
|---|
| 393 |
|
|---|
| 394 | for (int i=0; i<40; i++)
|
|---|
| 395 | {
|
|---|
| 396 | WriteBlockHeader(kBoardHeader, 1, i, sizeof(PEVNT_HEADER));
|
|---|
| 397 | fOut.write(reinterpret_cast<char*>(h->FADhead+i), sizeof(PEVNT_HEADER));
|
|---|
| 398 | }
|
|---|
| 399 |
|
|---|
| 400 | // FIXME: Split this
|
|---|
| 401 | const vector<char> block(sizeof(uint32_t)+sizeof(RUN_TAIL));
|
|---|
| 402 | WriteBlockHeader(kRunSummary, 1, 0, block.size());
|
|---|
| 403 |
|
|---|
| 404 | fPosTail = fOut.tellp();
|
|---|
| 405 | fOut.write(block.data(), block.size());
|
|---|
| 406 |
|
|---|
| 407 | if (!fOut)
|
|---|
| 408 | {
|
|---|
| 409 | ostringstream str;
|
|---|
| 410 | str << "ofstream::write() failed for '" << name << "': " << strerror(errno) << " [errno=" << errno << "]";
|
|---|
| 411 | Error(str);
|
|---|
| 412 |
|
|---|
| 413 | return false;
|
|---|
| 414 | }
|
|---|
| 415 |
|
|---|
| 416 | return true;
|
|---|
| 417 | }
|
|---|
| 418 | bool WriteEvt(EVENT *evt)
|
|---|
| 419 | {
|
|---|
| 420 | const int sh = sizeof(EVENT)-2 + NPIX*evt->Roi*2;
|
|---|
| 421 |
|
|---|
| 422 | WriteBlockHeader(kEvent, 1, fCounter++, sh);
|
|---|
| 423 | fOut.write(reinterpret_cast<char*>(evt)+2, sh);
|
|---|
| 424 | return true;
|
|---|
| 425 | }
|
|---|
| 426 | bool Close(RUN_TAIL *tail= 0)
|
|---|
| 427 | {
|
|---|
| 428 | WriteBlockHeader(kEndOfFile, 0, 0, 0);
|
|---|
| 429 |
|
|---|
| 430 | if (tail)
|
|---|
| 431 | {
|
|---|
| 432 | fOut.seekp(fPosTail);
|
|---|
| 433 |
|
|---|
| 434 | WriteValue(uint32_t(1));
|
|---|
| 435 | fOut.write(reinterpret_cast<char*>(tail), sizeof(RUN_TAIL));
|
|---|
| 436 | }
|
|---|
| 437 |
|
|---|
| 438 | if (!fOut)
|
|---|
| 439 | {
|
|---|
| 440 | ostringstream str;
|
|---|
| 441 |
|
|---|
| 442 | str << "ofstream::write() failed for '" << GetFileName() << "': " << strerror(errno) << " [errno=" << errno << "]";
|
|---|
| 443 | Error(str);
|
|---|
| 444 |
|
|---|
| 445 | return false;
|
|---|
| 446 | }
|
|---|
| 447 |
|
|---|
| 448 | fOut.close();
|
|---|
| 449 |
|
|---|
| 450 | if (!fOut)
|
|---|
| 451 | {
|
|---|
| 452 | ostringstream str;
|
|---|
| 453 | str << "ofstream::close() failed for '" << GetFileName() << "': " << strerror(errno) << " [errno=" << errno << "]";
|
|---|
| 454 | Error(str);
|
|---|
| 455 |
|
|---|
| 456 | return false;
|
|---|
| 457 | }
|
|---|
| 458 |
|
|---|
| 459 | return true;
|
|---|
| 460 | }
|
|---|
| 461 | };
|
|---|
| 462 |
|
|---|
| 463 | #ifdef HAVE_FITS
|
|---|
| 464 | class DataFileFits : public DataFileImp
|
|---|
| 465 | {
|
|---|
| 466 | CCfits::FITS* fFile; /// The pointer to the CCfits FITS file
|
|---|
| 467 | CCfits::Table* fTable; /// The pointer to the CCfits binary table
|
|---|
| 468 |
|
|---|
| 469 | uint64_t fNumRows; ///the number of rows that have been written already to the FITS file.
|
|---|
| 470 |
|
|---|
| 471 | Converter *fConv;
|
|---|
| 472 |
|
|---|
| 473 | public:
|
|---|
| 474 | DataFileFits(uint32_t runid, MessageImp &imp) :
|
|---|
| 475 | DataFileImp(runid, imp), fFile(0), fNumRows(0), fConv(0)
|
|---|
| 476 | {
|
|---|
| 477 | }
|
|---|
| 478 |
|
|---|
| 479 | // --------------------------------------------------------------------------
|
|---|
| 480 | //
|
|---|
| 481 | //! Default destructor
|
|---|
| 482 | //! The Fits file SHOULD have been closed already, otherwise the informations
|
|---|
| 483 | //! related to the RUN_TAIL will NOT be written to the file.
|
|---|
| 484 | //
|
|---|
| 485 | ~DataFileFits() { Close(); delete fConv; }
|
|---|
| 486 |
|
|---|
| 487 | // --------------------------------------------------------------------------
|
|---|
| 488 | //
|
|---|
| 489 | //! Add a new column to the vectors storing the column data.
|
|---|
| 490 | //! @param names the vector of string storing the columns names
|
|---|
| 491 | //! @param types the vector of string storing the FITS data format
|
|---|
| 492 | //! @param numElems the number of elements in this column
|
|---|
| 493 | //! @param type the char describing the FITS data format
|
|---|
| 494 | //! @param name the name of the particular column to be added.
|
|---|
| 495 | //
|
|---|
| 496 | inline void AddColumnEntry(vector<string>& names, vector<string>& types, int numElems, char type, string name)
|
|---|
| 497 | {
|
|---|
| 498 | names.push_back(name);
|
|---|
| 499 |
|
|---|
| 500 | ostringstream str;
|
|---|
| 501 | if (numElems != 1)
|
|---|
| 502 | str << numElems;
|
|---|
| 503 | str << type;
|
|---|
| 504 | types.push_back(str.str());
|
|---|
| 505 | }
|
|---|
| 506 |
|
|---|
| 507 | // --------------------------------------------------------------------------
|
|---|
| 508 | //
|
|---|
| 509 | //! Writes a single header key entry
|
|---|
| 510 | //! @param name the name of the key
|
|---|
| 511 | //! @param value its value
|
|---|
| 512 | //! @param comment the comment associated to that key
|
|---|
| 513 | //
|
|---|
| 514 | //FIXME this function is a duplicate from the class Fits. should we try to merge it ?
|
|---|
| 515 | template <typename T>
|
|---|
| 516 | void WriteKey(const string &name, const T &value, const string &comment)
|
|---|
| 517 | {
|
|---|
| 518 | try
|
|---|
| 519 | {
|
|---|
| 520 | fTable->addKey(name, value, comment);
|
|---|
| 521 | }
|
|---|
| 522 | catch (CCfits::FitsException e)
|
|---|
| 523 | {
|
|---|
| 524 | ostringstream str;
|
|---|
| 525 | str << "Could not add header key " << name;
|
|---|
| 526 | Error(str);
|
|---|
| 527 | }
|
|---|
| 528 | }
|
|---|
| 529 |
|
|---|
| 530 | template <typename T>
|
|---|
| 531 | void WriteKey(const string &name, const int idx, const T &value, const string &comment)
|
|---|
| 532 | {
|
|---|
| 533 | ostringstream str;
|
|---|
| 534 | str << name << idx;
|
|---|
| 535 |
|
|---|
| 536 | ostringstream com;
|
|---|
| 537 | com << "Board " << setw(2) << idx << ": " << comment;
|
|---|
| 538 |
|
|---|
| 539 | WriteKey(str.str(), value, com.str());
|
|---|
| 540 | }
|
|---|
| 541 |
|
|---|
| 542 | // --------------------------------------------------------------------------
|
|---|
| 543 | //
|
|---|
| 544 | //! DataFileFits constructor. This is the one that should be used, not the default one (parameter-less)
|
|---|
| 545 | //! @param runid This parameter should probably be removed. I first thought it was the run number, but apparently it is not
|
|---|
| 546 | //! @param h a pointer to the RUN_HEAD structure that contains the informations relative to this run
|
|---|
| 547 | //
|
|---|
| 548 | bool OpenFile(RUN_HEAD* h)
|
|---|
| 549 | {
|
|---|
| 550 | //Form filename, based on runid and run-type
|
|---|
| 551 | const string fileName = FormFileName(GetRunId(), "fits");
|
|---|
| 552 | if (access(fileName.c_str(), F_OK)==0)
|
|---|
| 553 | {
|
|---|
| 554 | Error("File '"+fileName+"' already exists.");
|
|---|
| 555 | return false;
|
|---|
| 556 | }
|
|---|
| 557 |
|
|---|
| 558 | fFileName = fileName;
|
|---|
| 559 |
|
|---|
| 560 | /*
|
|---|
| 561 | out <<
|
|---|
| 562 | "SIMPLE = T / file does conform to FITS standard "
|
|---|
| 563 | "BITPIX = 8 / number of bits per data pixel "
|
|---|
| 564 | "NAXIS = 0 / number of data axes "
|
|---|
| 565 | "EXTEND = T / FITS dataset may contain extensions "
|
|---|
| 566 | "COMMENT FITS (Flexible Image Transport System) format is defined in 'Astronomy"
|
|---|
| 567 | "COMMENT and Astrophysics', volume 376, page 359; bibcode: 2001A&A...376..359H "
|
|---|
| 568 | "END ";
|
|---|
| 569 | for (int i=0; i<29; i++)
|
|---|
| 570 | out << " "
|
|---|
| 571 | */
|
|---|
| 572 |
|
|---|
| 573 | //create the FITS object
|
|---|
| 574 | try
|
|---|
| 575 | {
|
|---|
| 576 | fFile = new CCfits::FITS(fileName, CCfits::RWmode::Write);
|
|---|
| 577 | }
|
|---|
| 578 | catch (CCfits::FitsException e)
|
|---|
| 579 | {
|
|---|
| 580 | ostringstream str;
|
|---|
| 581 | str << "Could not open FITS file " << fileName << ": " << e.message();
|
|---|
| 582 | Error(str);
|
|---|
| 583 | return false;
|
|---|
| 584 | }
|
|---|
| 585 |
|
|---|
| 586 | h->NroiTM = 0;
|
|---|
| 587 |
|
|---|
| 588 | vector<string> colNames;
|
|---|
| 589 | vector<string> dataTypes;
|
|---|
| 590 | AddColumnEntry(colNames, dataTypes, 1, 'J', "EventNum");
|
|---|
| 591 | AddColumnEntry(colNames, dataTypes, 1, 'I', "TriggerType");
|
|---|
| 592 | AddColumnEntry(colNames, dataTypes, 1, 'J', "NumBoards");
|
|---|
| 593 | AddColumnEntry(colNames, dataTypes, 1, 'J', "reserved");
|
|---|
| 594 | AddColumnEntry(colNames, dataTypes, 1, 'J', "SoftTrig");
|
|---|
| 595 | AddColumnEntry(colNames, dataTypes, 2, 'J', "PCTime");
|
|---|
| 596 | AddColumnEntry(colNames, dataTypes, h->NBoard, 'J', "BoardTime");
|
|---|
| 597 | AddColumnEntry(colNames, dataTypes, h->NPix, 'I', "StartCellData");
|
|---|
| 598 | AddColumnEntry(colNames, dataTypes, h->NTm, 'I', "StartCellTimeMarker");
|
|---|
| 599 | AddColumnEntry(colNames, dataTypes, h->NPix*h->Nroi, 'I', "Data");
|
|---|
| 600 | AddColumnEntry(colNames, dataTypes, h->NTm*h->NroiTM, 'I', "TimeMarker");
|
|---|
| 601 |
|
|---|
| 602 | // Write length of physical pipeline (1024)
|
|---|
| 603 |
|
|---|
| 604 | /*
|
|---|
| 605 | ostringstream fmt;
|
|---|
| 606 | fmt << "I:1"; // uint32_t EventNum
|
|---|
| 607 | fmt << ";S:1"; // uint16_t TriggerType
|
|---|
| 608 | fmt << ";I:5"; // uint32_t NumBoards, reserved, SoftTrig, PCTime, PCUsec
|
|---|
| 609 | fmt << ";I:" << h->NBoard; // uint32_t BoardTime[NBOARDS]
|
|---|
| 610 | fmt << ";S:" << h->NPix+h->NTm; // int16_t StartPix[NPIX], StartTM[NTMARK]
|
|---|
| 611 | fmt << ";S:" << h->NPix*h->Nroi + h->NTm*h->NroiTM; // int16_t Adc_Data[]
|
|---|
| 612 | */
|
|---|
| 613 | fConv = new Converter(Converter::ToFormat(dataTypes));
|
|---|
| 614 |
|
|---|
| 615 | //actually create the table
|
|---|
| 616 | try
|
|---|
| 617 | {
|
|---|
| 618 | fTable = fFile->addTable("Events", 0, colNames, dataTypes);
|
|---|
| 619 | }
|
|---|
| 620 | catch (const CCfits::FitsException &e)
|
|---|
| 621 | {
|
|---|
| 622 | ostringstream str;
|
|---|
| 623 | str << "Could not create FITS table 'Events' in file " << fileName << " reason: " << e.message();
|
|---|
| 624 | Error(str);
|
|---|
| 625 | return false;
|
|---|
| 626 | }
|
|---|
| 627 |
|
|---|
| 628 | if (fTable->rows() != 0)
|
|---|
| 629 | {
|
|---|
| 630 | Error("FITS table created on the fly looks non-empty.");
|
|---|
| 631 | return false;
|
|---|
| 632 | }
|
|---|
| 633 |
|
|---|
| 634 | //write header data
|
|---|
| 635 | //first the "standard" keys
|
|---|
| 636 | WriteKey("EXTREL", 1.0f, "Release Number");
|
|---|
| 637 | WriteKey("TELESCOP", "FACT", "Telescope that acquired this data");
|
|---|
| 638 | WriteKey("ORIGIN", "ISDC", "Institution that wrote the file");
|
|---|
| 639 | WriteKey("CREATOR", "fadctrl", "Program that wrote this file (FACT++ Event Builder)");
|
|---|
| 640 |
|
|---|
| 641 | WriteKey("PACKAGE", PACKAGE_NAME, "Package name");
|
|---|
| 642 | WriteKey("VERSION", PACKAGE_VERSION, "Package description");
|
|---|
| 643 | WriteKey("COMPILED", __DATE__" "__TIME__, "Compile time");
|
|---|
| 644 | WriteKey("REVISION", REVISION, "SVN revision");
|
|---|
| 645 | //WriteKey("CONTACT", PACKAGE_BUGREPORT, "Current package maintainer");
|
|---|
| 646 | //WriteKey("URL", PACKAGE_URL, "Current repositiory location");
|
|---|
| 647 |
|
|---|
| 648 | WriteKey("BLDVER", h->Version, "Builder version");
|
|---|
| 649 | WriteKey("RUNID", GetRunId(), "Run number");
|
|---|
| 650 | WriteKey("RUNTYPE", h->RunType, "Type of run");
|
|---|
| 651 | WriteKey("NBOARD", h->NBoard, "Number of acquisition boards");
|
|---|
| 652 | WriteKey("NPIX", h->NPix, "Number of pixels");
|
|---|
| 653 | WriteKey("NTMARK", h->NTm, "Number of Time marks");
|
|---|
| 654 | WriteKey("NROI", h->Nroi, "Number of slices per pixels");
|
|---|
| 655 | WriteKey("NROITM", h->NroiTM, "Number of slices per time-marker");
|
|---|
| 656 |
|
|---|
| 657 | const Time now;
|
|---|
| 658 | WriteKey("TIMESYS", "UTC", "Time system");
|
|---|
| 659 | WriteKey("DATE", now.Iso(), "File creation date");
|
|---|
| 660 | WriteKey("NIGHT", now.NightAsInt(), "Night as int");
|
|---|
| 661 |
|
|---|
| 662 | //FIXME should we also put the start and stop time of the received data ?
|
|---|
| 663 | //now the events header related variables
|
|---|
| 664 | WriteKey("CAMERA", "MGeomCamFACT", "");
|
|---|
| 665 | WriteKey("DAQ", "DRS4", "");
|
|---|
| 666 |
|
|---|
| 667 | WriteKey("ADCCOUNT", 2.0f, "ADC Counts per milliVolt");
|
|---|
| 668 |
|
|---|
| 669 | Info("==> TODO: Write sampling frequency...");
|
|---|
| 670 |
|
|---|
| 671 | // Write a single key for:
|
|---|
| 672 | // -----------------------
|
|---|
| 673 | // Start package flag
|
|---|
| 674 | // package length
|
|---|
| 675 | // version number
|
|---|
| 676 | // status
|
|---|
| 677 | // Prescaler
|
|---|
| 678 |
|
|---|
| 679 | // Write 40 kays for (?)
|
|---|
| 680 | // Phaseshift
|
|---|
| 681 | // DAC
|
|---|
| 682 |
|
|---|
| 683 | for (int i=0; i<h->NBoard; i++)
|
|---|
| 684 | {
|
|---|
| 685 | const PEVNT_HEADER &hh = h->FADhead[i];
|
|---|
| 686 |
|
|---|
| 687 | // Header values whihc won't change during the run
|
|---|
| 688 | WriteKey("ID", i, hh.board_id, "Board ID");
|
|---|
| 689 | WriteKey("DNA", i, hh.DNA, "DNA");
|
|---|
| 690 | WriteKey("FWVER", i, hh.version_no, "Firmware Version");
|
|---|
| 691 | }
|
|---|
| 692 |
|
|---|
| 693 |
|
|---|
| 694 | /*
|
|---|
| 695 | //now the boards related keywords
|
|---|
| 696 | for (int i=0; i<h->NBoard; i++)
|
|---|
| 697 | {
|
|---|
| 698 | const PEVNT_HEADER &hh = h->FADhead[i];
|
|---|
| 699 |
|
|---|
| 700 | WriteKey("STPKGFG", i, hh.start_package_flag, "Start package flag");
|
|---|
| 701 | WriteKey("PKGLEN", i, hh.package_length, "Package length");
|
|---|
| 702 | WriteKey("STATUS", i, hh.PLLLCK, "");
|
|---|
| 703 |
|
|---|
| 704 | // WriteKey("TRIGCRC", i, hh.trigger_crc, "Trigger CRC");
|
|---|
| 705 | // WriteKey("TRIGTYP", i, hh.trigger_type, "Trigger type");
|
|---|
| 706 | // WriteKey("TRIGID", i, hh.trigger_id, "Trigger ID");
|
|---|
| 707 | // WriteKey("EVTCNTR", i, hh.fad_evt_counter, "FAD Event Counter");
|
|---|
| 708 | // WriteKey("REFCLK", i, hh.REFCLK_frequency, "Reference Clock Frequency");
|
|---|
| 709 |
|
|---|
| 710 | WriteKey("PHASESH", i, hh.adc_clock_phase_shift, "ADC clock phase shift");
|
|---|
| 711 | WriteKey("TRGGEN", i, hh.number_of_triggers_to_generate, "Number of triggers to generate");
|
|---|
| 712 | WriteKey("PRESC", i, hh.trigger_generator_prescaler, "Trigger generator prescaler");
|
|---|
| 713 | WriteKey("RUNNB", i, hh.runnumber, "Run number");
|
|---|
| 714 |
|
|---|
| 715 | WriteKey("TIME", i, hh.time, "Time");
|
|---|
| 716 |
|
|---|
| 717 | // for (int j=0;j<NTemp;j++)
|
|---|
| 718 | // {
|
|---|
| 719 | // str.str(""); str2.str("");
|
|---|
| 720 | // str << "DRS_T" << i << j;
|
|---|
| 721 | // str2 << "DRS temperature #" << i << " " << j;
|
|---|
| 722 | // WriteKey(str.str(), h->FADhead[i].drs_temperature[j], str2.str());
|
|---|
| 723 | // }
|
|---|
| 724 | for (int j=0;j<NDAC;j++)
|
|---|
| 725 | WriteKey("DAC", i*NDAC+j, hh.dac[j], "DAC");
|
|---|
| 726 | }
|
|---|
| 727 | */
|
|---|
| 728 |
|
|---|
| 729 | //Last but not least, add header keys that will be updated when closing the file
|
|---|
| 730 | WriteFooter(NULL);
|
|---|
| 731 |
|
|---|
| 732 | return true;
|
|---|
| 733 | }
|
|---|
| 734 |
|
|---|
| 735 |
|
|---|
| 736 | int WriteColumns(size_t &start, size_t size, const void *e)
|
|---|
| 737 | {
|
|---|
| 738 | int status = 0;
|
|---|
| 739 | fits_write_tblbytes(fFile->fitsPointer(), fNumRows, start, size,
|
|---|
| 740 | (unsigned char*)e, &status);
|
|---|
| 741 | if (status)
|
|---|
| 742 | {
|
|---|
| 743 | char text[30];//max length of cfitsio error strings (from doc)
|
|---|
| 744 | fits_get_errstatus(status, text);
|
|---|
| 745 |
|
|---|
| 746 | ostringstream str;
|
|---|
| 747 | str << "Writing FITS row " << fNumRows << ": " << text << " (file_write_tblbytes, rc=" << status << ")";
|
|---|
| 748 | Error(str);
|
|---|
| 749 | }
|
|---|
| 750 |
|
|---|
| 751 | start += size;
|
|---|
| 752 | return status;
|
|---|
| 753 | }
|
|---|
| 754 |
|
|---|
| 755 | // --------------------------------------------------------------------------
|
|---|
| 756 | //
|
|---|
| 757 | //! This writes one event to the file
|
|---|
| 758 | //! @param e the pointer to the EVENT
|
|---|
| 759 | //
|
|---|
| 760 | virtual bool WriteEvt(EVENT *e)
|
|---|
| 761 | {
|
|---|
| 762 | //FIXME As discussed earlier, we do not swap the bytes yet.
|
|---|
| 763 | fTable->makeThisCurrent();
|
|---|
| 764 |
|
|---|
| 765 | //insert a new row
|
|---|
| 766 | int status(0);
|
|---|
| 767 | if (fits_insert_rows(fTable->fitsPointer(), fNumRows, 1, &status))
|
|---|
| 768 | {
|
|---|
| 769 | char text[30];//max length of cfitsio error strings (from doc)
|
|---|
| 770 | fits_get_errstatus(status, text);
|
|---|
| 771 |
|
|---|
| 772 | ostringstream str;
|
|---|
| 773 | str << "Inserting row " << fNumRows << " into " << fFileName << ": " << text << " (fits_insert_rows, rc=" << status << ")";
|
|---|
| 774 | Error(str);
|
|---|
| 775 |
|
|---|
| 776 | return false;
|
|---|
| 777 | }
|
|---|
| 778 | fNumRows++;
|
|---|
| 779 |
|
|---|
| 780 | // FIXME: Get NPIX and NTMARK from header
|
|---|
| 781 | const size_t sz = sizeof(EVENT) + sizeof(e->StartPix)*e->Roi+sizeof(e->StartTM)*e->RoiTM;
|
|---|
| 782 |
|
|---|
| 783 | const vector<char> data = fConv->ToFits(reinterpret_cast<char*>(e)+4, sz-4);
|
|---|
| 784 |
|
|---|
| 785 | // column size pointer
|
|---|
| 786 | size_t col = 1;
|
|---|
| 787 | if (!WriteColumns(col, data.size(), data.data()))
|
|---|
| 788 | return true;
|
|---|
| 789 |
|
|---|
| 790 | //TODO output an error
|
|---|
| 791 | return false;
|
|---|
| 792 |
|
|---|
| 793 | /*
|
|---|
| 794 | //write the data, chunk by chunk
|
|---|
| 795 | //FIXME hard-coded size corresponds to current variables of the event, in bytes.
|
|---|
| 796 | //FIXME no padding was taken into account. Because smallest member is 2 bytes, I don't think that this should be a problem.
|
|---|
| 797 | const long sizeInBytesOfEventBeforePointers = 16;
|
|---|
| 798 |
|
|---|
| 799 | long col = 1;
|
|---|
| 800 | if (FitsWriteTblBytes(col, sizeInBytesOfEventBeforePointers, e))
|
|---|
| 801 | {
|
|---|
| 802 | //TODO output an error
|
|---|
| 803 | return false;
|
|---|
| 804 | }
|
|---|
| 805 | if (FitsWriteTblBytes(col, NBOARDS*2, e->BoardTime))
|
|---|
| 806 | {
|
|---|
| 807 | //TODO output an error
|
|---|
| 808 | return false;
|
|---|
| 809 | }
|
|---|
| 810 | if (FitsWriteTblBytes(col, NPIX*2, e->StartPix))
|
|---|
| 811 | {
|
|---|
| 812 | //TODO output an error
|
|---|
| 813 | return false;
|
|---|
| 814 | }
|
|---|
| 815 | if (FitsWriteTblBytes(col, NTMARK*2, e->StartTM))
|
|---|
| 816 | {
|
|---|
| 817 | //TODO output an error
|
|---|
| 818 | return false;
|
|---|
| 819 | }
|
|---|
| 820 | if (FitsWriteTblBytes(col, NPIX*fRoi*2, e->Adc_Data))
|
|---|
| 821 | {
|
|---|
| 822 | //TODO output an error
|
|---|
| 823 | return false;
|
|---|
| 824 | }
|
|---|
| 825 | return true;*/
|
|---|
| 826 | }
|
|---|
| 827 |
|
|---|
| 828 | void WriteFooter(RUN_TAIL *rt)
|
|---|
| 829 | {
|
|---|
| 830 | //write final header keys
|
|---|
| 831 | fTable->makeThisCurrent();
|
|---|
| 832 |
|
|---|
| 833 | WriteKey("NBEVTOK", rt ? rt->nEventsOk : uint32_t(0),
|
|---|
| 834 | "How many events were written");
|
|---|
| 835 |
|
|---|
| 836 | WriteKey("NBEVTREJ", rt ? rt->nEventsRej : uint32_t(0),
|
|---|
| 837 | "How many events were rejected by SW-trig");
|
|---|
| 838 |
|
|---|
| 839 | WriteKey("NBEVTBAD", rt ? rt->nEventsBad : uint32_t(0),
|
|---|
| 840 | "How many events were rejected by Error");
|
|---|
| 841 |
|
|---|
| 842 | //FIXME shouldn't we convert start and stop time to MjD first ?
|
|---|
| 843 | //FIXME shouldn't we also add an MjD reference ?
|
|---|
| 844 |
|
|---|
| 845 | WriteKey("TSTART", rt ? rt->PCtime0 : uint32_t(0),
|
|---|
| 846 | "Time when first event received");
|
|---|
| 847 |
|
|---|
| 848 | WriteKey("TSTOP", rt ? rt->PCtimeX : uint32_t(0),
|
|---|
| 849 | "Time when last event received");
|
|---|
| 850 | }
|
|---|
| 851 |
|
|---|
| 852 | // --------------------------------------------------------------------------
|
|---|
| 853 | //
|
|---|
| 854 | //! Closes the file, and before this it write the TAIL data
|
|---|
| 855 | //! @param rt the pointer to the RUN_TAIL data structure
|
|---|
| 856 | //
|
|---|
| 857 | virtual bool Close(RUN_TAIL *rt = 0)
|
|---|
| 858 | {
|
|---|
| 859 | if (!fFile)
|
|---|
| 860 | return false;
|
|---|
| 861 |
|
|---|
| 862 | WriteFooter(rt);
|
|---|
| 863 |
|
|---|
| 864 | delete fFile;
|
|---|
| 865 | fFile = NULL;
|
|---|
| 866 |
|
|---|
| 867 | return true;
|
|---|
| 868 | }
|
|---|
| 869 |
|
|---|
| 870 | };
|
|---|
| 871 | #else
|
|---|
| 872 | #define DataFileFits DataFileRaw
|
|---|
| 873 | #endif
|
|---|
| 874 |
|
|---|
| 875 | #include "DimWriteStatistics.h"
|
|---|
| 876 |
|
|---|
| 877 | class EventBuilderWrapper
|
|---|
| 878 | {
|
|---|
| 879 | public:
|
|---|
| 880 | // FIXME
|
|---|
| 881 | static EventBuilderWrapper *This;
|
|---|
| 882 |
|
|---|
| 883 | MessageImp &fMsg;
|
|---|
| 884 |
|
|---|
| 885 | private:
|
|---|
| 886 | boost::thread fThread;
|
|---|
| 887 |
|
|---|
| 888 | enum CommandStates_t // g_runStat
|
|---|
| 889 | {
|
|---|
| 890 | kAbort = -2, // quit as soon as possible ('abort')
|
|---|
| 891 | kExit = -1, // stop reading, quit when buffered events done ('exit')
|
|---|
| 892 | kInitialize = 0, // 'initialize' (e.g. dim not yet started)
|
|---|
| 893 | kHybernate = 1, // do nothing for long time ('hybernate') [wakeup within ~1sec]
|
|---|
| 894 | kSleep = 2, // do nothing ('sleep') [wakeup within ~10msec]
|
|---|
| 895 | kModeFlush = 10, // read data from camera, but skip them ('flush')
|
|---|
| 896 | kModeTest = 20, // read data and process them, but do not write to disk ('test')
|
|---|
| 897 | kModeFlag = 30, // read data, process and write all to disk ('flag')
|
|---|
| 898 | kModeRun = 40, // read data, process and write selected to disk ('run')
|
|---|
| 899 | };
|
|---|
| 900 |
|
|---|
| 901 | enum
|
|---|
| 902 | {
|
|---|
| 903 | kCurrent = 0,
|
|---|
| 904 | kTotal = 1,
|
|---|
| 905 | kEventId = 2,
|
|---|
| 906 | kTriggerId = 3,
|
|---|
| 907 | };
|
|---|
| 908 |
|
|---|
| 909 | enum FileFormat_t
|
|---|
| 910 | {
|
|---|
| 911 | kNone = 0,
|
|---|
| 912 | kDebug,
|
|---|
| 913 | kFits,
|
|---|
| 914 | kRaw,
|
|---|
| 915 | kCalib
|
|---|
| 916 | };
|
|---|
| 917 |
|
|---|
| 918 | FileFormat_t fFileFormat;
|
|---|
| 919 |
|
|---|
| 920 |
|
|---|
| 921 | uint32_t fMaxRun;
|
|---|
| 922 | uint32_t fLastOpened;
|
|---|
| 923 | uint32_t fLastClosed;
|
|---|
| 924 | uint32_t fNumEvts[4];
|
|---|
| 925 |
|
|---|
| 926 | DimWriteStatistics fDimWriteStats;
|
|---|
| 927 | DimDescribedService fDimRuns;
|
|---|
| 928 | DimDescribedService fDimEvents;
|
|---|
| 929 | DimDescribedService fDimEventData;
|
|---|
| 930 | DimDescribedService fDimFwVersion;
|
|---|
| 931 | DimDescribedService fDimRunNumber;
|
|---|
| 932 | DimDescribedService fDimStatus;
|
|---|
| 933 | DimDescribedService fDimDNA;
|
|---|
| 934 | DimDescribedService fDimTemperature;
|
|---|
| 935 | DimDescribedService fDimPrescaler;
|
|---|
| 936 | DimDescribedService fDimRefClock;
|
|---|
| 937 | DimDescribedService fDimRoi;
|
|---|
| 938 | DimDescribedService fDimDac;
|
|---|
| 939 | DimDescribedService fDimStatistics1;
|
|---|
| 940 | DimDescribedService fDimStatistics2;
|
|---|
| 941 |
|
|---|
| 942 | bool fDebugStream;
|
|---|
| 943 | bool fDebugRead;
|
|---|
| 944 | bool fDebugLog;
|
|---|
| 945 |
|
|---|
| 946 | uint32_t fRunNumber;
|
|---|
| 947 |
|
|---|
| 948 | void InitRunNumber()
|
|---|
| 949 | {
|
|---|
| 950 | // FIXME: Add a check that we are not too close to noon!
|
|---|
| 951 | //const int night = Time().NightAsInt();
|
|---|
| 952 |
|
|---|
| 953 | fRunNumber = 1000;
|
|---|
| 954 |
|
|---|
| 955 | while (--fRunNumber>0)
|
|---|
| 956 | {
|
|---|
| 957 | const string name = DataFileImp::FormFileName(fRunNumber, "");
|
|---|
| 958 |
|
|---|
| 959 | if (access((name+"bin").c_str(), F_OK) == 0)
|
|---|
| 960 | break;
|
|---|
| 961 | if (access((name+"fits").c_str(), F_OK) == 0)
|
|---|
| 962 | break;
|
|---|
| 963 | }
|
|---|
| 964 |
|
|---|
| 965 | fRunNumber++;
|
|---|
| 966 |
|
|---|
| 967 | ostringstream str;
|
|---|
| 968 | str << "Starting with run number " << fRunNumber;
|
|---|
| 969 | fMsg.Message(str);
|
|---|
| 970 |
|
|---|
| 971 | fMsg.Info(" ==> TODO: Run-number detection doesn't work when noon passes!");
|
|---|
| 972 | fMsg.Info(" ==> TODO: Crosscheck with database!");
|
|---|
| 973 | }
|
|---|
| 974 |
|
|---|
| 975 | public:
|
|---|
| 976 | EventBuilderWrapper(MessageImp &imp) : fMsg(imp),
|
|---|
| 977 | fFileFormat(kNone), fMaxRun(0), fLastOpened(0), fLastClosed(0),
|
|---|
| 978 | fDimWriteStats ("FAD_CONTROL", imp),
|
|---|
| 979 | fDimRuns ("FAD_CONTROL/RUNS", "I:5;C", ""),
|
|---|
| 980 | fDimEvents ("FAD_CONTROL/EVENTS", "I:4", ""),
|
|---|
| 981 | fDimEventData ("FAD_CONTROL/EVENT_DATA", "S:1;I:1;S:1;I:1;I:2;I:40;S:1440;S:160;F", ""),
|
|---|
| 982 | fDimFwVersion ("FAD_CONTROL/FIRMWARE_VERSION", "F:42", ""),
|
|---|
| 983 | fDimRunNumber ("FAD_CONTROL/RUN_NUMBER", "I:42", ""),
|
|---|
| 984 | fDimStatus ("FAD_CONTROL/STATUS", "S:42", ""),
|
|---|
| 985 | fDimDNA ("FAD_CONTROL/DNA", "X:40", ""),
|
|---|
| 986 | fDimTemperature ("FAD_CONTROL/TEMPERATURE", "F:82", ""),
|
|---|
| 987 | fDimPrescaler ("FAD_CONTROL/PRESCALER", "S:42", ""),
|
|---|
| 988 | fDimRefClock ("FAD_CONTROL/REFERENCE_CLOCK", "I:42", ""),
|
|---|
| 989 | fDimRoi ("FAD_CONTROL/REGION_OF_INTEREST", "S:2", ""),
|
|---|
| 990 | fDimDac ("FAD_CONTROL/DAC", "S:336", ""),
|
|---|
| 991 | fDimStatistics1 ("FAD_CONTROL/STATISTICS1", "I:3;I:5;X:4;I:3;I:3;I:40;I:1;I:2;C:40;I:40;I:40;X:40", ""),
|
|---|
| 992 | fDimStatistics2 ("FAD_CONTROL/STATISTICS2", "I:1;I:280;X:40;I:40;I:4;I:4;I:2;I:2;I:3;C:40", ""),
|
|---|
| 993 | fDebugStream(false), fDebugRead(false), fDebugLog(false)
|
|---|
| 994 | {
|
|---|
| 995 | if (This)
|
|---|
| 996 | throw logic_error("EventBuilderWrapper cannot be instantiated twice.");
|
|---|
| 997 |
|
|---|
| 998 | This = this;
|
|---|
| 999 |
|
|---|
| 1000 | memset(fNumEvts, 0, sizeof(fNumEvts));
|
|---|
| 1001 |
|
|---|
| 1002 | fDimEvents.Update(fNumEvts);
|
|---|
| 1003 |
|
|---|
| 1004 | for (size_t i=0; i<40; i++)
|
|---|
| 1005 | ConnectSlot(i, tcp::endpoint());
|
|---|
| 1006 |
|
|---|
| 1007 | InitRunNumber();
|
|---|
| 1008 | }
|
|---|
| 1009 | virtual ~EventBuilderWrapper()
|
|---|
| 1010 | {
|
|---|
| 1011 | Abort();
|
|---|
| 1012 |
|
|---|
| 1013 | // FIXME: Used timed_join and abort afterwards
|
|---|
| 1014 | // What's the maximum time the eb need to abort?
|
|---|
| 1015 | fThread.join();
|
|---|
| 1016 | //ffMsg.Info("EventBuilder stopped.");
|
|---|
| 1017 |
|
|---|
| 1018 | for (vector<DataFileImp*>::iterator it=fFiles.begin(); it!=fFiles.end(); it++)
|
|---|
| 1019 | delete *it;
|
|---|
| 1020 | }
|
|---|
| 1021 |
|
|---|
| 1022 | struct RunDescription
|
|---|
| 1023 | {
|
|---|
| 1024 | uint32_t maxtime;
|
|---|
| 1025 | uint32_t maxevt;
|
|---|
| 1026 |
|
|---|
| 1027 | FAD::Configuration reference;
|
|---|
| 1028 | };
|
|---|
| 1029 |
|
|---|
| 1030 | map<uint32_t, RunDescription> fExpectedRuns;
|
|---|
| 1031 |
|
|---|
| 1032 | uint32_t StartNewRun(int64_t maxtime, int64_t maxevt, const FAD::Configuration &ref)
|
|---|
| 1033 | {
|
|---|
| 1034 | if (maxtime<=0 || maxtime>24*60*60)
|
|---|
| 1035 | maxtime = 24*60*60;
|
|---|
| 1036 | if (maxevt<=0 || maxevt>INT32_MAX)
|
|---|
| 1037 | maxevt = INT32_MAX;
|
|---|
| 1038 |
|
|---|
| 1039 | const RunDescription descr =
|
|---|
| 1040 | {
|
|---|
| 1041 | uint32_t(maxtime),
|
|---|
| 1042 | uint32_t(maxevt),
|
|---|
| 1043 | ref
|
|---|
| 1044 | };
|
|---|
| 1045 |
|
|---|
| 1046 | // FIMXE: Maybe reset an event counter so that the mcp can count events?
|
|---|
| 1047 |
|
|---|
| 1048 | fExpectedRuns[fRunNumber] = descr;
|
|---|
| 1049 | return fRunNumber++;
|
|---|
| 1050 | }
|
|---|
| 1051 |
|
|---|
| 1052 | bool IsThreadRunning()
|
|---|
| 1053 | {
|
|---|
| 1054 | return !fThread.timed_join(boost::posix_time::microseconds(0));
|
|---|
| 1055 | }
|
|---|
| 1056 |
|
|---|
| 1057 | void SetMaxMemory(unsigned int mb) const
|
|---|
| 1058 | {
|
|---|
| 1059 | /*
|
|---|
| 1060 | if (mb*1000000<GetUsedMemory())
|
|---|
| 1061 | {
|
|---|
| 1062 | // ffMsg.Warn("...");
|
|---|
| 1063 | return;
|
|---|
| 1064 | }*/
|
|---|
| 1065 |
|
|---|
| 1066 | g_maxMem = size_t(mb)*1000000;
|
|---|
| 1067 | }
|
|---|
| 1068 |
|
|---|
| 1069 | void StartThread(const vector<tcp::endpoint> &addr)
|
|---|
| 1070 | {
|
|---|
| 1071 | if (IsThreadRunning())
|
|---|
| 1072 | {
|
|---|
| 1073 | fMsg.Warn("Start - EventBuilder still running");
|
|---|
| 1074 | return;
|
|---|
| 1075 | }
|
|---|
| 1076 |
|
|---|
| 1077 | fLastMessage.clear();
|
|---|
| 1078 |
|
|---|
| 1079 | for (size_t i=0; i<40; i++)
|
|---|
| 1080 | ConnectSlot(i, addr[i]);
|
|---|
| 1081 |
|
|---|
| 1082 | g_runStat = kModeRun;
|
|---|
| 1083 |
|
|---|
| 1084 | fMsg.Message("Starting EventBuilder thread");
|
|---|
| 1085 |
|
|---|
| 1086 | fThread = boost::thread(StartEvtBuild);
|
|---|
| 1087 | }
|
|---|
| 1088 | void ConnectSlot(unsigned int i, const tcp::endpoint &addr)
|
|---|
| 1089 | {
|
|---|
| 1090 | if (i>39)
|
|---|
| 1091 | return;
|
|---|
| 1092 |
|
|---|
| 1093 | if (addr==tcp::endpoint())
|
|---|
| 1094 | {
|
|---|
| 1095 | DisconnectSlot(i);
|
|---|
| 1096 | return;
|
|---|
| 1097 | }
|
|---|
| 1098 |
|
|---|
| 1099 | g_port[i].sockAddr.sin_family = AF_INET;
|
|---|
| 1100 | g_port[i].sockAddr.sin_addr.s_addr = htonl(addr.address().to_v4().to_ulong());
|
|---|
| 1101 | g_port[i].sockAddr.sin_port = htons(addr.port());
|
|---|
| 1102 | // In this order
|
|---|
| 1103 | g_port[i].sockDef = 1;
|
|---|
| 1104 | }
|
|---|
| 1105 | void DisconnectSlot(unsigned int i)
|
|---|
| 1106 | {
|
|---|
| 1107 | if (i>39)
|
|---|
| 1108 | return;
|
|---|
| 1109 |
|
|---|
| 1110 | g_port[i].sockDef = 0;
|
|---|
| 1111 | // In this order
|
|---|
| 1112 | g_port[i].sockAddr.sin_family = AF_INET;
|
|---|
| 1113 | g_port[i].sockAddr.sin_addr.s_addr = 0;
|
|---|
| 1114 | g_port[i].sockAddr.sin_port = 0;
|
|---|
| 1115 | }
|
|---|
| 1116 | void IgnoreSlot(unsigned int i)
|
|---|
| 1117 | {
|
|---|
| 1118 | if (i>39)
|
|---|
| 1119 | return;
|
|---|
| 1120 | if (g_port[i].sockAddr.sin_port==0)
|
|---|
| 1121 | return;
|
|---|
| 1122 |
|
|---|
| 1123 | g_port[i].sockDef = -1;
|
|---|
| 1124 | }
|
|---|
| 1125 |
|
|---|
| 1126 |
|
|---|
| 1127 | void Abort()
|
|---|
| 1128 | {
|
|---|
| 1129 | fMsg.Message("Signal abort to EventBuilder thread...");
|
|---|
| 1130 | g_runStat = kAbort;
|
|---|
| 1131 | }
|
|---|
| 1132 |
|
|---|
| 1133 | void ResetThread(bool soft)
|
|---|
| 1134 | {
|
|---|
| 1135 | /*
|
|---|
| 1136 | if (g_reset > 0)
|
|---|
| 1137 |
|
|---|
| 1138 | * suspend reading
|
|---|
| 1139 | * reset = g_reset;
|
|---|
| 1140 | * g_reset=0
|
|---|
| 1141 |
|
|---|
| 1142 | * reset% 10
|
|---|
| 1143 | == 0 leave event Buffers as they are
|
|---|
| 1144 | == 1 let all buffers drain (write (incomplete) events)
|
|---|
| 1145 | > 1 flush all buffers (do not write buffered events)
|
|---|
| 1146 |
|
|---|
| 1147 | * (reset/10)%10
|
|---|
| 1148 | > 0 close all sockets and destroy them (also free the
|
|---|
| 1149 | allocated read-buffers)
|
|---|
| 1150 | recreate before resuming operation
|
|---|
| 1151 | [ this is more than just close/open that can be
|
|---|
| 1152 | triggered by e.g. close/open the base-socket ]
|
|---|
| 1153 |
|
|---|
| 1154 | * (reset/100)%10
|
|---|
| 1155 | > 0 close all open run-files
|
|---|
| 1156 |
|
|---|
| 1157 | * (reset/1000)
|
|---|
| 1158 | sleep so many seconds before resuming operation
|
|---|
| 1159 | (does not (yet) take into account time left when waiting
|
|---|
| 1160 | for buffers getting empty ...)
|
|---|
| 1161 |
|
|---|
| 1162 | * resume_reading
|
|---|
| 1163 |
|
|---|
| 1164 | */
|
|---|
| 1165 | fMsg.Message("Signal reset to EventBuilder thread...");
|
|---|
| 1166 | g_reset = soft ? 101 : 102;
|
|---|
| 1167 | }
|
|---|
| 1168 |
|
|---|
| 1169 | void Exit()
|
|---|
| 1170 | {
|
|---|
| 1171 | fMsg.Message("Signal exit to EventBuilder thread...");
|
|---|
| 1172 | g_runStat = kExit;
|
|---|
| 1173 | }
|
|---|
| 1174 |
|
|---|
| 1175 | /*
|
|---|
| 1176 | void Wait()
|
|---|
| 1177 | {
|
|---|
| 1178 | fThread.join();
|
|---|
| 1179 | ffMsg.Message("EventBuilder stopped.");
|
|---|
| 1180 | }*/
|
|---|
| 1181 |
|
|---|
| 1182 | void Hybernate() const { g_runStat = kHybernate; }
|
|---|
| 1183 | void Sleep() const { g_runStat = kSleep; }
|
|---|
| 1184 | void FlushMode() const { g_runStat = kModeFlush; }
|
|---|
| 1185 | void TestMode() const { g_runStat = kModeTest; }
|
|---|
| 1186 | void FlagMode() const { g_runStat = kModeFlag; }
|
|---|
| 1187 | void RunMode() const { g_runStat = kModeRun; }
|
|---|
| 1188 |
|
|---|
| 1189 | // FIXME: To be removed
|
|---|
| 1190 | void SetMode(int mode) const { g_runStat = mode; }
|
|---|
| 1191 |
|
|---|
| 1192 | bool IsConnected(int i) const { return gi_NumConnect[i]==7; }
|
|---|
| 1193 | bool IsConnecting(int i) const { return !IsConnected(i) && !IsDisconnected(i); }
|
|---|
| 1194 | bool IsDisconnected(int i) const { return gi_NumConnect[i]<=0 && g_port[i].sockDef==0; }
|
|---|
| 1195 | int GetNumConnected(int i) const { return gi_NumConnect[i]; }
|
|---|
| 1196 |
|
|---|
| 1197 | void SetIgnore(int i, bool b) const { if (g_port[i].sockDef!=0) g_port[i].sockDef=b?-1:1; }
|
|---|
| 1198 | bool IsIgnored(int i) const { return g_port[i].sockDef==-1; }
|
|---|
| 1199 |
|
|---|
| 1200 | void SetOutputFormat(FileFormat_t f)
|
|---|
| 1201 | {
|
|---|
| 1202 | fFileFormat = f;
|
|---|
| 1203 | if (fFileFormat==kCalib)
|
|---|
| 1204 | DataFileCalib::Restart();
|
|---|
| 1205 | }
|
|---|
| 1206 |
|
|---|
| 1207 | void SetDebugLog(bool b) { fDebugLog = b; }
|
|---|
| 1208 |
|
|---|
| 1209 | void SetDebugStream(bool b)
|
|---|
| 1210 | {
|
|---|
| 1211 | fDebugStream = b;
|
|---|
| 1212 | if (b)
|
|---|
| 1213 | return;
|
|---|
| 1214 |
|
|---|
| 1215 | for (int i=0; i<40; i++)
|
|---|
| 1216 | {
|
|---|
| 1217 | if (!fDumpStream[i].is_open())
|
|---|
| 1218 | continue;
|
|---|
| 1219 |
|
|---|
| 1220 | fDumpStream[i].close();
|
|---|
| 1221 |
|
|---|
| 1222 | ostringstream name;
|
|---|
| 1223 | name << "socket_dump-" << setfill('0') << setw(2) << i << ".bin";
|
|---|
| 1224 | fMsg.Message("Closed file '"+name.str()+"'");
|
|---|
| 1225 | }
|
|---|
| 1226 | }
|
|---|
| 1227 |
|
|---|
| 1228 | void SetDebugRead(bool b)
|
|---|
| 1229 | {
|
|---|
| 1230 | fDebugRead = b;
|
|---|
| 1231 | if (b || !fDumpRead.is_open())
|
|---|
| 1232 | return;
|
|---|
| 1233 |
|
|---|
| 1234 | fDumpRead.close();
|
|---|
| 1235 | fMsg.Message("Closed file 'socket_events.txt'");
|
|---|
| 1236 | }
|
|---|
| 1237 |
|
|---|
| 1238 | // size_t GetUsedMemory() const { return gi_usedMem; }
|
|---|
| 1239 |
|
|---|
| 1240 | virtual int CloseOpenFiles() { CloseRunFile(0, 0, 0); return 0; }
|
|---|
| 1241 |
|
|---|
| 1242 |
|
|---|
| 1243 | /*
|
|---|
| 1244 | struct OpenFileToDim
|
|---|
| 1245 | {
|
|---|
| 1246 | int code;
|
|---|
| 1247 | char fileName[FILENAME_MAX];
|
|---|
| 1248 | };
|
|---|
| 1249 |
|
|---|
| 1250 | SignalRunOpened(runid, filename);
|
|---|
| 1251 | // Send num open files
|
|---|
| 1252 | // Send runid, (more info about the run?), filename via dim
|
|---|
| 1253 |
|
|---|
| 1254 | SignalEvtWritten(runid);
|
|---|
| 1255 | // Send num events written of newest file
|
|---|
| 1256 |
|
|---|
| 1257 | SignalRunClose(runid);
|
|---|
| 1258 | // Send new num open files
|
|---|
| 1259 | // Send empty file-name if no file is open
|
|---|
| 1260 |
|
|---|
| 1261 | */
|
|---|
| 1262 |
|
|---|
| 1263 | // -------------- Mapped event builder callbacks ------------------
|
|---|
| 1264 |
|
|---|
| 1265 | void UpdateRuns(const string &fname="")
|
|---|
| 1266 | {
|
|---|
| 1267 | uint32_t values[5] =
|
|---|
| 1268 | {
|
|---|
| 1269 | static_cast<uint32_t>(fFiles.size()),
|
|---|
| 1270 | 0xffffffff,
|
|---|
| 1271 | 0,
|
|---|
| 1272 | fLastOpened,
|
|---|
| 1273 | fLastClosed
|
|---|
| 1274 | };
|
|---|
| 1275 |
|
|---|
| 1276 | for (vector<DataFileImp*>::const_iterator it=fFiles.begin();
|
|---|
| 1277 | it!=fFiles.end(); it++)
|
|---|
| 1278 | {
|
|---|
| 1279 | const DataFileImp *file = *it;
|
|---|
| 1280 |
|
|---|
| 1281 | if (file->GetRunId()<values[1])
|
|---|
| 1282 | values[1] = file->GetRunId();
|
|---|
| 1283 |
|
|---|
| 1284 | if (file->GetRunId()>values[2])
|
|---|
| 1285 | values[2] = file->GetRunId();
|
|---|
| 1286 | }
|
|---|
| 1287 |
|
|---|
| 1288 | fMaxRun = values[2];
|
|---|
| 1289 |
|
|---|
| 1290 | vector<char> data(sizeof(values)+fname.size()+1);
|
|---|
| 1291 | memcpy(data.data(), values, sizeof(values));
|
|---|
| 1292 | strcpy(data.data()+sizeof(values), fname.c_str());
|
|---|
| 1293 |
|
|---|
| 1294 | fDimRuns.Update(data);
|
|---|
| 1295 | }
|
|---|
| 1296 |
|
|---|
| 1297 | vector<DataFileImp*> fFiles;
|
|---|
| 1298 |
|
|---|
| 1299 | FileHandle_t runOpen(uint32_t runid, RUN_HEAD *h, size_t)
|
|---|
| 1300 | {
|
|---|
| 1301 | fMsg.Info(" ==> TODO: Update run configuration in database!");
|
|---|
| 1302 |
|
|---|
| 1303 | // Check if file already exists...
|
|---|
| 1304 | DataFileImp *file = 0;
|
|---|
| 1305 | switch (fFileFormat)
|
|---|
| 1306 | {
|
|---|
| 1307 | case kNone: file = new DataFileNone(runid, fMsg); break;
|
|---|
| 1308 | case kDebug: file = new DataFileDebug(runid, fMsg); break;
|
|---|
| 1309 | case kFits: file = new DataFileFits(runid, fMsg); break;
|
|---|
| 1310 | case kRaw: file = new DataFileRaw(runid, fMsg); break;
|
|---|
| 1311 | case kCalib: file = new DataFileCalib(runid, fMsg); break;
|
|---|
| 1312 | }
|
|---|
| 1313 |
|
|---|
| 1314 | try
|
|---|
| 1315 | {
|
|---|
| 1316 | if (!file->OpenFile(h))
|
|---|
| 1317 | return 0;
|
|---|
| 1318 | }
|
|---|
| 1319 | catch (const exception &e)
|
|---|
| 1320 | {
|
|---|
| 1321 | fMsg.Error("Exception trying to open file: "+string(e.what()));
|
|---|
| 1322 | return 0;
|
|---|
| 1323 | }
|
|---|
| 1324 |
|
|---|
| 1325 | fFiles.push_back(file);
|
|---|
| 1326 |
|
|---|
| 1327 | ostringstream str;
|
|---|
| 1328 | str << "Opened: " << file->GetFileName() << " (" << file->GetRunId() << ")";
|
|---|
| 1329 | fMsg.Info(str);
|
|---|
| 1330 |
|
|---|
| 1331 | fDimWriteStats.FileOpened(file->GetFileName());
|
|---|
| 1332 |
|
|---|
| 1333 | fLastOpened = runid;
|
|---|
| 1334 | UpdateRuns(file->GetFileName());
|
|---|
| 1335 |
|
|---|
| 1336 | fNumEvts[kEventId] = 0;
|
|---|
| 1337 | fNumEvts[kTriggerId] = 0;
|
|---|
| 1338 |
|
|---|
| 1339 | fNumEvts[kCurrent] = 0;
|
|---|
| 1340 | fDimEvents.Update(fNumEvts);
|
|---|
| 1341 | // fDimCurrentEvent.Update(uint32_t(0));
|
|---|
| 1342 |
|
|---|
| 1343 | return reinterpret_cast<FileHandle_t>(file);
|
|---|
| 1344 | }
|
|---|
| 1345 |
|
|---|
| 1346 | int runWrite(FileHandle_t handler, EVENT *e, size_t)
|
|---|
| 1347 | {
|
|---|
| 1348 | DataFileImp *file = reinterpret_cast<DataFileImp*>(handler);
|
|---|
| 1349 |
|
|---|
| 1350 | if (!file->WriteEvt(e))
|
|---|
| 1351 | return -1;
|
|---|
| 1352 |
|
|---|
| 1353 | if (file->GetRunId()==fMaxRun)
|
|---|
| 1354 | {
|
|---|
| 1355 | fNumEvts[kCurrent]++;
|
|---|
| 1356 | fNumEvts[kEventId] = e->EventNum;
|
|---|
| 1357 | fNumEvts[kTriggerId] = e->TriggerType;
|
|---|
| 1358 | }
|
|---|
| 1359 |
|
|---|
| 1360 | fNumEvts[kTotal]++;
|
|---|
| 1361 |
|
|---|
| 1362 | static Time oldt(boost::date_time::neg_infin);
|
|---|
| 1363 | Time newt;
|
|---|
| 1364 | if (newt>oldt+boost::posix_time::seconds(1))
|
|---|
| 1365 | {
|
|---|
| 1366 | fDimEvents.Update(fNumEvts);
|
|---|
| 1367 | oldt = newt;
|
|---|
| 1368 | }
|
|---|
| 1369 |
|
|---|
| 1370 |
|
|---|
| 1371 | // ===> SignalEvtWritten(runid);
|
|---|
| 1372 | // Send num events written of newest file
|
|---|
| 1373 |
|
|---|
| 1374 | /* close run runId (all all runs if runId=0) */
|
|---|
| 1375 | /* return: 0=close scheduled / >0 already closed / <0 does not exist */
|
|---|
| 1376 | //CloseRunFile(file->GetRunId(), time(NULL)+2) ;
|
|---|
| 1377 |
|
|---|
| 1378 | return 0;
|
|---|
| 1379 | }
|
|---|
| 1380 |
|
|---|
| 1381 | int runClose(FileHandle_t handler, RUN_TAIL *tail, size_t)
|
|---|
| 1382 | {
|
|---|
| 1383 | fMsg.Info(" ==> TODO: Update run configuration in database!");
|
|---|
| 1384 |
|
|---|
| 1385 | DataFileImp *file = reinterpret_cast<DataFileImp*>(handler);
|
|---|
| 1386 |
|
|---|
| 1387 | const vector<DataFileImp*>::iterator it = find(fFiles.begin(), fFiles.end(), file);
|
|---|
| 1388 | if (it==fFiles.end())
|
|---|
| 1389 | {
|
|---|
| 1390 | ostringstream str;
|
|---|
| 1391 | str << "File handler (" << handler << ") requested to close by event builder doesn't exist.";
|
|---|
| 1392 | fMsg.Fatal(str);
|
|---|
| 1393 | return -1;
|
|---|
| 1394 | }
|
|---|
| 1395 |
|
|---|
| 1396 | fFiles.erase(it);
|
|---|
| 1397 |
|
|---|
| 1398 | fLastClosed = file->GetRunId();
|
|---|
| 1399 | UpdateRuns();
|
|---|
| 1400 |
|
|---|
| 1401 | fDimEvents.Update(fNumEvts);
|
|---|
| 1402 |
|
|---|
| 1403 | const bool rc = file->Close(tail);
|
|---|
| 1404 | if (!rc)
|
|---|
| 1405 | {
|
|---|
| 1406 | // Error message
|
|---|
| 1407 | }
|
|---|
| 1408 |
|
|---|
| 1409 | ostringstream str;
|
|---|
| 1410 | str << "Closed: " << file->GetFileName() << " (" << file->GetRunId() << ")";
|
|---|
| 1411 | fMsg.Info(str);
|
|---|
| 1412 |
|
|---|
| 1413 | delete file;
|
|---|
| 1414 |
|
|---|
| 1415 | // ==> SignalRunClose(runid);
|
|---|
| 1416 | // Send new num open files
|
|---|
| 1417 | // Send empty file-name if no file is open
|
|---|
| 1418 |
|
|---|
| 1419 | return rc ? 0 : -1;
|
|---|
| 1420 | }
|
|---|
| 1421 |
|
|---|
| 1422 | ofstream fDumpStream[40];
|
|---|
| 1423 |
|
|---|
| 1424 | void debugStream(int isock, void *buf, int len)
|
|---|
| 1425 | {
|
|---|
| 1426 | if (!fDebugStream)
|
|---|
| 1427 | return;
|
|---|
| 1428 |
|
|---|
| 1429 | const int slot = isock/7;
|
|---|
| 1430 | if (slot<0 || slot>39)
|
|---|
| 1431 | return;
|
|---|
| 1432 |
|
|---|
| 1433 | if (!fDumpStream[slot].is_open())
|
|---|
| 1434 | {
|
|---|
| 1435 | ostringstream name;
|
|---|
| 1436 | name << "socket_dump-" << setfill('0') << setw(2) << slot << ".bin";
|
|---|
| 1437 |
|
|---|
| 1438 | fDumpStream[slot].open(name.str().c_str(), ios::app);
|
|---|
| 1439 | if (!fDumpStream[slot])
|
|---|
| 1440 | {
|
|---|
| 1441 | ostringstream str;
|
|---|
| 1442 | str << "Open file '" << name << "': " << strerror(errno) << " (errno=" << errno << ")";
|
|---|
| 1443 | fMsg.Error(str);
|
|---|
| 1444 |
|
|---|
| 1445 | return;
|
|---|
| 1446 | }
|
|---|
| 1447 |
|
|---|
| 1448 | fMsg.Message("Opened file '"+name.str()+"' for writing.");
|
|---|
| 1449 | }
|
|---|
| 1450 |
|
|---|
| 1451 | fDumpStream[slot].write(reinterpret_cast<const char*>(buf), len);
|
|---|
| 1452 | }
|
|---|
| 1453 |
|
|---|
| 1454 | ofstream fDumpRead; // Stream to possibly dump docket events
|
|---|
| 1455 |
|
|---|
| 1456 | void debugRead(int isock, int ibyte, uint32_t event, uint32_t ftmevt, uint32_t runno, int state, uint32_t tsec, uint32_t tusec)
|
|---|
| 1457 | {
|
|---|
| 1458 | // isock = socketID (0-279)
|
|---|
| 1459 | // ibyte = #bytes gelesen
|
|---|
| 1460 | // event = eventId (oder 0 wenn noch nicht bekannt)
|
|---|
| 1461 | // state : 1=finished reading data
|
|---|
| 1462 | // 0=reading data
|
|---|
| 1463 | // -1=start reading data (header)
|
|---|
| 1464 | // -2=start reading data,
|
|---|
| 1465 | // eventId not known yet (too little data)
|
|---|
| 1466 | // tsec, tusec = time when reading seconds, microseconds
|
|---|
| 1467 | //
|
|---|
| 1468 | if (!fDebugRead || ibyte==0)
|
|---|
| 1469 | return;
|
|---|
| 1470 |
|
|---|
| 1471 | if (!fDumpRead.is_open())
|
|---|
| 1472 | {
|
|---|
| 1473 | fDumpRead.open("socket_events.txt", ios::app);
|
|---|
| 1474 | if (!fDumpRead)
|
|---|
| 1475 | {
|
|---|
| 1476 | ostringstream str;
|
|---|
| 1477 | str << "Open file 'socket_events.txt': " << strerror(errno) << " (errno=" << errno << ")";
|
|---|
| 1478 | fMsg.Error(str);
|
|---|
| 1479 |
|
|---|
| 1480 | return;
|
|---|
| 1481 | }
|
|---|
| 1482 |
|
|---|
| 1483 | fMsg.Message("Opened file 'socket_events.txt' for writing.");
|
|---|
| 1484 |
|
|---|
| 1485 | fDumpRead << "# START: " << Time().GetAsStr() << endl;
|
|---|
| 1486 | fDumpRead << "# state time_sec time_usec socket slot runno event_id trigger_id bytes_received" << endl;
|
|---|
| 1487 | }
|
|---|
| 1488 |
|
|---|
| 1489 | fDumpRead
|
|---|
| 1490 | << setw(2) << state << " "
|
|---|
| 1491 | << setw(8) << tsec << " "
|
|---|
| 1492 | << setw(9) << tusec << " "
|
|---|
| 1493 | << setw(3) << isock << " "
|
|---|
| 1494 | << setw(2) << isock/7 << " "
|
|---|
| 1495 | << runno << " "
|
|---|
| 1496 | << event << " "
|
|---|
| 1497 | << ftmevt << " "
|
|---|
| 1498 | << ibyte << endl;
|
|---|
| 1499 | }
|
|---|
| 1500 |
|
|---|
| 1501 | array<uint16_t,2> fVecRoi;
|
|---|
| 1502 |
|
|---|
| 1503 | int eventCheck(PEVNT_HEADER *fadhd, EVENT *event)
|
|---|
| 1504 | {
|
|---|
| 1505 | /*
|
|---|
| 1506 | fadhd[i] ist ein array mit den 40 fad-headers
|
|---|
| 1507 | (falls ein board nicht gelesen wurde, ist start_package_flag =0 )
|
|---|
| 1508 |
|
|---|
| 1509 | event ist die Struktur, die auch die write routine erhaelt;
|
|---|
| 1510 | darin sind im header die 'soll-werte' fuer z.B. eventID
|
|---|
| 1511 | als auch die ADC-Werte (falls Du die brauchst)
|
|---|
| 1512 |
|
|---|
| 1513 | Wenn die routine einen negativen Wert liefert, wird das event
|
|---|
| 1514 | geloescht (nicht an die write-routine weitergeleitet [mind. im Prinzip]
|
|---|
| 1515 | */
|
|---|
| 1516 |
|
|---|
| 1517 | const array<uint16_t,2> roi = {{ event->Roi, event->RoiTM }};
|
|---|
| 1518 |
|
|---|
| 1519 | if (roi!=fVecRoi)
|
|---|
| 1520 | {
|
|---|
| 1521 | Update(fDimRoi, roi);
|
|---|
| 1522 | fVecRoi = roi;
|
|---|
| 1523 | }
|
|---|
| 1524 |
|
|---|
| 1525 | const FAD::EventHeader *beg = reinterpret_cast<FAD::EventHeader*>(fadhd);
|
|---|
| 1526 | const FAD::EventHeader *end = reinterpret_cast<FAD::EventHeader*>(fadhd)+40;
|
|---|
| 1527 |
|
|---|
| 1528 | for (const FAD::EventHeader *ptr=beg; ptr!=end; ptr++)
|
|---|
| 1529 | {
|
|---|
| 1530 | // Event incomplete
|
|---|
| 1531 | //if (ptr->fStartDelimiter==0)
|
|---|
| 1532 | // return -1;
|
|---|
| 1533 |
|
|---|
| 1534 | // Either one of
|
|---|
| 1535 | // * fStatus
|
|---|
| 1536 | // * fRunNumber
|
|---|
| 1537 | // * fEventCounter
|
|---|
| 1538 | // * fAdcClockPhaseShift
|
|---|
| 1539 | // * fTriggerGeneratorPrescaler
|
|---|
| 1540 | // * fDac
|
|---|
| 1541 | // inconsistent
|
|---|
| 1542 |
|
|---|
| 1543 | // FIXME: Produce some output, only once per run or
|
|---|
| 1544 | // minute
|
|---|
| 1545 |
|
|---|
| 1546 | /*
|
|---|
| 1547 | if (*ptr != *beg)
|
|---|
| 1548 | return -1;
|
|---|
| 1549 |
|
|---|
| 1550 | if (ptr->fTriggerType != beg->fTriggerType)
|
|---|
| 1551 | return -1;
|
|---|
| 1552 | if (ptr->fTriggerId != beg->fTriggerId)
|
|---|
| 1553 | return -1;
|
|---|
| 1554 | if (ptr->fVersion != beg->fVersion)
|
|---|
| 1555 | return -1;
|
|---|
| 1556 | */
|
|---|
| 1557 | }
|
|---|
| 1558 |
|
|---|
| 1559 | // check REFCLK_frequency
|
|---|
| 1560 | // check consistency with command configuration
|
|---|
| 1561 | // how to log errors?
|
|---|
| 1562 | // need gotNewRun/closedRun to know it is finished
|
|---|
| 1563 |
|
|---|
| 1564 | static Time oldt(boost::date_time::neg_infin);
|
|---|
| 1565 | Time newt;
|
|---|
| 1566 |
|
|---|
| 1567 | if (newt<oldt+boost::posix_time::seconds(1))
|
|---|
| 1568 | return 0;
|
|---|
| 1569 |
|
|---|
| 1570 | oldt = newt;
|
|---|
| 1571 |
|
|---|
| 1572 | const size_t sz = sizeof(EVENT)+event->Roi*2*1440;
|
|---|
| 1573 |
|
|---|
| 1574 | vector<char> data(sz+event->Roi*2*1440);
|
|---|
| 1575 | memcpy(data.data(), event, sizeof(EVENT));
|
|---|
| 1576 |
|
|---|
| 1577 | DataFileCalib::Apply(reinterpret_cast<float*>(data.data()+sizeof(EVENT)),
|
|---|
| 1578 | event->Adc_Data, event->StartPix, event->Roi);
|
|---|
| 1579 |
|
|---|
| 1580 | fDimEventData.Update(data);
|
|---|
| 1581 |
|
|---|
| 1582 | return 0;
|
|---|
| 1583 | }
|
|---|
| 1584 |
|
|---|
| 1585 | bool IsRunStarted() const
|
|---|
| 1586 | {
|
|---|
| 1587 | return fExpectedRuns.find(fRunNumber-1)==fExpectedRuns.end();
|
|---|
| 1588 | }
|
|---|
| 1589 |
|
|---|
| 1590 | void gotNewRun(int runnr, PEVNT_HEADER */*headers*/)
|
|---|
| 1591 | {
|
|---|
| 1592 | // This function is called even when writing is switched off
|
|---|
| 1593 | const map<uint32_t,RunDescription>::iterator it = fExpectedRuns.find(runnr);
|
|---|
| 1594 | if (it==fExpectedRuns.end())
|
|---|
| 1595 | {
|
|---|
| 1596 | ostringstream str;
|
|---|
| 1597 | str << "gotNewRun - Run " << runnr << " wasn't expected." << endl;
|
|---|
| 1598 | return;
|
|---|
| 1599 | }
|
|---|
| 1600 |
|
|---|
| 1601 | CloseRunFile(runnr, time(NULL)+it->second.maxtime, it->second.maxevt);
|
|---|
| 1602 | // return: 0=close scheduled / >0 already closed / <0 does not exist
|
|---|
| 1603 |
|
|---|
| 1604 | fExpectedRuns.erase(it);
|
|---|
| 1605 | }
|
|---|
| 1606 |
|
|---|
| 1607 | map<boost::thread::id, string> fLastMessage;
|
|---|
| 1608 |
|
|---|
| 1609 | void factOut(int severity, int err, const char *message)
|
|---|
| 1610 | {
|
|---|
| 1611 | if (!fDebugLog && severity==99)
|
|---|
| 1612 | return;
|
|---|
| 1613 |
|
|---|
| 1614 | ostringstream str;
|
|---|
| 1615 | //str << boost::this_thread::get_id() << " ";
|
|---|
| 1616 | str << "EventBuilder(";
|
|---|
| 1617 | if (err<0)
|
|---|
| 1618 | str << "---";
|
|---|
| 1619 | else
|
|---|
| 1620 | str << err;
|
|---|
| 1621 | str << "): " << message;
|
|---|
| 1622 |
|
|---|
| 1623 | string &old = fLastMessage[boost::this_thread::get_id()];
|
|---|
| 1624 |
|
|---|
| 1625 | if (str.str()==old)
|
|---|
| 1626 | return;
|
|---|
| 1627 | old = str.str();
|
|---|
| 1628 |
|
|---|
| 1629 | fMsg.Update(str, severity);
|
|---|
| 1630 | }
|
|---|
| 1631 | /*
|
|---|
| 1632 | void factStat(int64_t *stat, int len)
|
|---|
| 1633 | {
|
|---|
| 1634 | if (len!=7)
|
|---|
| 1635 | {
|
|---|
| 1636 | fMsg.Warn("factStat received unknown number of values.");
|
|---|
| 1637 | return;
|
|---|
| 1638 | }
|
|---|
| 1639 |
|
|---|
| 1640 | vector<int64_t> data(1, g_maxMem);
|
|---|
| 1641 | data.insert(data.end(), stat, stat+len);
|
|---|
| 1642 |
|
|---|
| 1643 | static vector<int64_t> last(8);
|
|---|
| 1644 | if (data==last)
|
|---|
| 1645 | return;
|
|---|
| 1646 | last = data;
|
|---|
| 1647 |
|
|---|
| 1648 | fDimStatistics.Update(data);
|
|---|
| 1649 |
|
|---|
| 1650 | // len ist die Laenge des arrays.
|
|---|
| 1651 | // array[4] enthaelt wieviele bytes im Buffer aktuell belegt sind; daran
|
|---|
| 1652 | // kannst Du pruefen, ob die 100MB voll sind ....
|
|---|
| 1653 |
|
|---|
| 1654 | ostringstream str;
|
|---|
| 1655 | str
|
|---|
| 1656 | << "Wait=" << stat[0] << " "
|
|---|
| 1657 | << "Skip=" << stat[1] << " "
|
|---|
| 1658 | << "Del=" << stat[2] << " "
|
|---|
| 1659 | << "Tot=" << stat[3] << " "
|
|---|
| 1660 | << "Mem=" << stat[4] << "/" << g_maxMem << " "
|
|---|
| 1661 | << "Read=" << stat[5] << " "
|
|---|
| 1662 | << "Conn=" << stat[6];
|
|---|
| 1663 |
|
|---|
| 1664 | fMsg.Info(str);
|
|---|
| 1665 | }
|
|---|
| 1666 | */
|
|---|
| 1667 |
|
|---|
| 1668 | void factStat(const EVT_STAT &stat)
|
|---|
| 1669 | {
|
|---|
| 1670 | fDimStatistics2.Update(stat);
|
|---|
| 1671 | /*
|
|---|
| 1672 | //some info about what happened since start of program (or last 'reset')
|
|---|
| 1673 | uint32_t reset ; //#if increased, reset all counters
|
|---|
| 1674 | uint32_t numRead[MAX_SOCK] ; //how often succesfull read from N sockets per loop
|
|---|
| 1675 |
|
|---|
| 1676 | uint64_t gotByte[NBOARDS] ; //#Bytes read per Board
|
|---|
| 1677 | uint32_t gotErr[NBOARDS] ; //#Communication Errors per Board
|
|---|
| 1678 | uint32_t evtGet; //#new Start of Events read
|
|---|
| 1679 | uint32_t evtTot; //#complete Events read
|
|---|
| 1680 | uint32_t evtErr; //#Events with Errors
|
|---|
| 1681 | uint32_t evtSkp; //#Events incomplete (timeout)
|
|---|
| 1682 |
|
|---|
| 1683 | uint32_t procTot; //#Events processed
|
|---|
| 1684 | uint32_t procErr; //#Events showed problem in processing
|
|---|
| 1685 | uint32_t procTrg; //#Events accepted by SW trigger
|
|---|
| 1686 | uint32_t procSkp; //#Events rejected by SW trigger
|
|---|
| 1687 |
|
|---|
| 1688 | uint32_t feedTot; //#Events used for feedBack system
|
|---|
| 1689 | uint32_t feedErr; //#Events rejected by feedBack
|
|---|
| 1690 |
|
|---|
| 1691 | uint32_t wrtTot; //#Events written to disk
|
|---|
| 1692 | uint32_t wrtErr; //#Events with write-error
|
|---|
| 1693 |
|
|---|
| 1694 | uint32_t runOpen; //#Runs opened
|
|---|
| 1695 | uint32_t runClose; //#Runs closed
|
|---|
| 1696 | uint32_t runErr; //#Runs with open/close errors
|
|---|
| 1697 |
|
|---|
| 1698 |
|
|---|
| 1699 | //info about current connection status
|
|---|
| 1700 | uint8_t numConn[NBOARDS] ; //#Sockets succesfully open per board
|
|---|
| 1701 | */
|
|---|
| 1702 | }
|
|---|
| 1703 |
|
|---|
| 1704 | void factStat(const GUI_STAT &stat)
|
|---|
| 1705 | {
|
|---|
| 1706 | fDimStatistics1.Update(stat);
|
|---|
| 1707 | /*
|
|---|
| 1708 | //info about status of the main threads
|
|---|
| 1709 | int32_t readStat ; //read thread
|
|---|
| 1710 | int32_t procStat ; //processing thread(s)
|
|---|
| 1711 | int32_t writStat ; //write thread
|
|---|
| 1712 |
|
|---|
| 1713 | //info about some rates
|
|---|
| 1714 | int32_t deltaT ; //time in milli-seconds for rates
|
|---|
| 1715 | int32_t readEvt ; //#events read
|
|---|
| 1716 | int32_t procEvt ; //#events processed
|
|---|
| 1717 | int32_t writEvt ; //#events written
|
|---|
| 1718 | int32_t skipEvt ; //#events skipped
|
|---|
| 1719 |
|
|---|
| 1720 | //some info about current state of event buffer (snapspot)
|
|---|
| 1721 | int32_t evtBuf; //#Events currently waiting in Buffer
|
|---|
| 1722 | uint64_t totMem; //#Bytes available in Buffer
|
|---|
| 1723 | uint64_t usdMem; //#Bytes currently used
|
|---|
| 1724 | uint64_t maxMem; //max #Bytes used during past Second
|
|---|
| 1725 | */
|
|---|
| 1726 | }
|
|---|
| 1727 |
|
|---|
| 1728 |
|
|---|
| 1729 | array<FAD::EventHeader, 40> fVecHeader;
|
|---|
| 1730 |
|
|---|
| 1731 | template<typename T, class S>
|
|---|
| 1732 | array<T, 42> Compare(const S *vec, const T *t)
|
|---|
| 1733 | {
|
|---|
| 1734 | const int offset = reinterpret_cast<const char *>(t) - reinterpret_cast<const char *>(vec);
|
|---|
| 1735 |
|
|---|
| 1736 | const T *min = NULL;
|
|---|
| 1737 | const T *val = NULL;
|
|---|
| 1738 | const T *max = NULL;
|
|---|
| 1739 |
|
|---|
| 1740 | array<T, 42> arr;
|
|---|
| 1741 |
|
|---|
| 1742 | bool rc = true;
|
|---|
| 1743 | for (int i=0; i<40; i++)
|
|---|
| 1744 | {
|
|---|
| 1745 | const char *base = reinterpret_cast<const char*>(vec+i);
|
|---|
| 1746 | const T *ref = reinterpret_cast<const T*>(base+offset);
|
|---|
| 1747 |
|
|---|
| 1748 | arr[i] = *ref;
|
|---|
| 1749 |
|
|---|
| 1750 | if (gi_NumConnect[i]!=7)
|
|---|
| 1751 | {
|
|---|
| 1752 | arr[i] = 0;
|
|---|
| 1753 | continue;
|
|---|
| 1754 | }
|
|---|
| 1755 |
|
|---|
| 1756 | if (!val)
|
|---|
| 1757 | {
|
|---|
| 1758 | min = ref;
|
|---|
| 1759 | val = ref;
|
|---|
| 1760 | max = ref;
|
|---|
| 1761 | }
|
|---|
| 1762 |
|
|---|
| 1763 | if (*ref<*min)
|
|---|
| 1764 | min = ref;
|
|---|
| 1765 |
|
|---|
| 1766 | if (*ref>*max)
|
|---|
| 1767 | max = ref;
|
|---|
| 1768 |
|
|---|
| 1769 | if (*val!=*ref)
|
|---|
| 1770 | rc = false;
|
|---|
| 1771 | }
|
|---|
| 1772 |
|
|---|
| 1773 | arr[40] = val ? *min : 1;
|
|---|
| 1774 | arr[41] = val ? *max : 0;
|
|---|
| 1775 |
|
|---|
| 1776 | return arr;
|
|---|
| 1777 | }
|
|---|
| 1778 |
|
|---|
| 1779 | template<typename T>
|
|---|
| 1780 | array<T, 42> CompareBits(const FAD::EventHeader *h, const T *t)
|
|---|
| 1781 | {
|
|---|
| 1782 | const int offset = reinterpret_cast<const char *>(t) - reinterpret_cast<const char *>(h);
|
|---|
| 1783 |
|
|---|
| 1784 | T val = 0;
|
|---|
| 1785 | T rc = 0;
|
|---|
| 1786 |
|
|---|
| 1787 | array<T, 42> vec;
|
|---|
| 1788 |
|
|---|
| 1789 | bool first = true;
|
|---|
| 1790 |
|
|---|
| 1791 | for (int i=0; i<40; i++)
|
|---|
| 1792 | {
|
|---|
| 1793 | const char *base = reinterpret_cast<const char*>(&fVecHeader[i]);
|
|---|
| 1794 | const T *ref = reinterpret_cast<const T*>(base+offset);
|
|---|
| 1795 |
|
|---|
| 1796 | vec[i+2] = *ref;
|
|---|
| 1797 |
|
|---|
| 1798 | if (gi_NumConnect[i]!=7)
|
|---|
| 1799 | {
|
|---|
| 1800 | vec[i+2] = 0;
|
|---|
| 1801 | continue;
|
|---|
| 1802 | }
|
|---|
| 1803 |
|
|---|
| 1804 | if (first)
|
|---|
| 1805 | {
|
|---|
| 1806 | first = false;
|
|---|
| 1807 | val = *ref;
|
|---|
| 1808 | rc = 0;
|
|---|
| 1809 | }
|
|---|
| 1810 |
|
|---|
| 1811 | rc |= val^*ref;
|
|---|
| 1812 | }
|
|---|
| 1813 |
|
|---|
| 1814 | vec[0] = rc;
|
|---|
| 1815 | vec[1] = val;
|
|---|
| 1816 |
|
|---|
| 1817 | return vec;
|
|---|
| 1818 | }
|
|---|
| 1819 |
|
|---|
| 1820 | template<typename T, size_t N>
|
|---|
| 1821 | void Update(DimDescribedService &svc, const array<T, N> &data, int n=N)
|
|---|
| 1822 | {
|
|---|
| 1823 | // svc.setQuality(vec[40]<=vec[41]);
|
|---|
| 1824 | svc.setData(const_cast<T*>(data.data()), sizeof(T)*n);
|
|---|
| 1825 | svc.updateService();
|
|---|
| 1826 | }
|
|---|
| 1827 |
|
|---|
| 1828 | template<typename T>
|
|---|
| 1829 | void Print(const char *name, const pair<bool,array<T, 43>> &data)
|
|---|
| 1830 | {
|
|---|
| 1831 | cout << name << "|" << data.first << "|" << data.second[1] << "|" << data.second[0] << "<x<" << data.second[1] << ":";
|
|---|
| 1832 | for (int i=0; i<40;i++)
|
|---|
| 1833 | cout << " " << data.second[i+3];
|
|---|
| 1834 | cout << endl;
|
|---|
| 1835 | }
|
|---|
| 1836 |
|
|---|
| 1837 | vector<uint> fNumConnected;
|
|---|
| 1838 |
|
|---|
| 1839 | void debugHead(int /*socket*/, const FAD::EventHeader &h)
|
|---|
| 1840 | {
|
|---|
| 1841 | const uint16_t id = h.Id();
|
|---|
| 1842 | if (id>39)
|
|---|
| 1843 | return;
|
|---|
| 1844 |
|
|---|
| 1845 | if (fNumConnected.size()!=40)
|
|---|
| 1846 | fNumConnected.resize(40);
|
|---|
| 1847 |
|
|---|
| 1848 | const vector<uint> con(gi_NumConnect, gi_NumConnect+40);
|
|---|
| 1849 |
|
|---|
| 1850 | const bool changed = con!=fNumConnected || !IsThreadRunning();
|
|---|
| 1851 |
|
|---|
| 1852 | fNumConnected = con;
|
|---|
| 1853 |
|
|---|
| 1854 | const FAD::EventHeader old = fVecHeader[id];
|
|---|
| 1855 | fVecHeader[id] = h;
|
|---|
| 1856 |
|
|---|
| 1857 | if (old.fVersion != h.fVersion || changed)
|
|---|
| 1858 | {
|
|---|
| 1859 | const array<uint16_t,42> ver = Compare(&fVecHeader[0], &fVecHeader[0].fVersion);
|
|---|
| 1860 |
|
|---|
| 1861 | array<float,42> data;
|
|---|
| 1862 | for (int i=0; i<42; i++)
|
|---|
| 1863 | {
|
|---|
| 1864 | ostringstream str;
|
|---|
| 1865 | str << (ver[i]>>8) << '.' << (ver[i]&0xff);
|
|---|
| 1866 | data[i] = stof(str.str());
|
|---|
| 1867 | }
|
|---|
| 1868 | Update(fDimFwVersion, data);
|
|---|
| 1869 | }
|
|---|
| 1870 |
|
|---|
| 1871 | if (old.fRunNumber != h.fRunNumber || changed)
|
|---|
| 1872 | {
|
|---|
| 1873 | const array<uint32_t,42> run = Compare(&fVecHeader[0], &fVecHeader[0].fRunNumber);
|
|---|
| 1874 | fDimRunNumber.Update(run);
|
|---|
| 1875 | }
|
|---|
| 1876 |
|
|---|
| 1877 | if (old.fTriggerGeneratorPrescaler != h.fTriggerGeneratorPrescaler || changed)
|
|---|
| 1878 | {
|
|---|
| 1879 | const array<uint16_t,42> pre = Compare(&fVecHeader[0], &fVecHeader[0].fTriggerGeneratorPrescaler);
|
|---|
| 1880 | fDimPrescaler.Update(pre);
|
|---|
| 1881 | }
|
|---|
| 1882 |
|
|---|
| 1883 | if (old.fDNA != h.fDNA || changed)
|
|---|
| 1884 | {
|
|---|
| 1885 | const array<uint64_t,42> dna = Compare(&fVecHeader[0], &fVecHeader[0].fDNA);
|
|---|
| 1886 | Update(fDimDNA, dna, 40);
|
|---|
| 1887 | }
|
|---|
| 1888 |
|
|---|
| 1889 | if (old.fStatus != h.fStatus || changed)
|
|---|
| 1890 | {
|
|---|
| 1891 | const array<uint16_t,42> sts = CompareBits(&fVecHeader[0], &fVecHeader[0].fStatus);
|
|---|
| 1892 | Update(fDimStatus, sts);
|
|---|
| 1893 | }
|
|---|
| 1894 |
|
|---|
| 1895 | if (memcmp(old.fDac, h.fDac, sizeof(h.fDac)) || changed)
|
|---|
| 1896 | {
|
|---|
| 1897 | array<uint16_t, FAD::kNumDac*42> dacs;
|
|---|
| 1898 |
|
|---|
| 1899 | for (int i=0; i<FAD::kNumDac; i++)
|
|---|
| 1900 | {
|
|---|
| 1901 | const array<uint16_t, 42> dac = Compare(&fVecHeader[0], &fVecHeader[0].fDac[i]);
|
|---|
| 1902 | memcpy(&dacs[i*42], &dac[0], sizeof(uint16_t)*42);
|
|---|
| 1903 | }
|
|---|
| 1904 |
|
|---|
| 1905 | Update(fDimDac, dacs);
|
|---|
| 1906 | }
|
|---|
| 1907 |
|
|---|
| 1908 | // -----------
|
|---|
| 1909 |
|
|---|
| 1910 | static Time oldt(boost::date_time::neg_infin);
|
|---|
| 1911 | Time newt;
|
|---|
| 1912 |
|
|---|
| 1913 | if (newt>oldt+boost::posix_time::seconds(1))
|
|---|
| 1914 | {
|
|---|
| 1915 | oldt = newt;
|
|---|
| 1916 |
|
|---|
| 1917 | // --- RefClock
|
|---|
| 1918 |
|
|---|
| 1919 | const array<uint32_t,42> clk = Compare(&fVecHeader[0], &fVecHeader[0].fFreqRefClock);
|
|---|
| 1920 | Update(fDimRefClock, clk);
|
|---|
| 1921 |
|
|---|
| 1922 | // --- Temperatures
|
|---|
| 1923 |
|
|---|
| 1924 | const array<int16_t,42> tmp[4] =
|
|---|
| 1925 | {
|
|---|
| 1926 | Compare(&fVecHeader[0], &fVecHeader[0].fTempDrs[0]), // 0-39:val, 40:min, 41:max
|
|---|
| 1927 | Compare(&fVecHeader[0], &fVecHeader[0].fTempDrs[1]), // 0-39:val, 40:min, 41:max
|
|---|
| 1928 | Compare(&fVecHeader[0], &fVecHeader[0].fTempDrs[2]), // 0-39:val, 40:min, 41:max
|
|---|
| 1929 | Compare(&fVecHeader[0], &fVecHeader[0].fTempDrs[3]) // 0-39:val, 40:min, 41:max
|
|---|
| 1930 | };
|
|---|
| 1931 |
|
|---|
| 1932 | vector<int16_t> data;
|
|---|
| 1933 | data.reserve(82);
|
|---|
| 1934 | data.push_back(tmp[0][40]); // min: 0
|
|---|
| 1935 | data.insert(data.end(), tmp[0].data(), tmp[0].data()+40); // val: 1-40
|
|---|
| 1936 | data.push_back(tmp[0][41]); // max: 41
|
|---|
| 1937 | data.insert(data.end(), tmp[0].data(), tmp[0].data()+40); // val: 42-81
|
|---|
| 1938 |
|
|---|
| 1939 | for (int j=1; j<=3; j++)
|
|---|
| 1940 | {
|
|---|
| 1941 | const array<int16_t,42> &ref = tmp[j];
|
|---|
| 1942 |
|
|---|
| 1943 | // Gloabl min
|
|---|
| 1944 | if (ref[40]<data[0]) // 40=min
|
|---|
| 1945 | data[0] = ref[40];
|
|---|
| 1946 |
|
|---|
| 1947 | // Global max
|
|---|
| 1948 | if (ref[41]>data[41]) // 41=max
|
|---|
| 1949 | data[41] = ref[41];
|
|---|
| 1950 |
|
|---|
| 1951 | for (int i=0; i<40; i++)
|
|---|
| 1952 | {
|
|---|
| 1953 | // min per board
|
|---|
| 1954 | if (ref[i]<data[i+1]) // data: 1-40
|
|---|
| 1955 | data[i+1] = ref[i]; // ref: 0-39
|
|---|
| 1956 |
|
|---|
| 1957 | // max per board
|
|---|
| 1958 | if (ref[i]>data[i+42]) // data: 42-81
|
|---|
| 1959 | data[i+42] = ref[i]; // ref: 0-39
|
|---|
| 1960 | }
|
|---|
| 1961 |
|
|---|
| 1962 |
|
|---|
| 1963 | }
|
|---|
| 1964 |
|
|---|
| 1965 | vector<float> deg(82); // 0: global min, 1-40: min
|
|---|
| 1966 | for (int i=0; i<82; i++) // 41: global max, 42-81: max
|
|---|
| 1967 | deg[i] = data[i]/16.;
|
|---|
| 1968 | fDimTemperature.Update(deg);
|
|---|
| 1969 | }
|
|---|
| 1970 |
|
|---|
| 1971 | /*
|
|---|
| 1972 | uint16_t fTriggerType;
|
|---|
| 1973 | uint32_t fTriggerId;
|
|---|
| 1974 | uint32_t fEventCounter;
|
|---|
| 1975 | uint16_t fAdcClockPhaseShift;
|
|---|
| 1976 | uint16_t fNumTriggersToGenerate;
|
|---|
| 1977 | uint16_t fTriggerGeneratorPrescaler;
|
|---|
| 1978 | uint32_t fTimeStamp;
|
|---|
| 1979 | int16_t fTempDrs[kNumTemp]; // In units of 1/16 deg(?)
|
|---|
| 1980 | uint16_t fDac[kNumDac];
|
|---|
| 1981 | */
|
|---|
| 1982 | }
|
|---|
| 1983 | };
|
|---|
| 1984 |
|
|---|
| 1985 | EventBuilderWrapper *EventBuilderWrapper::This = 0;
|
|---|
| 1986 |
|
|---|
| 1987 | // ----------- Event builder callbacks implementation ---------------
|
|---|
| 1988 | extern "C"
|
|---|
| 1989 | {
|
|---|
| 1990 | FileHandle_t runOpen(uint32_t irun, RUN_HEAD *runhd, size_t len)
|
|---|
| 1991 | {
|
|---|
| 1992 | return EventBuilderWrapper::This->runOpen(irun, runhd, len);
|
|---|
| 1993 | }
|
|---|
| 1994 |
|
|---|
| 1995 | int runWrite(FileHandle_t fileId, EVENT *event, size_t len)
|
|---|
| 1996 | {
|
|---|
| 1997 | return EventBuilderWrapper::This->runWrite(fileId, event, len);
|
|---|
| 1998 | }
|
|---|
| 1999 |
|
|---|
| 2000 | int runClose(FileHandle_t fileId, RUN_TAIL *runth, size_t len)
|
|---|
| 2001 | {
|
|---|
| 2002 | return EventBuilderWrapper::This->runClose(fileId, runth, len);
|
|---|
| 2003 | }
|
|---|
| 2004 |
|
|---|
| 2005 | void factOut(int severity, int err, const char *message)
|
|---|
| 2006 | {
|
|---|
| 2007 | EventBuilderWrapper::This->factOut(severity, err, message);
|
|---|
| 2008 | }
|
|---|
| 2009 |
|
|---|
| 2010 | void factStat(GUI_STAT stat)
|
|---|
| 2011 | {
|
|---|
| 2012 | EventBuilderWrapper::This->factStat(stat);
|
|---|
| 2013 | }
|
|---|
| 2014 |
|
|---|
| 2015 | void factStatNew(EVT_STAT stat)
|
|---|
| 2016 | {
|
|---|
| 2017 | EventBuilderWrapper::This->factStat(stat);
|
|---|
| 2018 | }
|
|---|
| 2019 |
|
|---|
| 2020 | void debugHead(int socket, int/*board*/, void *buf)
|
|---|
| 2021 | {
|
|---|
| 2022 | const uint16_t *ptr = reinterpret_cast<uint16_t*>(buf);
|
|---|
| 2023 |
|
|---|
| 2024 | EventBuilderWrapper::This->debugHead(socket, FAD::EventHeader(ptr));
|
|---|
| 2025 | }
|
|---|
| 2026 |
|
|---|
| 2027 | void debugStream(int isock, void *buf, int len)
|
|---|
| 2028 | {
|
|---|
| 2029 | return EventBuilderWrapper::This->debugStream(isock, buf, len);
|
|---|
| 2030 | }
|
|---|
| 2031 |
|
|---|
| 2032 | void debugRead(int isock, int ibyte, int32_t event, int32_t ftmevt, int32_t runno, int state, uint32_t tsec, uint32_t tusec)
|
|---|
| 2033 | {
|
|---|
| 2034 | EventBuilderWrapper::This->debugRead(isock, ibyte, event, ftmevt, runno, state, tsec, tusec);
|
|---|
| 2035 | }
|
|---|
| 2036 |
|
|---|
| 2037 | int eventCheck(PEVNT_HEADER *fadhd, EVENT *event)
|
|---|
| 2038 | {
|
|---|
| 2039 | return EventBuilderWrapper::This->eventCheck(fadhd, event);
|
|---|
| 2040 | }
|
|---|
| 2041 |
|
|---|
| 2042 | void gotNewRun( int runnr, PEVNT_HEADER *headers )
|
|---|
| 2043 | {
|
|---|
| 2044 | return EventBuilderWrapper::This->gotNewRun(runnr, headers);
|
|---|
| 2045 | }
|
|---|
| 2046 | }
|
|---|
| 2047 |
|
|---|
| 2048 | #endif
|
|---|