Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Data Structures | File List | Namespace Members | Data Fields | Related Pages

RawScanResultStreamer_v1.cpp

00001 #include "RawScanResultStreamer_v1.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_v1::s_version=1;
00025 
00026 RawScanResultStreamer_v1::RawScanResultStreamer_v1() throw() {}
00027 
00028 bool RawScanResultStreamer_v1::inMap = IOManager::addToMap("SctData::RawScanResult",  auto_ptr<Streamer>(new RawScanResultStreamer_v1()));
00029 
00030 shared_ptr<Streamable> RawScanResultStreamer_v1::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_v1::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     
00049     out.put ( link0->GetArray(), (link0->GetNbinsX()+2)*(link0->GetNbinsY()+2) );
00050     out.put ( link1->GetArray(), (link1->GetNbinsX()+2)*(link1->GetNbinsY()+2) );
00051 }
00052 
00053 void RawScanResultStreamer_v1::read(IStream& in, Streamable& ob, const IOManager& manager) const {
00054     manager.readImpl(in, ob, "SctData::ScanResult");
00055 
00056     RawScanResult& raw = dynamic_cast<RawScanResult&>(ob);
00057     helper.set(raw);
00058 
00059     //cout <<"Parent finished" << endl;
00060     //cout << raw.getHeader().getRunNumber() << "  " << raw.getHeader().getScanNumber()
00061     //     << "  " << raw.getHeader().getModuleName() << endl;
00062 
00063     //Now refresh and fill the data
00064     readData(in, raw);
00065     
00066     // cout <<"Done" << endl;
00067 }
00068 
00069 
00070 void RawScanResultStreamer_v1::setHistSize(RawScanResult& raw, unsigned nbinsx, short unsigned ilink) const{
00071   //cout << "RawScanResultStreamer_v1 setHistSize " << nbinsx <<" "<< ilink<<endl;
00072   // check its not already the right size!
00073   if ( helper.getScanData(ilink) && (unsigned) helper.getScanData(ilink)->GetNbinsX()==nbinsx ) return;
00074 
00075   //Create hists
00076   double* bins = raw.getPoints().getEdgesAscending();
00077   
00078   ostringstream name; name << raw.getUniqueID() << "_link" << ilink;
00079   auto_ptr<TH2D> link_hist (new TH2D(name.str().c_str(), name.str().c_str(), nbinsx, -0.5, nbinsx-0.5,
00080                      raw.getPoints().getNPoints(), bins));
00081   
00082   delete [] bins;
00083   
00084   link_hist->SetEntries(1);
00085   helper.setScanData(ilink, link_hist);
00086 }
00087 
00088 void RawScanResultStreamer_v1::readData(IStream& in, RawScanResult& raw) const {
00089     unsigned int size;
00090     unsigned short type;
00091     unsigned short width;
00092     in >> size >> type >> width;
00093 
00094     helper.setDataType(type);
00095 
00096     switch (type) {
00097     case SR_DT_SLICE:
00098         readSliceData(size, width, in, raw);
00099         return;
00100     case SR_DT_ROOTHIST:
00101         readRootData(size, width, in, raw);
00102         return;
00103     case SR_DT_RAWHIST:
00104         readRawData(size, width, in, raw);
00105         return;
00106     default:
00107     ostringstream oss;
00108     oss << "RawScanResultIS::refreshData Unknown data format: " << type << " size was: " << size;
00109         throw VersionNotSupportedException(oss.str(), __FILE__, __LINE__); 
00110     }
00111 }
00112 
00113 void RawScanResultStreamer_v1::readRootData(unsigned int isize, unsigned short width, IStream& in, RawScanResult& raw) const {
00114     
00115     for (unsigned ilink=0; ilink<2; ++ilink){
00116       double* data;
00117       unsigned int size = 0;
00118       in.get(&data, size);    
00119       
00120       unsigned size_y= raw.getPoints().getNPoints() + 2 ;
00121       if (size % size_y !=0 ) {
00122     ostringstream oss;
00123     oss << "RawScanResultIS::readRootData.  Link " << ilink << " size = " << size << ", not divisible by " << size_y;
00124     throw StreamCorruptedException(oss.str(), __FILE__, __LINE__);
00125       }
00126       unsigned size_x=size/size_y;
00127       if (size_x<=1){
00128     ostringstream oss;
00129     oss << "X-size of root data must be 2 or greater! Got "<< size_x;
00130     throw StreamCorruptedException(oss.str(), __FILE__, __LINE__);
00131       }
00132       setHistSize(raw, size_x-2, ilink);
00133       //cout << "link 0 size_y = " << size_x << " nbins_x= " << size_x << endl;
00134       for (unsigned int i=0; i<size; ++i) {
00135         helper.getScanData(ilink)->SetBinContent(i, data[i]);
00136       }
00137       delete [] data;
00138     }
00139 }
00140 
00141 void RawScanResultStreamer_v1::readRawData(unsigned int isize, unsigned short width, IStream& in, RawScanResult& raw) const {
00142   int npoints = raw.getPoints().getNPoints();
00143   unsigned wid_mult = width==SR_WD_16 ? 1 : width==SR_WD_32 ? 2 : width==SR_WD_64 ? 4 : 0;
00144   unsigned quotient = npoints * wid_mult;
00145   if (quotient==0 || isize % quotient != 0) {
00146     ostringstream oss; oss << "Data size  = " << isize << " but trying to divide by " << quotient;
00147     throw StreamCorruptedException(oss.str(), __FILE__, __LINE__);
00148   } 
00149   unsigned x_size = isize / quotient;
00150 
00151   for (unsigned ilink=0; ilink<2; ++ilink){
00152     setHistSize(raw, x_size, ilink);
00153   }
00154   
00155   TH2D* link0 = helper.getScanData(0);
00156   TH2D* link1 = helper.getScanData(1);
00157 
00158   unsigned size=0;
00159   bool ascending=raw.getPoints().ascending();
00160   switch (width) {
00161   case SR_WD_16:{
00162     UINT16* data = 0;
00163     in.get(&data, size);
00164     if (size!=isize){
00165       ostringstream oss; oss << "Size (" << size << ") dosent match header size(" << isize << ")";
00166       throw StreamCorruptedException(oss.str(), __FILE__, __LINE__);
00167     }
00168     for (int j=0; j<npoints; ++j) {
00169       int ipt=ascending ? j : npoints-j-1;
00170       for (unsigned int i=0; i<x_size; ++i) {
00171     //+1 because bin 0 is underflow
00172     link0->SetBinContent(link0->GetBin(i+1,j+1), data[ipt*x_size*2 + i]);
00173     link1->SetBinContent(link1->GetBin(i+1,j+1), data[ipt*x_size*2 + i+x_size]);
00174       }
00175     }
00176     delete[] data; return;
00177   }
00178   case SR_WD_32: {
00179     UINT32* data = 0;
00180     in.get(&data, size);
00181     if (size!=isize){
00182       ostringstream oss; oss << "Size (" << size << ") dosent match header size(" << isize << ")";
00183       throw StreamCorruptedException(oss.str(), __FILE__, __LINE__);
00184     }
00185     for (int j=0; j<npoints; ++j) {
00186       int ipt=ascending ? j : npoints-j-1;
00187       for (unsigned int i=0; i<x_size; ++i) {
00188     //+1 because bin 0 is underflow
00189     link0->SetBinContent(link0->GetBin(i+1,j+1), data[ipt*x_size*2 + i]);
00190     link1->SetBinContent(link1->GetBin(i+1,j+1), data[ipt*x_size*2 + i+x_size]);
00191       }
00192     }
00193     delete[] data; return;
00194   }
00195   default : {
00196     ostringstream oss; oss << "Dont know how to interpret data of width " << width;
00197     throw UnsupportedOperationError(oss.str(), __FILE__, __LINE__);
00198   }
00199   }
00200 }
00201 
00202 void RawScanResultStreamer_v1::readSliceData(unsigned int isize, unsigned short width, IStream& in, RawScanResult& raw) const {
00203     int npoints = raw.getPoints().getNPoints();
00204     unsigned expectedSize=2048u * npoints;
00205 
00206     //cout << width << endl;
00207     if (isize != expectedSize) 
00208     throw StreamCorruptedException("RawScanResultIS::readSliceData. Expected and header sizes not equal.  Expected: " + lexical_cast<string>(expectedSize) + 
00209                        " but header: " + lexical_cast<string>(isize), __FILE__, __LINE__);
00210     
00211     setHistSize(raw, nChannelLink, 0);
00212     setHistSize(raw, nChannelLink, 1);
00213     
00214     TH2D* link0 = helper.getScanData(0);
00215     TH2D* link1 = helper.getScanData(1);
00216     unsigned int size = 0;
00217     bool ascending=raw.getPoints().ascending();
00218     switch (width) {
00219     case SR_WD_16:{
00220     UINT16* data = 0;
00221     in.get(&data, size);
00222     
00223     //cout << "16bit: expected: " << expectedSize << " header: " << isize << " actual: " << size << endl;
00224     if (size != expectedSize)
00225         throw StreamCorruptedException("RawScanResultIS::readSliceData. Expected and actual sizes not equal.  Expected: " + lexical_cast<string>(expectedSize) + 
00226                        " but actual: " + lexical_cast<string>(size), __FILE__, __LINE__);
00227     
00228     for (int j=0; j<npoints; ++j) {
00229       int ipt=ascending ? j : npoints-j-1;
00230       for (unsigned int i=0; i<nChannelLink; ++i) {
00231         //+1 because bin 0 is underflow
00232         link0->SetBinContent(link0->GetBin(i+1,j+1), data[ipt*2048 + i]);
00233         link1->SetBinContent(link1->GetBin(i+1,j+1), data[ipt*2048 + i+1024]);
00234         }
00235     }
00236     delete[] data;  
00237     }
00238     return;
00239     
00240     case SR_WD_32: {
00241     UINT32* data = 0;
00242     in.get(&data, size);
00243     
00244     //cout << "32bit: expected: " << expectedSize << " header: " << isize << " actual: " << size << endl;
00245     if (size != expectedSize)
00246         throw StreamCorruptedException("RawScanResultIS::readSliceData. Expected and actual sizes not equal.  Expected: " + lexical_cast<string>(expectedSize) + 
00247                        " but actual: " + lexical_cast<string>(size), __FILE__, __LINE__);
00248     
00249     for (int j=0; j<npoints; ++j) {
00250       int ipt=ascending ? j : npoints-j-1;
00251       for (unsigned int i=0; i<nChannelLink; ++i) {
00252         //+1 because bin 0 is underflow
00253         link0->SetBinContent(link0->GetBin(i+1,j+1), data[ipt*2048 + i]);
00254         link1->SetBinContent(link1->GetBin(i+1,j+1), data[ipt*2048 + i+1024]);
00255         }
00256     }
00257     delete[] data;  
00258     }
00259     return;
00260     }
00261     
00262 
00263 }
00264 
00265 }
00266 }

Generated on Thu Jul 15 09:50:50 2004 for SCT DAQ/DCS Software - C++ by doxygen 1.3.5