00001 #include "RawScanResultStreamer.h"
00002 #include "Sct/SctNames.h"
00003 #include "Sct/SctParameters.h"
00004 #include "Sct/LogicErrors.h"
00005 #include "Sct/VersionNotSupportedException.h"
00006 #include "../RawScanResult.h"
00007 #include "SctApi/dataTypes.h"
00008
00009 #include "TH2.h"
00010
00011 #include <iostream>
00012 #include <sstream>
00013
00014 using namespace std;
00015 using namespace Sct;
00016
00017 namespace SctData {
00018 namespace IO {
00019
00020 RawScanResultStreamer::RawScanResultStreamer() throw() {}
00021
00022 bool RawScanResultStreamer::inMap = IOManager::addToMap("SctData::RawScanResult", auto_ptr<Streamer>(new RawScanResultStreamer()));
00023
00024 shared_ptr<Streamable> RawScanResultStreamer::read(IStream& in, const IOManager& manager) const throw(LogicError, IoError) {
00025 shared_ptr<Streamable> ad (&helper.create());
00026 read(in, *ad, manager);
00027 return ad;
00028 }
00029
00030 void RawScanResultStreamer::write(OStream& out, const Streamable& ob, const IOManager& manager) const throw(LogicError, IoError) {
00031
00032 ScanResultStreamer::write(out, ob, manager);
00033
00034
00035 const RawScanResult& raw = dynamic_cast<const RawScanResult&>(ob);
00036 helper.set(raw);
00037
00038
00039 TH2D* link0 = helper.getScanData(0);
00040 TH2D* link1 = helper.getScanData(1);
00041 out << (link0->GetNbinsX()+2)*(link0->GetNbinsY()+2);
00042 out << SR_DT_ROOTHIST << SR_WD_64;
00043
00044 out.put ( link0->GetArray(), (link0->GetNbinsX()+2)*(link0->GetNbinsY()+2) );
00045 out.put ( link1->GetArray(), (link1->GetNbinsX()+2)*(link1->GetNbinsY()+2) );
00046 }
00047
00048 void RawScanResultStreamer::read(IStream& in, Streamable& ob, const IOManager& manager) const throw(LogicError, IoError) {
00049
00050
00051 ScanResultStreamer::read(in, ob, manager);
00052
00053
00054 RawScanResult& raw = dynamic_cast<RawScanResult&>(ob);
00055 helper.set(raw);
00056
00057
00058
00059
00060
00061
00062 double* bins = raw.getPoints().getEdges();
00063
00064 string name = raw.getUniqueID() + "_link0";
00065 auto_ptr<TH2D> link0 (new TH2D(name.c_str(), name.c_str(), nChannelLink, -0.5, nChannelLink-0.5,
00066 raw.getPoints().getNPoints(), bins));
00067
00068 name = raw.getUniqueID() + "_link1";
00069 auto_ptr<TH2D> link1 (new TH2D(name.c_str(), name.c_str(), nChannelLink, -0.5, nChannelLink-0.5,
00070 raw.getPoints().getNPoints(), bins));
00071
00072 delete [] bins;
00073
00074 link0->SetEntries(1);
00075 link1->SetEntries(1);
00076
00077
00078 helper.setScanData(0, link0);
00079 helper.setScanData(1, link1);
00080
00081
00082
00083 readData(in, raw);
00084
00085
00086 }
00087
00088 void RawScanResultStreamer::readData(IStream& in, RawScanResult& raw) const throw(LogicError, IoError) {
00089 unsigned int size;
00090 unsigned short type;
00091 unsigned short width;
00092 in >> size >> type >> width;
00093
00094 switch (type) {
00095 case SR_DT_SLICE:
00096 readSliceData(size, width, in, raw);
00097 return;
00098 case SR_DT_ROOTHIST:
00099 readRootData(size, width, in, raw);
00100 return;
00101 default:
00102 ostringstream oss;
00103 oss << "RawScanResultIS::refreshData Unknown data format: " << type << " size was: " << size;
00104 throw VersionNotSupportedException(oss.str(), __FILE__, __LINE__);
00105 }
00106 }
00107
00108 void RawScanResultStreamer::readRootData(unsigned int isize, unsigned short width, IStream& in, RawScanResult& raw) const throw(LogicError, IoError) {
00109 TH2D* link0 = helper.getScanData(0);
00110 TH2D* link1 = helper.getScanData(1);
00111
00112 double* data;
00113 unsigned int size = 0;
00114 in.get(&data, size);
00115
00116 unsigned expectedSize=(nChannelLink + 2) * ( raw.getPoints().getNPoints() + 2 );
00117 if (size != expectedSize) {
00118 ostringstream oss;
00119 oss << "RawScanResultIS::refreshRootData A Wrong data size. Expected: " << expectedSize;
00120 oss << " Got: " << size;
00121 throw StreamCorruptedException(oss.str(), __FILE__, __LINE__);
00122 }
00123
00124
00125
00126
00127 for (unsigned int i=0; i<size; ++i) {
00128 link0->SetBinContent(i, data[i]);
00129 }
00130 delete [] data;
00131
00132 in.get(&data, size);
00133 if (size != expectedSize) {
00134 ostringstream oss;
00135 oss << "RawScanResultIS::refreshRootData B Wrong data size. Expected: " << expectedSize;
00136 oss << " Got: " << size;
00137 throw StreamCorruptedException(oss.str(), __FILE__, __LINE__);
00138 }
00139 for (unsigned int i=0; i<size; ++i) {
00140 link1->SetBinContent(i, data[i]);
00141 }
00142 delete [] data;
00143
00144 }
00145
00146 void RawScanResultStreamer::readSliceData(unsigned int isize, unsigned short width, IStream& in, RawScanResult& raw) const throw(LogicError, IoError) {
00147 TH2D* link0 = helper.getScanData(0);
00148 TH2D* link1 = helper.getScanData(1);
00149
00150 unsigned int size = 0;
00151 int npoints = raw.getPoints().getNPoints();
00152 unsigned expectedSize=2048u * npoints;
00153
00154
00155 switch (width) {
00156 case SR_WD_16:{
00157 unsigned short* data = 0;
00158 in.get(&data, size);
00159
00160 if (size != expectedSize && size != isize) {
00161 ostringstream oss;
00162 oss << "RawScanResultIS::refreshRootData B Wrong data size. Expected: " << expectedSize;
00163 oss << " Got: " << size;
00164 throw StreamCorruptedException(oss.str(), __FILE__, __LINE__);
00165 }
00166
00167 for (int j=0; j<npoints; ++j) {
00168 for (unsigned int i=0; i<nChannelLink; ++i) {
00169
00170 link0->SetBinContent(link0->GetBin(i+1,j+1), data[j*2048 + i]);
00171 link1->SetBinContent(link1->GetBin(i+1,j+1), data[j*2048 + i+1024]);
00172 }
00173 }
00174 delete[] data;
00175 }
00176 return;
00177
00178 case SR_WD_32: {
00179 UINT32* data = 0;
00180 in.get(&data, size);
00181
00182 if (size != expectedSize && size != isize) {
00183 ostringstream oss;
00184 oss << "RawScanResultIS::refreshRootData B Wrong data size. Expected: " << expectedSize;
00185 oss << " Got: " << size;
00186 throw StreamCorruptedException(oss.str(), __FILE__, __LINE__);
00187 }
00188
00189 for (int j=0; j<npoints; ++j) {
00190 for (unsigned int i=0; i<nChannelLink; ++i) {
00191
00192 link0->SetBinContent(link0->GetBin(i+1,j+1), data[j*2048 + i]);
00193 link1->SetBinContent(link1->GetBin(i+1,j+1), data[j*2048 + i+1024]);
00194 }
00195 }
00196 delete[] data;
00197 }
00198 return;
00199 }
00200
00201
00202 }
00203
00204 }
00205 }