/home/bes3soft/bes3soft/Boss/7.0.2/dist/7.0.2/Event/eformat/eformat-00-00-04/src/util.cxx

Go to the documentation of this file.
00001 //Dear emacs, this is -*- c++ -*-
00002 
00014 #include "eformat/util.h"
00015 #include "eformat/ROBFragment.h"
00016 #include "eformat/ROSFragment.h"
00017 #include "eformat/SubDetectorFragment.h"
00018 #include "eformat/FullEventFragment.h"
00019 #include "ers/StreamFactory.h"
00020 #include "eformat/WrongMarkerIssue.h"
00021 
00022 uint32_t* eformat::next_fragment (std::fstream& fs, uint32_t* addr,
00023                                   size_t size)
00024 {
00025   using namespace eformat;
00026 
00027   off_t offset = fs.tellg();
00028   ERS_DEBUG_3("Current stream position is 0x%lx", offset);
00029 
00030   uint32_t data[2];
00031   if (fs && fs.good() && ! fs.eof()) {
00032     //soft check, so we don't make mistakes
00033     fs.read((char*)data, 8);
00034     if (!fs.good() || fs.eof()) return 0; // end-of-file 
00035     switch((HeaderMarker)data[0]) {
00036     case FULL_EVENT:
00037     case SUB_DETECTOR:
00038     case ROS:
00039     case ROB:
00040     case ROD:
00041       break;
00042     default:
00043       //stop!
00044       std::cout << "Word at offset " << HEX(offset) << " is not one of "
00045                 << HEX(FULL_EVENT) << ", "
00046                 << HEX(SUB_DETECTOR) << ", "
00047                 << HEX(ROS) << ", "
00048                 << HEX(ROB) << "or "
00049                 << HEX(ROS) << ". Stopping execution..." << std::endl;
00050       return 0;
00051     }
00052   }
00053   else return 0; //file is over
00054 
00055   //data[1] is the fragment size, in words. Take it and read the fragment
00056   ERS_DEBUG_3("Resetting stream position to 0x%lx...", offset);
00057   fs.seekg(offset);
00058   if (addr && (size >= (data[1]*4))) {
00059     //space is preallocated and checked
00060     ERS_DEBUG_3("Reading fragment data...");
00061     fs.read((char*)addr, data[1]*4);
00062     ERS_DEBUG_2("Size of fragment readout is %u bytes", 4*data[1]);
00063     ERS_DEBUG_3("Stream position is 0x%lx", offset = fs.tellg());
00064     return addr;
00065   }
00066   else if (addr) {
00067     std::cout << "The fragment at offset " << HEX(offset) << " has "
00068               << data[1]*4 << " bytes and you provided only "
00069               << size << " bytes in your buffer. Stopping execution..."
00070               << std::endl;
00071     return 0;
00072   }
00073 
00074   //if I get here, is because I have to allocate space myself
00075   ERS_DEBUG_3("Allocating fragment data storage of size %ud bytes", 4*data[1]);
00076   uint32_t* retval = new uint32_t[data[1]];
00077   ERS_DEBUG_3("Reading fragment data...");
00078   fs.read((char*)retval, data[1]*4);
00079   ERS_DEBUG_2("Size of fragment readout is %u bytes", 4*data[1]);
00080   ERS_DEBUG_3("Stream position is 0x%lx", offset = fs.tellg());
00081   return retval;
00082 }
00083 
00084 size_t eformat::find_rods (const uint32_t* block_start, size_t block_size,
00085                            const uint32_t** rod, uint32_t* rod_size,
00086                            size_t max_count)
00087 {
00088   const uint32_t* block_end = block_start + block_size;
00089   size_t counter = 0;
00090   while (block_end > block_start) {
00091     uint32_t curr_rod_size = 12; //< base size for a ROD as eformat 2.4
00092     curr_rod_size += *(block_end-3) + *(block_end-2); //status and data sizes
00093     block_end -= curr_rod_size;
00094     if (rod && counter < max_count) {
00095       if (block_end[0] != eformat::ROD) 
00096         throw EFORMAT_WRONG_MARKER(block_end[0], eformat::ROD);
00097       rod_size[counter] = curr_rod_size;
00098       rod[counter] = block_end;
00099     }
00100     ++counter;
00101   }
00102   return counter;
00103 }
00104 
00105 size_t eformat::get_robs (const uint32_t* fragment, const uint32_t** rob,
00106                           size_t max_count)
00107 {
00108   using namespace eformat;
00109   ERS_DEBUG_1("Getting all ROB's from 0x%x...", fragment[0]);
00110 
00111   size_t counter = 0;
00112   switch ((eformat::HeaderMarker)(fragment[0])) {
00113   case ROD:
00114     return 0;
00115   case ROB:
00116     {
00117       if ( max_count > 0 ) {
00118         rob[0] = fragment;
00119         ++counter;
00120       }
00121       else return 0; //not enough space
00122     }
00123     break;
00124   case ROS:
00125     {
00126       eformat::ROSFragment<const uint32_t*> ros(fragment);
00127       counter += ros.children(rob, max_count);
00128       ERS_DEBUG_1("ROS 0x%x contains %d ROB's", ros.source_id(), counter);
00129     }
00130     break;
00131   case SUB_DETECTOR:
00132     {
00133       eformat::SubDetectorFragment<const uint32_t*> sd(fragment);
00134       const uint32_t* ros[256];
00135       size_t ros_counter = sd.children(ros, 256);
00136       for (size_t i=0; i<ros_counter; ++i)
00137         counter += get_robs(ros[i], &rob[counter], max_count - counter);
00138       ERS_DEBUG_1("Subdetector 0x%x contains %d ROB's", sd.source_id(), 
00139                   counter);
00140     }
00141     break;
00142   case FULL_EVENT:
00143     {
00144       eformat::FullEventFragment<const uint32_t*> fe(fragment);
00145       const uint32_t* sd[64];
00146       size_t sd_counter = fe.children(sd, 64);
00147       for (size_t i=0; i<sd_counter; ++i)
00148         counter += get_robs(sd[i], &rob[counter], max_count - counter);
00149       ERS_DEBUG_1("Fullevent 0x%x contains %d ROB's", fe.source_id(), counter);
00150     }
00151     break;
00152   }
00153 
00154   return counter;
00155 }

Generated on Tue Nov 29 22:58:25 2016 for BOSS_7.0.2 by  doxygen 1.4.7