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