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