RawScanResultStreamer_v2.cpp

00001 #include "RawScanResultStreamer_v2.h"
00002 #include "Sct/SctNames.h"
00003 #include "Sct/SctParameters.h"
00004 #include "Sct/LogicErrors.h"
00005 #include "Sct/VersionNotSupportedException.h"
00006 #include "Sct/UnsupportedOperationError.h"
00007 #include "../RawScanResult.h"
00008 #include "ScanResultWriter/dataTypes.h"
00009 
00010 #include "TH2.h"
00011 
00012 #include <iostream>
00013 #include <sstream>
00014 #include <boost/lexical_cast.hpp>
00015 
00016 using namespace std;
00017 using namespace Sct;
00018 using namespace boost;
00019 
00020 namespace SctData {
00021 namespace IO {
00022 
00023 /* READ NOTES ON STREAMERS AND VERSIONS BEFORE EDITING THIS FILE! */
00024 unsigned RawScanResultStreamer_v2::s_version=2;
00025 
00026 RawScanResultStreamer_v2::RawScanResultStreamer_v2() throw() {}
00027 
00028 bool RawScanResultStreamer_v2::inMap = IOManager::addToMap("SctData::RawScanResult",  auto_ptr<Streamer>(new RawScanResultStreamer_v2()));
00029 
00030 shared_ptr<Streamable> RawScanResultStreamer_v2::read(IStream& in, const IOManager& manager) const {
00031     shared_ptr<Streamable> ad (&helper.create());
00032     read(in, *ad, manager);
00033     return ad;
00034 }
00035 
00036 void RawScanResultStreamer_v2::write(OStream& out, const Streamable& ob, const IOManager& manager) const {
00037     manager.writeImpl(out, ob, "SctData::ScanResult");
00038 
00039     //Downcast should always work!
00040     const RawScanResult& raw = dynamic_cast<const RawScanResult&>(ob);
00041     helper.set(raw);
00042 
00043     //Now publish data - we publish using the ROOTHIST format.
00044     TH2D* link0 = helper.getScanData(0);
00045     TH2D* link1 = helper.getScanData(1);
00046     out << (link0->GetNbinsX()+2)*(link0->GetNbinsY()+2);   //Note, size is number in 1 link ie no bins * no points
00047     out << SR_DT_ROOTHIST << SR_WD_64;
00048     //cout << "Raw.getDataType() is " << raw.getDataType() << endl;
00049     // ROOTDATA-specific stuff:
00050     out << raw.getDataType();
00051     out.put ( link0->GetArray(), (link0->GetNbinsX()+2)*(link0->GetNbinsY()+2) );
00052     out.put ( link1->GetArray(), (link1->GetNbinsX()+2)*(link1->GetNbinsY()+2) );
00053 }
00054 
00055 void RawScanResultStreamer_v2::read(IStream& in, Streamable& ob, const IOManager& manager) const {
00056     manager.readImpl(in, ob, "SctData::ScanResult");
00057 
00058     RawScanResult& raw = dynamic_cast<RawScanResult&>(ob);
00059     helper.set(raw);
00060 
00061     //cout <<"Parent finished" << endl;
00062     //cout << raw.getHeader().getRunNumber() << "  " << raw.getHeader().getScanNumber()
00063     //     << "  " << raw.getHeader().getModuleName() << endl;
00064 
00065     //Now refresh and fill the data
00066     readData(in, raw);
00067     
00068     //cout <<"Done" << endl;
00069 }
00070 
00071 
00072 void RawScanResultStreamer_v2::setHistSize(RawScanResult& raw, unsigned nbinsx, short unsigned ilink) const{
00074   // check its not already the right size!
00075   if ( helper.getScanData(ilink) && (unsigned) helper.getScanData(ilink)->GetNbinsX()==nbinsx ) return;
00076 
00077   //Create hists
00078   double* bins = raw.getPoints().getEdgesAscending();
00079   
00080   ostringstream name; name << raw.getUniqueID() << "_link" << ilink;
00081   auto_ptr<TH2D> link_hist (new TH2D(name.str().c_str(), name.str().c_str(), nbinsx, -0.5, nbinsx-0.5,
00082                      raw.getPoints().getNPoints(), bins));
00083   
00084   delete [] bins;
00085   
00086   link_hist->SetEntries(1);
00087   helper.setScanData(ilink, link_hist);
00088 }
00089 
00090 void RawScanResultStreamer_v2::readData(IStream& in, RawScanResult& raw) const {
00091     unsigned int size;
00092     unsigned short type;
00093     unsigned short width;
00094     in >> size >> type >> width;
00095     helper.setDataType(type);
00096 
00097     switch (type) {
00098     case SR_DT_SLICE:
00099         readSliceData(size, width, in, raw);
00100         return;
00101     case SR_DT_ROOTHIST:
00102         readRootData(size, width, in, raw);
00103         return;
00104     case SR_DT_RAWHIST:
00105         readRawData(size, width, in, raw);
00106         return;
00107     default:
00108     ostringstream oss;
00109     oss << "RawScanResultIS::refreshData Unknown data format: " << type << " size was: " << size;
00110         throw VersionNotSupportedException(oss.str(), __FILE__, __LINE__); 
00111     }
00112 }
00113 
00114 void RawScanResultStreamer_v2::readRootData(unsigned int isize, unsigned short width, IStream& in, RawScanResult& raw) const {
00115 
00116   unsigned short original_type;
00117   in >> original_type;
00118   helper.setDataType(original_type);
00119 
00120     for (unsigned ilink=0; ilink<2; ++ilink){
00121       double* data;
00122       unsigned int size = 0;
00123       in.get(&data, size);    
00124       
00125       unsigned size_y= raw.getPoints().getNPoints() + 2 ;
00126       if (size % size_y !=0 ) {
00127     ostringstream oss;
00128     oss << "RawScanResultIS::readRootData.  Link " << ilink << " size = " << size << ", not divisible by " << size_y;
00129     throw StreamCorruptedException(oss.str(), __FILE__, __LINE__);
00130       }
00131       unsigned size_x=size/size_y;
00132       if (size_x<=1){
00133     ostringstream oss;
00134     oss << "X-size of root data must be 2 or greater! Got "<< size_x;
00135     throw StreamCorruptedException(oss.str(), __FILE__, __LINE__);
00136       }
00137       setHistSize(raw, size_x-2, ilink);
00139       for (unsigned int i=0; i<size; ++i) {
00140         helper.getScanData(ilink)->SetBinContent(i, data[i]);
00141       }
00142       delete [] data;
00143     }
00144 }
00145 
00146 void RawScanResultStreamer_v2::readRawData(unsigned int isize, unsigned short width, IStream& in, RawScanResult& raw) const {
00147   int npoints = raw.getPoints().getNPoints();
00148   unsigned wid_mult = width==SR_WD_16 ? 1 : width==SR_WD_32 ? 2 : width==SR_WD_64 ? 4 : 0;
00149   unsigned quotient = npoints * wid_mult;
00150   if (quotient==0 || isize % quotient != 0) {
00151     ostringstream oss; oss << "Data size  = " << isize << " but trying to divide by " << quotient;
00152     throw StreamCorruptedException(oss.str(), __FILE__, __LINE__);
00153   } 
00154   unsigned x_size = isize / quotient;
00155 
00156   for (unsigned ilink=0; ilink<2; ++ilink){
00157     setHistSize(raw, x_size, ilink);
00158   }
00159   
00160   TH2D* link0 = helper.getScanData(0);
00161   TH2D* link1 = helper.getScanData(1);
00162 
00163   unsigned size=0;
00164   bool ascending=raw.getPoints().ascending();
00165   switch (width) {
00166   case SR_WD_16:{
00167     UINT16* data = 0;
00168     in.get(&data, size);
00169     if (size!=isize){
00170       ostringstream oss; oss << "Size (" << size << ") dosent match header size(" << isize << ")";
00171       throw StreamCorruptedException(oss.str(), __FILE__, __LINE__);
00172     }
00173     for (int j=0; j<npoints; ++j) {
00174       int ipt=ascending ? j : npoints-j-1;
00175       for (unsigned int i=0; i<x_size; ++i) {
00176     //+1 because bin 0 is underflow
00177     link0->SetBinContent(link0->GetBin(i+1,j+1), data[ipt*x_size*2 + i]);
00178     link1->SetBinContent(link1->GetBin(i+1,j+1), data[ipt*x_size*2 + i+x_size]);
00179       }
00180     }
00181     delete[] data; return;
00182   }
00183   case SR_WD_32: {
00184     UINT32* data = 0;
00185     in.get(&data, size);
00186     if (size!=isize){
00187       ostringstream oss; oss << "Size (" << size << ") dosent match header size(" << isize << ")";
00188       throw StreamCorruptedException(oss.str(), __FILE__, __LINE__);
00189     }
00190     for (int j=0; j<npoints; ++j) {
00191       int ipt=ascending ? j : npoints-j-1;
00192       for (unsigned int i=0; i<x_size; ++i) {
00193     //+1 because bin 0 is underflow
00194     link0->SetBinContent(link0->GetBin(i+1,j+1), data[ipt*x_size*2 + i]);
00195     link1->SetBinContent(link1->GetBin(i+1,j+1), data[ipt*x_size*2 + i+x_size]);
00196       }
00197     }
00198     delete[] data; return;
00199   }
00200   default : {
00201     ostringstream oss; oss << "Dont know how to interpret data of width " << width;
00202     throw UnsupportedOperationError(oss.str(), __FILE__, __LINE__);
00203   }
00204   }
00205 }
00206 
00207 void RawScanResultStreamer_v2::readSliceData(unsigned int isize, unsigned short width, IStream& in, RawScanResult& raw) const {
00208     int npoints = raw.getPoints().getNPoints();
00209     unsigned expectedSize=2048u * npoints;
00210 
00211     //cout << width << endl;
00212     if (isize != expectedSize) 
00213     throw StreamCorruptedException("RawScanResultIS::readSliceData. Expected and header sizes not equal.  Expected: " + lexical_cast<string>(expectedSize) + 
00214                        " but header: " + lexical_cast<string>(isize), __FILE__, __LINE__);
00215     
00216     setHistSize(raw, nChannelLink, 0);
00217     setHistSize(raw, nChannelLink, 1);
00218     
00219     TH2D* link0 = helper.getScanData(0);
00220     TH2D* link1 = helper.getScanData(1);
00221     unsigned int size = 0;
00222     bool ascending=raw.getPoints().ascending();
00223     switch (width) {
00224     case SR_WD_16:{
00225     UINT16* data = 0;
00226     in.get(&data, size);
00227     
00228     //cout << "16bit: expected: " << expectedSize << " header: " << isize << " actual: " << size << endl;
00229     if (size != expectedSize)
00230         throw StreamCorruptedException("RawScanResultIS::readSliceData. Expected and actual sizes not equal.  Expected: " + lexical_cast<string>(expectedSize) + 
00231                        " but actual: " + lexical_cast<string>(size), __FILE__, __LINE__);
00232     
00233     for (int j=0; j<npoints; ++j) {
00234       int ipt=ascending ? j : npoints-j-1;
00235       for (unsigned int i=0; i<nChannelLink; ++i) {
00236         //+1 because bin 0 is underflow
00237         link0->SetBinContent(link0->GetBin(i+1,j+1), data[ipt*2048 + i]);
00238         link1->SetBinContent(link1->GetBin(i+1,j+1), data[ipt*2048 + i+1024]);
00239         }
00240     }
00241     // Also get Occupancy histogram information from chips "6,7", "14,15"
00242     
00243 
00244     delete[] data;  
00245     }
00246       return;
00247 
00248     case SR_WD_32: {
00249     UINT32* data = 0;
00250     in.get(&data, size);
00251     
00252     //cout << "32bit: expected: " << expectedSize << " header: " << isize << " actual: " << size << endl;
00253     if (size != expectedSize)
00254         throw StreamCorruptedException("RawScanResultIS::readSliceData. Expected and actual sizes not equal.  Expected: " + lexical_cast<string>(expectedSize) + 
00255                        " but actual: " + lexical_cast<string>(size), __FILE__, __LINE__);
00256     
00257     for (int j=0; j<npoints; ++j) {
00258       int ipt=ascending ? j : npoints-j-1;
00259       for (unsigned int i=0; i<nChannelLink; ++i) {
00260         //+1 because bin 0 is underflow
00261         link0->SetBinContent(link0->GetBin(i+1,j+1), data[ipt*2048 + i]);
00262         link1->SetBinContent(link1->GetBin(i+1,j+1), data[ipt*2048 + i+1024]);
00263         }
00264     }
00265     delete[] data;  
00266     }
00267     return;
00268     }
00269     
00270 
00271 }
00272 
00273 }
00274 }

Generated on Mon Feb 6 14:01:25 2006 for SCT DAQ/DCS Software - C++ by  doxygen 1.4.6