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