qavrg 0.0.28
|
00001 #include "qavrgacquisitionsimulated.h" 00002 #include "qavrgacquisitionthread.h" 00003 #include "qavrgfitter.h" 00004 00005 #include <QtConcurrentRun> 00006 #include <math.h> 00007 #include <QTime> 00008 #include <stdio.h> 00009 00010 QavrgAcquisitionSimulated::QavrgAcquisitionSimulated(QavrgAcquisitionThread *acq, QavrgApplication *app, QObject *parent) 00011 : QavrgAcquisitionData(acq, app, parent), 00012 m_AcquisitionThread(acq) 00013 { 00014 } 00015 00016 void QavrgAcquisitionSimulated::simulatedConfigureAverager(AcquisitionMode mode) 00017 { 00018 set_AcquisitionMode(mode); 00019 } 00020 00021 void QavrgAcquisitionSimulated::simulatedStartAcquisition(int rep, int /*nrep*/) 00022 { 00023 QMutexLocker lock(&m_AccumulatorMutex); 00024 00025 // printf("Starting simulated acquisition %d of %d\n", rep, nrep); 00026 00027 if (rep == 0) { 00028 int nchan = get_NChannels(); 00029 int nsamp = get_NSamples(); 00030 00031 m_Accumulator.resize(nchan); 00032 00033 for (int chan=0; chan<nchan; chan++) { 00034 m_Accumulator[chan].resize(nsamp); 00035 m_Accumulator[chan].fill(0); 00036 } 00037 00038 // m_Futures.resize(nchan); 00039 } 00040 } 00041 00042 void QavrgAcquisitionSimulated::simulatedWaitAcquisition(int /*rep*/, int /*nrep*/) 00043 { 00044 // QMutexLocker lock(&m_Mutex); 00045 // 00046 // QFuture<void> result = QtConcurrent::run( 00047 // this, &QavrgAcquisitionSimulated::calculateSimulatedAcquisition, rep, nrep); 00048 // QTime timer; 00049 // 00050 // timer.start(); 00051 00052 double extraTime = get_AcquireIntegrationTime()/* - timer.restart()/1000.0*/; 00053 00054 // printf("Sleep for %g sec\n", extraTime); 00055 00056 while (extraTime > 0.1) { 00057 if (m_AcquireCancel) break; 00058 00059 m_AcquisitionThread->sleepFor(0.1); 00060 extraTime -= 0.1; 00061 } 00062 00063 if (extraTime >= 0) { 00064 m_AcquisitionThread->sleepFor(extraTime); 00065 } 00066 // result.waitForFinished(); 00067 } 00068 00069 void QavrgAcquisitionSimulated::simulatedReadoutAcquisition(int rep, int nrep) 00070 { 00071 calculateSimulatedAcquisition(rep, nrep); 00072 } 00073 00074 double QavrgAcquisitionSimulated::randomPoisson(double val) const 00075 { 00076 return val+sqrt(val)*(((double)qrand()/(double)RAND_MAX)-0.5); 00077 } 00078 00079 void QavrgAcquisitionSimulated::calculateSimulatedAcquisition(int rep, int nrep) 00080 { 00081 // printf("Calculating simulated acquisition rep %d nrep %d\n", rep, nrep); 00082 00083 int nchan = get_NChannels(); 00084 int nsamp = get_NSamples(); 00085 00086 for (int chan=0; chan<nchan; chan++) { 00087 QMutexLocker lock(&m_AccumulatorMutex); 00088 00089 double samplesPerOrbit = get_SamplesPerOrbit(); 00090 double bunchesPerOrbit = 24; 00091 double samplesPerBunch = samplesPerOrbit / bunchesPerOrbit; 00092 double amplitude = 1.0; 00093 double width = 10.0; 00094 double integ = get_AcquireIntegrationTime(); 00095 00096 // printf("NS: %d, SPO: %g, BPO: %g, SPB: %g\n", nsamp, samplesPerOrbit, bunchesPerOrbit, samplesPerBunch); 00097 00098 if (get_AcquisitionMode() != AcquireDark) { 00099 for (double bunch = 0; bunch < nsamp; bunch += samplesPerBunch) { 00100 double amp = randomPoisson(amplitude*integ*10.0)/(10.0*integ); 00101 00102 for (double i = 0; (i < (8*width)) && (((bunch+i) < nsamp)); i++) { 00103 double val = randomPoisson(amp*exp(-(i)/width)*100.0)/(amp*100.0); 00104 m_Accumulator[chan][bunch+i] += val; 00105 } 00106 } 00107 } 00108 00109 for (int i = 0; i<nsamp; i++) { 00110 double val = randomPoisson(0.3*integ*100.0)/(100.0*integ); 00111 m_Accumulator[chan][i] += val; 00112 } 00113 } 00114 00115 if (rep >= (nrep-1)) { 00116 switch (get_AcquisitionMode()) { 00117 00118 case AcquireDone: 00119 printf("Invalid acquisition mode in QavrgAcquisitionSimulated::calculateSimulatedAcquisition\n"); 00120 return; 00121 00122 case AcquireData: 00123 case AcquireScope: 00124 case AcquireOnce: 00125 for (int chan=0; chan<nchan; chan++) { 00126 fitter(chan) -> resize(nsamp); 00127 QMutexLocker lock(&m_AccumulatorMutex); 00128 QMutexLocker lock1(fitter(chan)->mutex()); 00129 00130 double *rawData = fitter(chan)->rawDataPtr(); 00131 double *accum = m_Accumulator[chan].data(); 00132 00133 for (int i = 0; i<nsamp; i++) { 00134 rawData[i] = accum[i]/nrep; 00135 } 00136 } 00137 00138 if (get_AcquisitionMode() == AcquireScope) { 00139 incr_NQueuedResults(); 00140 00141 emit new_RawData(); 00142 } 00143 00144 break; 00145 00146 case AcquireDark: 00147 for (int chan=0; chan<nchan; chan++) { 00148 fitter(chan) -> resize(nsamp); 00149 QMutexLocker lock(&m_AccumulatorMutex); 00150 QMutexLocker lock1(fitter(chan)->mutex()); 00151 00152 double *darkData = fitter(chan)->darkDataPtr(); 00153 double *accum = m_Accumulator[chan].data(); 00154 00155 for (int i = 0; i<nsamp; i++) { 00156 darkData[i] = accum[i]/nrep; 00157 } 00158 00159 fitter(chan) -> set_DarkAvailable(true); 00160 } 00161 00162 incr_NQueuedResults(); 00163 00164 emit new_DarkData(); 00165 00166 break; 00167 00168 case AcquireReference: 00169 for (int chan=0; chan<nchan; chan++) { 00170 fitter(chan) -> resize(nsamp); 00171 QMutexLocker lock(&m_AccumulatorMutex); 00172 QMutexLocker lock1(fitter(chan)->mutex()); 00173 00174 double *refData = fitter(chan)->referenceDataPtr(); 00175 double *accum = m_Accumulator[chan].data(); 00176 00177 for (int i = 0; i<nsamp; i++) { 00178 refData[i] = accum[i]/nrep; 00179 } 00180 00181 fitter(chan) -> set_ReferenceAvailable(true); 00182 } 00183 00184 incr_NQueuedResults(); 00185 00186 emit new_ReferenceData(); 00187 00188 break; 00189 } 00190 00191 if (get_AcquisitionMode() == AcquireData || get_AcquisitionMode() == AcquireOnce) { 00192 QList< QFuture<void> > futures; 00193 00194 QTime timer; 00195 timer.start(); 00196 00197 for (int chan=0; chan<nchan; chan++) { 00198 if (fitter(chan) -> get_ReferenceAvailable()) { 00199 // printf("concurrent fitting, max Threads = %d\n", QThreadPool::globalInstance()->maxThreadCount()); 00200 futures.append(QtConcurrent::run(fitter(chan), &QavrgFitter::performCalculation)); 00201 } 00202 } 00203 00204 foreach(QFuture<void> f, futures) { 00205 f.waitForFinished(); 00206 } 00207 00208 incr_NQueuedResults(); 00209 00210 emit new_FitData(); /* implies new_RawData(), also */ 00211 00212 emit new_FittingResults(); 00213 00214 emit printMessage(tr("Fitting took %1 msec").arg(timer.restart())); 00215 } 00216 00217 // printf("Simulated Readout complete...\n"); 00218 } 00219 }