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