Changeset 11376 for trunk/FACT++
- Timestamp:
- 07/13/11 14:59:53 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/FACT++/src/EventBuilderWrapper.h
r11359 r11376 33 33 34 34 protected: 35 MessageImp &fLog; 35 36 string fFileName; 36 37 37 38 public: 38 DataFileImp(uint32_t id ) : fRunId(id) { }39 DataFileImp(uint32_t id, MessageImp &imp) : fRunId(id), fLog(imp) { } 39 40 virtual ~DataFileImp() { } 40 41 … … 94 95 { 95 96 public: 96 DataFileNone(uint32_t id ) : DataFileImp(id) { }97 DataFileNone(uint32_t id, MessageImp &imp) : DataFileImp(id, imp) { } 97 98 98 99 bool OpenFile(RUN_HEAD* h) … … 123 124 { 124 125 public: 125 DataFileDebug(uint32_t id ) : DataFileNone(id) { }126 DataFileDebug(uint32_t id, MessageImp &imp) : DataFileNone(id, imp) { } 126 127 127 128 bool Write(EVENT *e) … … 197 198 198 199 public: 199 DataFileRaw(uint32_t id ) : DataFileImp(id), fPosTail(0) { }200 ~DataFileRaw() { Close(); }200 DataFileRaw(uint32_t id, MessageImp &imp) : DataFileImp(id, imp), fPosTail(0) { } 201 ~DataFileRaw() { if (fOut.is_open()) Close(); } 201 202 202 203 void WriteBlockHeader(uint32_t type, uint32_t ver, uint32_t cnt, uint32_t len) … … 223 224 }; 224 225 225 virtualbool OpenFile(RUN_HEAD *h)226 bool OpenFile(RUN_HEAD *h) 226 227 { 227 228 const string name = FormFileName(h->RunType, "bin"); 229 if (access(name.c_str(), F_OK)==0) 230 { 231 fLog.Error("File '"+name+"' already exists."); 232 return false; 233 } 228 234 229 235 fFileName = name; … … 233 239 if (!fOut) 234 240 { 235 //ostringstream str;236 //str << "Open file " << name << ": " << strerror(errno) << " (errno=" << errno << ")";237 //Error(str);241 ostringstream str; 242 str << "Open file " << name << ": " << strerror(errno) << " (errno=" << errno << ")"; 243 fLog.Error(str); 238 244 239 245 return false; … … 268 274 if (!fOut) 269 275 { 270 //ostringstream str;271 //str << "Open file " << name << ": " << strerror(errno) << " (errno=" << errno << ")";272 //Error(str);276 ostringstream str; 277 str << "Open file " << name << ": " << strerror(errno) << " (errno=" << errno << ")"; 278 fLog.Error(str); 273 279 274 280 return false; … … 277 283 return true; 278 284 } 279 virtualbool Write(EVENT *evt)285 bool Write(EVENT *evt) 280 286 { 281 287 const int sh = sizeof(EVENT)-2 + NPIX*evt->Roi*2; … … 285 291 return true; 286 292 } 287 virtualbool Close(RUN_TAIL *tail= 0)293 bool Close(RUN_TAIL *tail= 0) 288 294 { 289 295 WriteBlockHeader(kEndOfFile, 0, 0, 0); … … 299 305 if (!fOut) 300 306 { 301 //ostringstream str;302 //str << "Open file " << name << ": " << strerror(errno) << " (errno=" << errno << ")";303 //Error(str);307 ostringstream str; 308 str << " Writing footer: " << strerror(errno) << " (errno=" << errno << ")"; 309 fLog.Error(str); 304 310 305 311 return false; … … 310 316 if (!fOut) 311 317 { 312 //ostringstream str;313 //str << "Open file " << name << ": " << strerror(errno) << " (errno=" << errno << ")";314 //Error(str);318 ostringstream str; 319 str << "Closing file: " << strerror(errno) << " (errno=" << errno << ")"; 320 fLog.Error(str); 315 321 316 322 return false; … … 321 327 }; 322 328 323 #ifdef HA S_FITS329 #ifdef HAVE_FITS 324 330 class DataFileFits : public DataFileImp 325 331 { … … 329 335 uint64_t fNumRows; ///the number of rows that have been written already to the FITS file. 330 336 337 Converter *fConv; 338 331 339 public: 332 DataFileFits(uint32_t runid) : DataFileImp(runid), fFile(0) 340 DataFileFits(uint32_t runid, MessageImp &imp) : 341 DataFileImp(runid, imp), fFile(0), fNumRows(0), fConv(0) 333 342 { 334 343 } … … 340 349 //! related to the RUN_TAIL will NOT be written to the file. 341 350 // 342 ~DataFileFits() { Close(); }351 ~DataFileFits() { Close(); delete fConv; } 343 352 344 353 // -------------------------------------------------------------------------- … … 380 389 { 381 390 ostringstream str; 382 str << "Could not add header key " ;383 //TODO pipe the error message somewhere391 str << "Could not add header key " << name; 392 fLog.Error(str); 384 393 } 385 394 } … … 404 413 //Form filename, based on runid and run-type 405 414 const string fileName = FormFileName(h->RunType, "fits"); 415 if (access(fileName.c_str(), F_OK)==0) 416 { 417 fLog.Error("File '"+fileName+"' already exists."); 418 return false; 419 } 406 420 407 421 fFileName = fileName; 422 423 /* 424 out << 425 "SIMPLE = T / file does conform to FITS standard " 426 "BITPIX = 8 / number of bits per data pixel " 427 "NAXIS = 0 / number of data axes " 428 "EXTEND = T / FITS dataset may contain extensions " 429 "COMMENT FITS (Flexible Image Transport System) format is defined in 'Astronomy" 430 "COMMENT and Astrophysics', volume 376, page 359; bibcode: 2001A&A...376..359H " 431 "END "; 432 for (int i=0; i<29; i++) 433 out << " " 434 */ 408 435 409 436 //create the FITS object … … 415 442 { 416 443 ostringstream str; 417 str << "Could not open FITS file " << fileName << " reason: " << e.message(); 418 //TODO display the message somewhere 419 444 str << "Could not open FITS file " << fileName << ": " << e.message(); 445 fLog.Error(str); 420 446 return false; 421 447 } 422 423 //create columns according to header424 ostringstream arrayTypes;425 426 // uint32_t EventNum ; // EventNumber as from FTM427 // uint16_t TriggerType ; // Trigger Type from FTM428 // uint32_t SoftTrig ; // SoftTrigger Info (TBD)429 // uint32_t PCTime ; // when did event start to arrive at PC430 // uint32_t BoardTime[NBOARDS];//431 // int16_t StartPix[NPIX]; // First Channel per Pixel (Pixels sorted according Software ID) ; -1 if not filled432 // int16_t StartTM[NTMARK]; // First Channel for TimeMark (sorted Hardware ID) ; -1 if not filled433 // uint16_t Adc_Data[]; // final length defined by malloc ....434 448 435 449 vector<string> colNames; 436 450 vector<string> dataTypes; 437 AddColumnEntry(colNames, dataTypes, 1, 'V', "EventNum"); 438 AddColumnEntry(colNames, dataTypes, 1, 'U', "TriggerType"); 439 AddColumnEntry(colNames, dataTypes, 1, 'V', "SoftTrig"); 440 AddColumnEntry(colNames, dataTypes, 1, 'V', "PCTime"); 441 AddColumnEntry(colNames, dataTypes, NBOARDS, 'V', "BoardTime"); 442 AddColumnEntry(colNames, dataTypes, NPIX, 'I', "StartPix"); 443 AddColumnEntry(colNames, dataTypes, NTMARK, 'I', "StartTM"); 444 AddColumnEntry(colNames, dataTypes, NPIX*h->Nroi, 'U', "Data"); 451 AddColumnEntry(colNames, dataTypes, 1, 'J', "EventNum"); 452 AddColumnEntry(colNames, dataTypes, 1, 'I', "TriggerType"); 453 AddColumnEntry(colNames, dataTypes, 1, 'J', "SoftTrig"); 454 AddColumnEntry(colNames, dataTypes, 1, 'J', "PCTime"); 455 AddColumnEntry(colNames, dataTypes, NBOARDS, 'J', "BoardTime"); 456 AddColumnEntry(colNames, dataTypes, NPIX, 'I', "StartPix"); 457 AddColumnEntry(colNames, dataTypes, NTMARK, 'I', "StartTM"); 458 AddColumnEntry(colNames, dataTypes, NPIX*h->Nroi, 'I', "Data"); 459 460 ostringstream fmt; 461 fmt << "I:1;S:1;I:1;I:1"; 462 fmt << ";I:" << NBOARDS; 463 fmt << ";S:" << NPIX; 464 fmt << ";S:" << NTMARK; 465 fmt << ";S:" << NPIX*h->Nroi; 466 467 fConv = new Converter(fmt.str()); 445 468 446 469 //actually create the table … … 448 471 { 449 472 fTable = fFile->addTable("Events", 0, colNames, dataTypes); 450 if (fTable->rows() != 0)451 {452 ostringstream str;453 str << "Error: table created on the fly looks non-empty.";454 //TODO giev the error text to some error handler455 //FIXME I guess that this error checking is useless. remove it for performances.456 }457 473 } 458 474 catch (const CCfits::FitsException &e) 459 475 { 460 476 ostringstream str; 461 str << "Could not create FITS table " << "Events" << " in file " << fileName << " reason: " << e.message(); 462 //TODO give the error text to some error handler 463 464 Close(); 477 str << "Could not create FITS table 'Events' in file " << fileName << " reason: " << e.message(); 478 fLog.Error(str); 479 return false; 480 } 481 482 if (fTable->rows() != 0) 483 { 484 fLog.Error("FITS table created on the fly looks non-empty."); 465 485 return false; 466 486 } … … 468 488 //write header data 469 489 //first the "standard" keys 470 string stringValue;471 490 WriteKey("EXTREL", 1.0f, "Release Number"); 472 491 WriteKey("TELESCOP", "FACT", "Telescope that acquired this data"); … … 474 493 WriteKey("CREATOR", "FACT++ Event Builder", "Program that wrote this file"); 475 494 495 string stringValue; 476 496 stringValue = Time().GetAsStr(); 477 497 stringValue[10]= 'T'; … … 488 508 WriteKey("NTM", h->NTm, "Number of Time marks"); 489 509 WriteKey("NROI", h->Nroi, "Number of slices per pixels"); 490 510 /* 491 511 //now the boards related keywords 492 512 for (int i=0; i<h->NBoard; i++) … … 505 525 WriteKey("STATUS", i, hh.PLLLCK, 506 526 ""); 507 /* 508 WriteKey("TRIGCRC", i, hh.trigger_crc,509 "Trigger CRC");510 511 WriteKey("TRIGTYP", i, hh.trigger_type,512 "Trigger type");513 514 WriteKey("TRIGID", i, hh.trigger_id,515 "Trigger ID");516 517 WriteKey("EVTCNTR", i, hh.fad_evt_counter,518 "FAD Event Counter");519 520 WriteKey("REFCLK", i, hh.REFCLK_frequency,521 "Reference Clock Frequency");522 */ 527 528 // WriteKey("TRIGCRC", i, hh.trigger_crc, 529 // "Trigger CRC"); 530 531 // WriteKey("TRIGTYP", i, hh.trigger_type, 532 // "Trigger type"); 533 534 // WriteKey("TRIGID", i, hh.trigger_id, 535 // "Trigger ID"); 536 537 // WriteKey("EVTCNTR", i, hh.fad_evt_counter, 538 // "FAD Event Counter"); 539 540 // WriteKey("REFCLK", i, hh.REFCLK_frequency, 541 // "Reference Clock Frequency"); 542 523 543 WriteKey("BOARDID", i, hh.board_id, 524 544 "Board ID"); … … 536 556 WriteKey("TIME", i, hh.time, "Time"); 537 557 WriteKey("RUNNB", i, hh.runnumber, "Run number"); 538 /* 539 for (int j=0;j<NTemp;j++) 540 { 541 str.str(""); str2.str(""); 542 str << "DRS_T" << i << j; 543 str2 << "DRS temperature #" << i << " " << j; 544 WriteKey(str.str(), h->FADhead[i].drs_temperature[j], str2.str()); 545 } 546 */ 558 559 // for (int j=0;j<NTemp;j++) 560 // { 561 // str.str(""); str2.str(""); 562 // str << "DRS_T" << i << j; 563 // str2 << "DRS temperature #" << i << " " << j; 564 // WriteKey(str.str(), h->FADhead[i].drs_temperature[j], str2.str()); 565 // } 547 566 for (int j=0;j<NDAC;j++) 548 567 WriteKey("DAC", i*NDAC+j, hh.dac[j], "DAC"); 549 550 //Last but not least, add header keys that will be updated when closing the file 551 WriteFooter(NULL); 552 } 568 } 569 */ 570 571 //Last but not least, add header keys that will be updated when closing the file 572 //WriteFooter(NULL); 553 573 554 574 return true; … … 556 576 557 577 558 int WriteColumns(size_t &start, size_t size, void *e)578 int WriteColumns(size_t &start, size_t size, const void *e) 559 579 { 560 580 int status = 0; 561 fits_write_tblbytes(fFile->fitsPointer(), fNumRows, start, size, reinterpret_cast<unsigned char*>(e), &status); 581 fits_write_tblbytes(fFile->fitsPointer(), fNumRows, start, size, 582 (unsigned char*)e, &status); 562 583 if (status) 563 584 { 564 585 char text[30];//max length of cfitsio error strings (from doc) 565 586 fits_get_errstatus(status, text); 566 //ostringstream str; 567 //str << "Writing FITS row " << i << " in " << groupName << ": " << text << " (file_write_tblbytes, rc=" << status << ")"; 568 //Error(str); 587 588 ostringstream str; 589 str << "Writing FITS row " << fNumRows << ": " << text << " (file_write_tblbytes, rc=" << status << ")"; 590 fLog.Error(str); 569 591 } 570 592 … … 587 609 if (fits_insert_rows(fTable->fitsPointer(), fNumRows, 1, &status)) 588 610 { 589 //ostringstream str; 590 //str << "Inserting row into " << fFileName << " failed (fits_insert_rows, rc=" << status << ")"; 591 //fMess->Error(str); 592 //TODO pipe this error message to the appropriate error stream 611 char text[30];//max length of cfitsio error strings (from doc) 612 fits_get_errstatus(status, text); 613 614 ostringstream str; 615 str << "Inserting row " << fNumRows << " into " << fFileName << ": " << text << " (fits_insert_rows, rc=" << status << ")"; 616 fLog.Error(str); 617 618 return false; 593 619 } 594 620 fNumRows++; 595 621 596 const int sh = sizeof(EVENT)+NPIX*e->Roi*2;622 const vector<char> data = fConv->ToFits(((char*)e)+2, sizeof(EVENT)+NPIX*e->Roi*2-2); 597 623 598 624 // column size pointer 599 625 size_t col = 1; 600 if (!WriteColumns(col, sh, e))626 if (!WriteColumns(col, data.size(), data.data())) 601 627 return true; 602 628 … … 673 699 return false; 674 700 675 WriteFooter(rt);701 //WriteFooter(rt); 676 702 677 703 delete fFile; … … 759 785 760 786 761 int fRunNumber; 762 787 uint32_t fRunNumber; 763 788 764 789 void InitRunNumber() … … 768 793 fRunNumber = 1000; 769 794 770 while (--fRunNumber> =0)795 while (--fRunNumber>0) 771 796 { 772 797 ostringstream name; … … 813 838 ConnectSlot(i, tcp::endpoint()); 814 839 815 //InitRunNumber();840 InitRunNumber(); 816 841 } 817 842 virtual ~EventBuilderWrapper() … … 827 852 delete *it; 828 853 } 854 855 uint32_t IncreaseRunNumber() 856 { 857 return fRunNumber++; 858 } 859 829 860 830 861 bool IsThreadRunning() … … 1076 1107 switch (fFileFormat) 1077 1108 { 1078 case kNone: file = new DataFileNone(runid ); break;1079 case kDebug: file = new DataFileDebug(runid ); break;1080 case kFits: file = new DataFileFits(runid ); break;1081 case kRaw: file = new DataFileRaw(runid ); break;1109 case kNone: file = new DataFileNone(runid, fMsg); break; 1110 case kDebug: file = new DataFileDebug(runid, fMsg); break; 1111 case kFits: file = new DataFileFits(runid, fMsg); break; 1112 case kRaw: file = new DataFileRaw(runid, fMsg); break; 1082 1113 } 1083 1114
Note:
See TracChangeset
for help on using the changeset viewer.