- Timestamp:
- 06/09/11 21:16:15 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/FACT++/src/EventBuilderWrapper.h
r10949 r10964 31 31 DataFileImp(uint32_t id) : fRunId(id) { } 32 32 33 virtual bool OpenFile( uint32_t runid,RUN_HEAD* h) = 0;33 virtual bool OpenFile(RUN_HEAD* h) = 0; 34 34 virtual bool Write(EVENT *) = 0; 35 35 virtual bool Close(RUN_TAIL * = 0) = 0; 36 36 37 37 uint32_t GetRunId() const { return fRunId; } 38 };39 40 41 #include "FAD.h"42 43 class DataFileRaw : public DataFileImp44 {45 public:46 DataFileRaw(uint32_t id) : DataFileImp(id) { }47 ~DataFileRaw() { Close(); }48 49 virtual bool OpenFile(uint32_t, RUN_HEAD* ) { return true; }50 virtual bool Write(EVENT *) { return true; }51 virtual bool Close(RUN_TAIL * = 0) { return true; }52 };53 54 55 class DataFileFits : public DataFileImp56 {57 CCfits::FITS* fFile; /// The pointer to the CCfits FITS file58 CCfits::Table* fTable; /// The pointer to the CCfits binary table59 60 uint64_t fNumRows; ///the number of rows that have been written already to the FITS file.61 uint32_t fRoi; ///the number of rows that have been written already to the FITS file.62 63 public:64 DataFileFits(uint32_t runid) : DataFileImp(runid), fFile(0)65 {66 }67 68 // --------------------------------------------------------------------------69 //70 //! Default destructor71 //! The Fits file SHOULD have been closed already, otherwise the informations72 //! related to the RUN_TAIL will NOT be written to the file.73 //74 ~DataFileFits() { Close(); }75 76 // --------------------------------------------------------------------------77 //78 //! Add a new column to the vectors storing the column data.79 //! @param names the vector of string storing the columns names80 //! @param types the vector of string storing the FITS data format81 //! @param numElems the number of elements in this column82 //! @param type the char describing the FITS data format83 //! @param name the name of the particular column to be added.84 //85 inline void AddColumnEntry(vector<string>& names, vector<string>& types, int numElems, char type, string name)86 {87 names.push_back(name);88 89 ostringstream str;90 if (numElems != 1)91 str << numElems;92 str << type;93 types.push_back(str.str());94 }95 96 // --------------------------------------------------------------------------97 //98 //! Writes a single header key entry99 //! @param name the name of the key100 //! @param value its value101 //! @param comment the comment associated to that key102 //103 //FIXME this function is a duplicate from the class Fits. should we try to merge it ?104 template <typename T>105 void WriteKey(const string &name, const T &value, const string &comment)106 {107 try108 {109 fTable->addKey(name, value, comment);110 }111 catch (CCfits::FitsException e)112 {113 ostringstream str;114 str << "Could not add header key ";115 //TODO pipe the error message somewhere116 }117 }118 119 template <typename T>120 void WriteKey(const string &name, const int idx, const T &value, const string &comment)121 {122 ostringstream str;123 str << name << idx;124 125 WriteKey(str.str(), value, comment);126 }127 38 128 39 // -------------------------------------------------------------------------- … … 133 44 //! @param extension a string containing the extension to be appened to the file name 134 45 // 135 string FormFileName(uint32_t run Number, uint32_t runType, string extension)46 string FormFileName(uint32_t runType, string extension) 136 47 { 137 48 //TODO where am I supposed to get the base directory from ? 138 49 //TODO also, for creating subsequent directories, should I use the functions from the dataLogger ? 139 50 string baseDirectory = "./Run"; 51 140 52 ostringstream result; 141 result << baseDirectory;142 result << Time::fmt("/%Y/%m/%d/") << (Time() - boost::posix_time::time_duration(12,0,0));143 result << setfill('0') << setw(8) << runNumber;144 result << " _001_";53 // result << baseDirectory; 54 // result << Time::fmt("/%Y/%m/%d/") << (Time() - boost::posix_time::time_duration(12,0,0)); 55 result << setfill('0') << setw(8) << fRunId; 56 result << ".001_"; 145 57 switch (runType) 146 58 { 59 case -1: 60 result << 'T'; 61 break; 147 62 case 0: 148 63 result << 'D'; … … 155 70 break; 156 71 case 3: 157 result << ' Y';72 result << 'N'; 158 73 break; 159 74 default: 160 //TODO pipe this error message to the appropriate error stream 161 cout << "Error unexpected event" << endl; 75 result << runType; 162 76 }; 163 result << " _data." << extension;77 result << "." << extension; 164 78 165 79 return result.str(); 80 } 81 82 }; 83 84 85 #include "FAD.h" 86 87 class DataFileRaw : public DataFileImp 88 { 89 ofstream fOut; 90 91 off_t fPosTail; 92 93 uint32_t fCounter; 94 95 96 // WRITE uint32_t 0xFAC77e1e (FACT Tele) 97 // === 98 // WRITE uint32_t TYPE(>0) == 1 99 // WRITE uint32_t ID(>0) == 0 100 // WRITE uint32_t VERSION(>0) == 1 101 // WRITE uint32_t LENGTH 102 // - 103 // WRITE uint32_t TELESCOPE ID 104 // WRITE uint32_t RUNID 105 // === 106 // WRITE uint32_t TYPE(>0) == 2 107 // WRITE uint32_t ID(>0) == 0 108 // WRITE uint32_t VERSION(>0) == 1 109 // WRITE uint32_t LENGTH 110 // - 111 // WRITE HEADER 112 // === 113 // [ 40 TIMES 114 // WRITE uint32_t TYPE(>0) == 3 115 // WRITE uint32_t ID(>0) == 0..39 116 // WRITE uint32_t VERSION(>0) == 1 117 // WRITE uint32_t LENGTH 118 // - 119 // WRITE BOARD-HEADER 120 // ] 121 // === 122 // WRITE uint32_t TYPE(>0) == 4 123 // WRITE uint32_t ID(>0) == 0 124 // WRITE uint32_t VERSION(>0) == 1 125 // WRITE uint32_t LENGTH 126 // - 127 // WRITE FOOTER (empty) 128 // === 129 // [ N times 130 // WRITE uint32_t TYPE(>0) == 10 131 // WRITE uint32_t ID(>0) == counter 132 // WRITE uint32_t VERSION(>0) == 1 133 // WRITE uint32_t LENGTH HEADER 134 // - 135 // WRITE HEADER+DATA 136 // ] 137 // === 138 // WRITE uint32_t TYPE ==0 139 // WRITE uint32_t VERSION==0 140 // WRITE uint32_t LENGTH ==0 141 // === 142 // Go back and write footer 143 144 public: 145 DataFileRaw(uint32_t id) : DataFileImp(id) { } 146 ~DataFileRaw() { Close(); } 147 148 void WriteBlockHeader(uint32_t type, uint32_t ver, uint32_t cnt, uint32_t len) 149 { 150 const uint32_t val[4] = { ver, type, cnt, len }; 151 152 fOut.write(reinterpret_cast<const char*>(val), sizeof(val)); 153 } 154 155 template<typename T> 156 void WriteValue(const T &t) 157 { 158 fOut.write(reinterpret_cast<const char*>(&t), sizeof(T)); 159 } 160 161 enum 162 { 163 kIdentifier = 1, 164 kRunHeader, 165 kBoardHeader, 166 kRunSummary, 167 kEvent, 168 }; 169 170 virtual bool OpenFile(RUN_HEAD *h) 171 { 172 const string name = FormFileName(h->RunType, "bin"); 173 174 errno = 0; 175 fOut.open(name.c_str(), ios_base::out); 176 if (!fOut) 177 { 178 //ostringstream str; 179 //str << "Open file " << name << ": " << strerror(errno) << " (errno=" << errno << ")"; 180 //Error(str); 181 182 return false; 183 } 184 185 fCounter = 0; 186 187 static uint32_t FACT = 0xFAC77e1e; 188 189 fOut.write(reinterpret_cast<char*>(&FACT), 4); 190 191 WriteBlockHeader(kIdentifier, 1, 0, 8); 192 WriteValue(uint32_t(0)); 193 WriteValue(GetRunId()); 194 195 WriteBlockHeader(kRunHeader, 1, 0, sizeof(RUN_HEAD)-sizeof(PEVNT_HEADER*)); 196 fOut.write(reinterpret_cast<char*>(h), sizeof(RUN_HEAD)-sizeof(PEVNT_HEADER*)); 197 198 for (int i=0; i<40; i++) 199 { 200 WriteBlockHeader(kBoardHeader, 1, i, sizeof(PEVNT_HEADER)); 201 fOut.write(reinterpret_cast<char*>(h->FADhead+i), sizeof(PEVNT_HEADER)); 202 } 203 204 const vector<char> block(sizeof(uint32_t)+sizeof(RUN_TAIL)); 205 WriteBlockHeader(kRunSummary, 1, 0, block.size()); 206 207 fPosTail = fOut.tellp(); 208 fOut.write(block.data(), block.size()); 209 210 if (!fOut) 211 { 212 //ostringstream str; 213 //str << "Open file " << name << ": " << strerror(errno) << " (errno=" << errno << ")"; 214 //Error(str); 215 216 return false; 217 } 218 219 return true; 220 } 221 virtual bool Write(EVENT *evt) 222 { 223 const int sh = sizeof(PEVNT_HEADER)+(NPIX-1)*evt->Roi*2; 224 225 WriteBlockHeader(kEvent, 1, fCounter++, sh); 226 fOut.write(reinterpret_cast<char*>(evt)+2, sh-2); 227 return true; 228 } 229 virtual bool Close(RUN_TAIL *tail= 0) 230 { 231 fOut.seekp(fPosTail); 232 233 WriteValue(uint32_t(1)); 234 fOut.write(reinterpret_cast<char*>(tail), sizeof(RUN_TAIL)); 235 236 if (!fOut) 237 { 238 //ostringstream str; 239 //str << "Open file " << name << ": " << strerror(errno) << " (errno=" << errno << ")"; 240 //Error(str); 241 242 return false; 243 } 244 245 fOut.close(); 246 247 if (!fOut) 248 { 249 //ostringstream str; 250 //str << "Open file " << name << ": " << strerror(errno) << " (errno=" << errno << ")"; 251 //Error(str); 252 253 return false; 254 } 255 256 return true; 257 } 258 }; 259 260 261 class DataFileFits : public DataFileImp 262 { 263 CCfits::FITS* fFile; /// The pointer to the CCfits FITS file 264 CCfits::Table* fTable; /// The pointer to the CCfits binary table 265 266 uint64_t fNumRows; ///the number of rows that have been written already to the FITS file. 267 268 public: 269 DataFileFits(uint32_t runid) : DataFileImp(runid), fFile(0) 270 { 271 } 272 273 // -------------------------------------------------------------------------- 274 // 275 //! Default destructor 276 //! The Fits file SHOULD have been closed already, otherwise the informations 277 //! related to the RUN_TAIL will NOT be written to the file. 278 // 279 ~DataFileFits() { Close(); } 280 281 // -------------------------------------------------------------------------- 282 // 283 //! Add a new column to the vectors storing the column data. 284 //! @param names the vector of string storing the columns names 285 //! @param types the vector of string storing the FITS data format 286 //! @param numElems the number of elements in this column 287 //! @param type the char describing the FITS data format 288 //! @param name the name of the particular column to be added. 289 // 290 inline void AddColumnEntry(vector<string>& names, vector<string>& types, int numElems, char type, string name) 291 { 292 names.push_back(name); 293 294 ostringstream str; 295 if (numElems != 1) 296 str << numElems; 297 str << type; 298 types.push_back(str.str()); 299 } 300 301 // -------------------------------------------------------------------------- 302 // 303 //! Writes a single header key entry 304 //! @param name the name of the key 305 //! @param value its value 306 //! @param comment the comment associated to that key 307 // 308 //FIXME this function is a duplicate from the class Fits. should we try to merge it ? 309 template <typename T> 310 void WriteKey(const string &name, const T &value, const string &comment) 311 { 312 try 313 { 314 fTable->addKey(name, value, comment); 315 } 316 catch (CCfits::FitsException e) 317 { 318 ostringstream str; 319 str << "Could not add header key "; 320 //TODO pipe the error message somewhere 321 } 322 } 323 324 template <typename T> 325 void WriteKey(const string &name, const int idx, const T &value, const string &comment) 326 { 327 ostringstream str; 328 str << name << idx; 329 330 WriteKey(str.str(), value, comment); 166 331 } 167 332 … … 172 337 //! @param h a pointer to the RUN_HEAD structure that contains the informations relative to this run 173 338 // 174 bool OpenFile( uint32_t /*runid*/,RUN_HEAD* h)339 bool OpenFile(RUN_HEAD* h) 175 340 { 176 341 //Form filename, based on runid and run-type 177 string fileName = FormFileName(h->FADhead[0].runnumber,h->RunType, "fits");342 const string fileName = FormFileName(h->RunType, "fits"); 178 343 179 344 //create the FITS object … … 214 379 AddColumnEntry(colNames, dataTypes, NPIX*h->Nroi, 'U', "Data"); 215 380 216 fRoi = h->Nroi;217 218 381 //actually create the table 219 382 try … … 228 391 } 229 392 } 230 catch (CCfits::FitsExceptione)393 catch (const CCfits::FitsException &e) 231 394 { 232 395 ostringstream str; … … 356 519 fNumRows++; 357 520 358 const int sh = sizeof(PEVNT_HEADER) -1;521 const int sh = sizeof(PEVNT_HEADER)+(NPIX-1)*e->Roi*2; 359 522 360 523 // column size pointer 361 524 size_t col = 1; 362 if (!WriteColumns(col, sh + NPIX*fRoi*2, e))525 if (!WriteColumns(col, sh, e)) 363 526 return true; 364 527 … … 654 817 try 655 818 { 656 if (!file->OpenFile( runid,h))819 if (!file->OpenFile(h)) 657 820 return 0; 658 821 }
Note:
See TracChangeset
for help on using the changeset viewer.