QXRD  0.11.16
qxrddetectorpilatus.cpp
Go to the documentation of this file.
1 #include "qxrddetectorpilatus.h"
2 #include "qxrddetectorthread.h"
3 #include "qcepproperty.h"
4 #include "qxrddebug.h"
5 #include "qxrdacquisition.h"
6 #include <QDir>
7 #include <QFile>
8 #include "qcepimagedata.h"
9 #include "qcepallocator.h"
10 #include "qxrddetectorproxy.h"
12 #include "qxrdexperiment.h"
13 #include "qxrddataprocessor.h"
14 
16  QxrdExperimentWPtr expt,
18  int detNum,
19  QcepObject *parent) :
20  QxrdDetector(saver, expt, acq, QxrdDetectorThread::PilatusDetector, detNum, parent),
21  m_PilatusSocket(),
22  m_ExposureTime(-1),
23  m_ExposuresPerFrame(-1),
24  m_ExposureFrameCount(-1),
25  m_Remote(new QxrdDetectorPilatusRemote(this)),
26  m_PilatusHost (saver, this, "pilatusHost", "s11id-pilatus", "Host Address of Computer running Camserver"),
27  m_PilatusPort (saver, this, "pilatusPort", 41234, "Camserver Port Number"),
28  m_PilatusUser (saver, this, "pilatusUser", "det", "Camserver User Name"),
29  m_PilatusSSH (saver, this, "pilatusSSH", "ssh", "ssh command path"),
30  m_PilatusSCP (saver, this, "pilatusSCP", "scp", "scp command path"),
31  m_PilatusDataDirectory (saver, this, "pilatusDataDirectory", "/home/det/shareddata/test/", "Data directory on Camserver computer"),
32  m_ReadFilesLocally (saver, this, "readFilesLocally", true, "Attempt to read acquired files into QXRD for further processing"),
33  m_DeleteFilesAfterReading(saver, this, "deleteFilesAfterReading", false, "Delete files from Camserver computer after reading"),
34  m_ExposureMode (saver, this, "exposureMode", 0, "Pilatus Exposure Mode = (0:No Trigger, 1:ExtTrigger, 2:ExtEnable"),
35  m_EnableFrequency (saver, this, "enableFrequency", 1000, "Frequency of ext enable signal"),
36  m_PilatusExtension (saver, this, "pilatusExtension", "cbf", "File format to be used by camserver")
37 {
39  printf("QxrdDetectorPilatus::QxrdDetectorPilatus(%p)\n", this);
40  }
41 }
42 
44 {
45 #ifndef QT_NO_DEBUG
46  printf("Deleting Pilatus detector\n");
47 #endif
48 
50  printf("QxrdDetectorPilatus::~QxrdDetectorPilatus(%p)\n", this);
51  }
52 }
53 
55 {
56  if (QThread::currentThread() != thread()) {
57  QMetaObject::invokeMethod(this, "startDetector"/*, Qt::BlockingQueuedConnection*/);
58  } else {
60 
61  printMessage(tr("Starting Pilatus Detector at %1:%2").arg(get_PilatusHost()).arg(get_PilatusPort()));
62 
63  m_PilatusSocket.connectToHost(get_PilatusHost(), get_PilatusPort());
64  m_PilatusSocket.waitForConnected();
65 
66  if (qcepDebug(DEBUG_PILATUS)) {
67  printMessage("Connected to pilatus...");
68  }
69 
70  connect(&m_PilatusSocket, &QTcpSocket::readyRead, this, &QxrdDetectorPilatus::readyRead);
71 
72  sendCommand("telemetry");
73  sendCommand("nimages 1");
74  sendCommand("nexpframe 1");
75 
76  imagePath(get_PilatusDataDirectory());
77  }
78 }
79 
81 {
82  if (QThread::currentThread() != thread()) {
83  QMetaObject::invokeMethod(this, "stopDetector");
84  } else {
86 
87  printMessage(tr("Stopping Pilatus Detector at %1").arg(get_PilatusHost()));
88 
89  m_PilatusSocket.close();
90  }
91 }
92 
94 {
95  if (QThread::currentThread() != thread()) {
96  QMetaObject::invokeMethod(this, "beginAcquisition", Qt::BlockingQueuedConnection, Q_ARG(double, exposure));
97  } else {
99 
100  if (qcepDebug(DEBUG_PILATUS)) {
101  printMessage(tr("QxrdDetectorPilatus::beginAcquisition(%1)").arg(exposure));
102  }
103 
104  imagePath(get_PilatusDataDirectory());
105 
106  beginExposure(exposure);
107  }
108 }
109 
111 {
112  if (QThread::currentThread() != thread()) {
113  QMetaObject::invokeMethod(this, "endAcquisition", Qt::BlockingQueuedConnection);
114  } else {
116  }
117 }
118 
120 {
121  if (QThread::currentThread() != thread()) {
122  QMetaObject::invokeMethod(this, "shutdownAcquisition", Qt::BlockingQueuedConnection);
123  } else {
125  }
126 }
127 
129 {
130  if (QThread::currentThread() != thread()) {
131  QMetaObject::invokeMethod(this, "executeCommand", Qt::BlockingQueuedConnection, Q_ARG(QString, cmd));
132  } else {
133  sendCommand(cmd);
134  }
135 }
136 
138 {
139  if (qcepDebug(DEBUG_PILATUS)) {
140  printMessage("QxrdDetectorPilatus::readyRead");
141 
142  printMessage(tr("%1 bytes available").arg(m_PilatusSocket.bytesAvailable()));
143  }
144 
145  m_Buffer.append(m_PilatusSocket.readAll());
146 
147  while(1) {
148  int ind1 = m_Buffer.indexOf('\n');
149  int ind2 = m_Buffer.indexOf(char(24));
150 
151  int ind = ind1;
152 
153  if (ind1 >= 0 && ind2 >= 0) {
154  if (ind1 < ind2) {
155  ind = ind1;
156  } else {
157  ind = ind2;
158  }
159  } else if (ind1 >= 0) {
160  ind = ind1;
161  } else {
162  ind = ind2;
163  }
164 
165  if (ind >= 0) {
166  QString line = m_Buffer.left(ind); // Don't include newline
167  m_Buffer.remove(0, ind+1);
168 
169  if (qcepDebug(DEBUG_PILATUS)) {
170  printMessage(tr("(%1) : \"%2\"").arg(line.count()).arg(QString(line)));
171  }
172 
173  interpretReply(line);
174  } else { // No new lines when expected
175  return;
176  }
177  }
178 }
179 
181 {
182  THREAD_CHECK;
183 
184  if (qcepDebug(DEBUG_PILATUS)) {
185  printMessage(tr("QxrdDetectorPilatus::sendCommand(\"%1\")").arg(cmd));
186  }
187 
188  m_PilatusSocket.write(qPrintable(cmd+"\n"));
189  m_PilatusSocket.waitForBytesWritten();
190 }
191 
193 {
194  if (checkDetectorEnabled()) {
195  sendCommand(tr("imgpath \"%1\"").arg(path));
196  }
197 }
198 
200 {
201  int expMode = get_ExposureMode();
202 
203  if (expMode == NoExternalTrigger) {
204  sendCommand(tr("ExpTime %1").arg(exposure));
205  sendCommand("nexpframe 1");
206  } else if (expMode == ExternalTrigger) {
207  sendCommand("Delay 0");
208  sendCommand(tr("ExpPeriod %1").arg(exposure));
209  sendCommand(tr("ExpTime %1").arg(exposure));
210  sendCommand("nexpframe 1");
211  } else if (expMode == ExternalEnable) {
212  int nFrames = (int) (get_EnableFrequency()*exposure);
213  sendCommand(tr("nexpframe %1").arg(nFrames));
214  }
215 }
216 
218 {
220 
221  if (acq) {
222  m_CurrentFile = acq->currentFileBase(get_DetectorNumber(), get_PilatusExtension());
223 
224  if (get_ReadFilesLocally()) { // Check to see if file exists...
225  if (get_DeleteFilesAfterReading()) {
226  // Attempt to delete file before acquisition...
227  }
228  }
229 
230  int expMode = get_ExposureMode();
231 
232  if (expMode == NoExternalTrigger) {
233  sendCommand(tr("Exposure \"%1\"").arg(m_CurrentFile));
234  } else if (expMode == ExternalTrigger) {
235  sendCommand(tr("ExtTrigger \"%1\"").arg(m_CurrentFile));
236  } else if (expMode == ExternalEnable) {
237  sendCommand(tr("ExtEnable \"%1\"").arg(m_CurrentFile));
238  }
239  }
240 }
241 
243 {
244  if (QThread::currentThread() != thread()) {
245  QMetaObject::invokeMethod(this, "beginFrame", Qt::BlockingQueuedConnection);
246  } else {
247  if (qcepDebug(DEBUG_PILATUS)) {
248  printMessage("QxrdDetectorPilatus::beginFrame");
249  }
250 
252 
253  expose();
254  }
255 }
256 
258 {
259  if (qcepDebug(DEBUG_PILATUS)) {
260  printMessage(tr("QxrdDetectorPilatus::interpretReply(\"%1\")").arg(reply));
261  }
262 
263  if (reply.startsWith("1 ERR")) {
264  criticalMessage(tr("Error with pilatus detector: %1").arg(reply));
265  } else if (reply.startsWith("7 OK")) { // Image has been saved...
266  if (get_ReadFilesLocally() == false) {
268  } else {
270 // pushFileExpected(m_CurrentFile);
272 
273  if (get_DeleteFilesAfterReading()) {
275  }
276  }
277  } else if (reply.startsWith("Image format:")) {
278  QRegExp matcher("Image format\\: (\\d+)\\(w\\) x (\\d+)\\(h\\) pixels(.*)");
279 
280  if (matcher.exactMatch(reply)) {
281  int width = matcher.cap(1).toInt();
282  int height = matcher.cap(2).toInt();
283 
284  printMessage(tr("Detector dimensions %1 cols x %2 rows").arg(width).arg(height));
285 
286  set_NRows(height);
287  set_NCols(width);
288  set_HBinning(1);
289  set_VBinning(1);
290  } else {
291  printMessage(tr("Unmatched: %1, %2").arg(reply).arg(matcher.pattern()));
292  printMessage(tr("Matched Length %1").arg(matcher.matchedLength()));
293  }
294  }
295 }
296 
298 {
300 
301  if (proxy) {
302  proxy->pushProperty(QxrdDetectorProxy::StringProperty, "pilatusHost", "Camserver Host", "s11id-pilatus");
303  proxy->pushProperty(QxrdDetectorProxy::FixedIntegerProperty, "pilatusPort", "Camserver Port", 41234);
304  proxy->pushProperty(QxrdDetectorProxy::StringProperty, "pilatusUser", "Camserver User", "det");
305  proxy->pushProperty(QxrdDetectorProxy::FilenameProperty, "pilatusSSH", "ssh command", "ssh");
306  proxy->pushProperty(QxrdDetectorProxy::FilenameProperty, "pilatusSCP", "scp command", "scp");
307  proxy->pushProperty(QxrdDetectorProxy::DirectoryProperty, "pilatusDataDirectory", "Camserver Data Directory", "/home/det/shareddata/test/");
308  proxy->pushProperty(QxrdDetectorProxy::BooleanProperty, "readFilesLocally", "Attempt to read acquired files into QXRD for further processing", true);
309  proxy->pushProperty(QxrdDetectorProxy::BooleanProperty, "deleteFilesAfterReading", "Delete files from camserver computer after use", false);
310  proxy->pushProperty(QxrdDetectorProxy::ExtensionProperty, "pilatusExtension", "File format for camserver", "cbf");
311  proxy->pushProperty(QxrdDetectorProxy::PilatusModeProperty, "exposureMode", "Pilatus Exposure Mode", 0);
312  proxy->pushProperty(QxrdDetectorProxy::DoubleProperty, "enableFrequency", "Ext Enable Frequency", 1000);
313  }
314 }
315 
317 {
319 
320  if (proxy) {
321  proxy->pushProperty(QxrdDetectorProxy::StringProperty, "pilatusHost", "Camserver Host", get_PilatusHost());
322  proxy->pushProperty(QxrdDetectorProxy::FixedIntegerProperty, "pilatusPort", "Camserver Port", get_PilatusPort());
323  proxy->pushProperty(QxrdDetectorProxy::StringProperty, "pilatusUser", "Camserver User", get_PilatusUser());
324  proxy->pushProperty(QxrdDetectorProxy::FilenameProperty, "pilatusSSH", "ssh command", get_PilatusSSH());
325  proxy->pushProperty(QxrdDetectorProxy::FilenameProperty, "pilatusSCP", "scp command", get_PilatusSCP());
326  proxy->pushProperty(QxrdDetectorProxy::DirectoryProperty, "pilatusDataDirectory", "Camserver Data Directory", get_PilatusDataDirectory());
327  proxy->pushProperty(QxrdDetectorProxy::BooleanProperty, "readFilesLocally", "Attempt to read acquired files into QXRD for further processing", get_ReadFilesLocally());
328  proxy->pushProperty(QxrdDetectorProxy::BooleanProperty, "deleteFilesAfterReading", "Delete files from camserver computer after use", get_DeleteFilesAfterReading());
329  proxy->pushProperty(QxrdDetectorProxy::ExtensionProperty, "pilatusExtension", "File format for camserver", get_PilatusExtension());
330  proxy->pushProperty(QxrdDetectorProxy::PilatusModeProperty, "exposureMode", "Pilatus Exposure Mode", get_ExposureMode());
331  proxy->pushProperty(QxrdDetectorProxy::DoubleProperty, "enableFrequency", "Ext Enable Frequency", get_EnableFrequency());
332  }
333 }
334 
336 {
338 
339  if (proxy) {
340  set_PilatusHost (proxy->property("pilatusHost").toString());
341  set_PilatusUser (proxy->property("pilatusUser").toString());
342  set_PilatusSSH (proxy->property("pilatusSSH").toString());
343  set_PilatusSCP (proxy->property("pilatusSCP").toString());
344  set_PilatusDataDirectory (proxy->property("pilatusDataDirectory").toString());
345  set_ReadFilesLocally (proxy->property("readFilesLocally").toBool());
346  set_DeleteFilesAfterReading(proxy->property("deleteFilesAfterReading").toBool());
347  set_PilatusExtension (proxy->property("pilatusExtension").toString());
348  set_ExposureMode (proxy->property("exposureMode").toInt());
349  set_EnableFrequency (proxy->property("enableFrequency").toDouble());
350  }
351 }
352 
354 {
355  if (QThread::currentThread() != thread()) {
356  QMetaObject::invokeMethod(this, "remoteConnect", Q_ARG(QString, sshCmd));
357  } else {
358  if (m_Remote) {
359  m_Remote->connectToRemote(sshCmd);
360  }
361  }
362 }
363 
365 {
366  if (QThread::currentThread() != thread()) {
367  QMetaObject::invokeMethod(this, "remoteCommand", Q_ARG(QString, cmd));
368  } else {
369  if (m_Remote) {
370  m_Remote->executeRemote(cmd);
371  }
372  }
373 }
374 
376 {
377  QString cmd = tr("%1 -o ForwardX11=No %2@%3 rm %4/%5")
378  .arg(get_PilatusSSH())
379  .arg(get_PilatusUser())
380  .arg(get_PilatusHost())
381  .arg(get_PilatusDataDirectory()).arg(file);
382 
383  if (qcepDebug(DEBUG_PILATUS)) {
384  printMessage(tr("Deleting: %1").arg(cmd));
385  }
386 
387  int rc = QProcess::execute(cmd);
388 
389  if (rc) {
390  printMessage(tr("rc = %1").arg(rc));
391  }
392 }
393 
395 {
397 
398  if (expt) {
399  QxrdDataProcessorPtr proc = expt->dataProcessor();
400 
401  if (proc) {
402  QString dest = proc->filePathInRawOutputDirectory(file);
403 
404  QString cmd = tr("%1 -o ForwardX11=No %2@%3:%4/%5 %6")
405  .arg(get_PilatusSCP())
406  .arg(get_PilatusUser())
407  .arg(get_PilatusHost())
408  .arg(get_PilatusDataDirectory()).arg(file)
409  .arg(dest);
410 
411  if (qcepDebug(DEBUG_PILATUS)) {
412  printMessage(cmd);
413  }
414 
415  int rc = QProcess::execute(cmd);
416 
417  if (rc) {
418  printMessage(tr("rc = %1").arg(rc));
419  }
420  }
421  }
422 }
423 
425 {
426  // Transfer contents of a remote file to a local memory buffer:"
427 
429 
430  if (expt) {
431  QxrdDataProcessorPtr proc = expt->dataProcessor();
432 
433  if (proc) {
434  QString dest = proc->filePathInRawOutputDirectory(file);
435 
436  remoteCommand(tr("echo -n transfer: ; ls -la %1").arg(file));
437  remoteCommand(tr("cat %1").arg(file));
438  }
439  }
440 }
441 
443 {
445 
446  if (expt) {
447  QxrdDataProcessorPtr proc = expt->dataProcessor();
448 
449  if (proc) {
450  QString dest = proc->filePathInRawOutputDirectory(f);
451 
453 
454  if (data->readImage(dest)) {
455  printMessage(tr("Read %1 successfully").arg(dest));
456  }
457 
458  enqueueAcquiredFrame(data);
459  }
460  }
461 }
QSharedPointer< QxrdExperiment > QxrdExperimentPtr
QSharedPointer< QxrdDetectorProxy > QxrdDetectorProxyPtr
virtual void pushPropertiesToProxy(QxrdDetectorProxyPtr proxy)
QxrdExperimentWPtr m_Experiment
Definition: qxrddetector.h:93
qint64 qcepDebug(int cond)
Definition: qcepdebug.cpp:26
QxrdDetectorPilatusRemotePtr m_Remote
void criticalMessage(QString msg, QDateTime ts=QDateTime::currentDateTime()) const
void imagePath(QString path)
QSharedPointer< QxrdAcquisition > QxrdAcquisitionPtr
void printMessage(QString msg, QDateTime ts=QDateTime::currentDateTime()) const
QSharedPointer< QxrdDataProcessor > QxrdDataProcessorPtr
void pullPropertiesfromProxy(QxrdDetectorProxyPtr proxy)
virtual void pullPropertiesfromProxy(QxrdDetectorProxyPtr proxy)
QxrdAcquisitionWPtr m_Acquisition
Definition: qxrddetector.h:94
QWeakPointer< QxrdExperiment > QxrdExperimentWPtr
bool checkDetectorEnabled()
void enqueueAcquiredFrame(QcepImageDataBasePtr img)
void remoteTransfer(QString file)
void executeCommand(QString cmd)
void beginAcquisition(double exposure)
void sendCommand(QString cmd)
virtual void beginFrame()
void beginExposure(double exposure)
virtual void startDetector()
void interpretReply(QString reply)
#define THREAD_CHECK
Definition: qcepmacros.h:11
virtual void stopDetector()
void pushPropertiesToProxy(QxrdDetectorProxyPtr proxy)
void remoteCommand(QString cmd)
QWeakPointer< QxrdAcquisition > QxrdAcquisitionWPtr
QSharedPointer< QcepInt32ImageData > QcepInt32ImageDataPtr
static void pushDefaultsToProxy(QxrdDetectorProxyPtr proxy, int detType)
virtual void shutdownAcquisition()
void remoteCopy(QString file)
virtual void endAcquisition()
QSharedPointer< QcepInt16ImageData > QcepInt16ImageDataPtr
static QcepInt32ImageDataPtr newInt32Image(AllocationStrategy strat, int width, int height, QcepObject *parent)
static void pushDefaultsToProxy(QxrdDetectorProxyPtr proxy)
QxrdDetectorPilatus(QcepSettingsSaverWPtr saver, QxrdExperimentWPtr expt, QxrdAcquisitionWPtr acq, int detNum, QcepObject *parent)
virtual void beginAcquisition(double exposure)
QWeakPointer< QcepSettingsSaver > QcepSettingsSaverWPtr
void remoteConnect(QString sshCmd)
void remoteDelete(QString file)