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