Skip to content

Commit cdac7e2

Browse files
committed
MCA run time is now correct
Performing an MCA run no longer locks up poll2's RunControl thread. Added a ::Step method to MCA class which allows doing an MCA run from a loop which is external to the MCA class. The MCA time for a run is now correct. MCA now keeps track of the time for a MCA run using time_t instead of usGetDTime. The start time is reset for each run and now starts from zero each time it is run.
1 parent c12d547 commit cdac7e2

6 files changed

Lines changed: 195 additions & 45 deletions

File tree

MCA/include/MCA.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
#ifndef MCA_H
22
#define MCA_H
33

4+
#include <ctime>
5+
46
#include "PixieSupport.h"
57

68
class PixieInterface;
79

810
///Abstract MCA class
911
class MCA {
1012
protected:
13+
/// Timers for the MCA object
14+
time_t start_time;
15+
time_t stop_time;
16+
1117
///Default number of bins in histogram.
1218
static const size_t HIS_SIZE = 16384;
1319
///Default number of channels in ADC.
@@ -19,9 +25,11 @@ class MCA {
1925
PixieInterface *_pif;
2026
public:
2127
///Default constructor.
22-
MCA(PixieInterface *pif) : _pif(pif) {};
28+
MCA(PixieInterface *pif);
2329
///Default destructor.
2430
virtual ~MCA() {};
31+
///Return the length of time the MCA has been running.
32+
double GetRunTime();
2533
///Abstract method describing how the MCA data is stored.
2634
virtual bool StoreData(int mod, int ch) = 0;
2735
///Abstract method to open a storage file.
@@ -32,6 +40,8 @@ class MCA {
3240
virtual bool IsOpen() {return _isOpen;};
3341
///Start the MCA running.
3442
virtual void Run(float duration, bool *stop=NULL);
43+
///Update the MCA histograms.
44+
virtual bool Step();
3545
};
3646

3747
#endif

MCA/source/MCA.cpp

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@
88
#include "Display.h"
99
#include "Utility.h"
1010

11+
///Default constructor
12+
MCA::MCA(PixieInterface *pif) : _pif(pif){
13+
time(&start_time);
14+
}
15+
16+
///Return the length of time the MCA has been running.
17+
double MCA::GetRunTime(){
18+
time(&stop_time);
19+
return difftime(stop_time, start_time);
20+
}
21+
1122
/**The MCA is initialized and run for the specified duration or until a
1223
* stop command is received. At specific intervals the MCA output is
1324
* updated via MCA::StoreData(). Will continue until external bool (stop)
@@ -20,17 +31,17 @@ void MCA::Run(float duration, bool *stop) {
2031
//Start the pixie histogram
2132
_pif->StartHistogramRun();
2233

23-
float runTime = 0;
34+
time(&start_time);
2435

2536
//Loop until we reach the run duration or a stop is received.
2637
while (true) {
2738
if(stop != NULL && *stop){ break; }
28-
else if(duration > 0.0 && (runTime >= duration)){ break; } // Adds support for infinite MCA runs
39+
else if(duration > 0.0 && (difftime(stop_time, start_time) >= duration)){ break; } // Adds support for infinite MCA runs
2940

3041
sleep(2);
42+
3143
//Update run time
32-
runTime += usGetDTime() / 1.0e6;
33-
std::cout << "|" << std::fixed << std::setprecision(2) << runTime << " s |\r" << std::flush;
44+
std::cout << "|" << std::fixed << std::setprecision(2) << GetRunTime() << " s |\r" << std::flush;
3445

3546
//Check if run is still ok
3647
if (!_pif->CheckRunStatus()) {
@@ -44,23 +55,43 @@ void MCA::Run(float duration, bool *stop) {
4455
StoreData(mod, ch);
4556
}
4657
}
58+
4759
//Flush the data to disk.
4860
Flush();
61+
62+
//Update the timer.
63+
time(&stop_time);
4964
}
5065

5166
//End the run
5267
_pif->EndRun();
5368

5469
//Display run completion information.
55-
runTime += usGetDTime() / 1.0e6;
5670
std::cout << std::endl;
5771
Display::LeaderPrint("Run finished");
5872
std::cout << Display::OkayStr() << std::endl;
5973
Display::LeaderPrint("Total running time:");
60-
std::cout << std::fixed << std::setprecision(2) << runTime << " s" << std::endl;
74+
std::cout << std::fixed << std::setprecision(2) << GetRunTime() << " s" << std::endl;
6175

6276
//Uset cout flags
6377
std::cout.unsetf(std::ios_base::floatfield);
6478
std::cout.precision(6);
79+
}
80+
81+
bool MCA::Step(){
82+
if(!_pif || !_pif->CheckRunStatus()){ return false; }
6583

84+
//Store the MCA data via the inherited method StoreData()
85+
for (int mod = 0; mod < _pif->GetNumberCards(); mod++) {
86+
for (unsigned int ch = 0; ch < _pif->GetNumberChannels(); ch++) {
87+
StoreData(mod, ch);
88+
}
89+
}
90+
91+
//Flush the data to disk.
92+
Flush();
93+
94+
time(&stop_time);
95+
96+
return true;
6697
}

MCA/source/MCA_DAMM.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ bool MCA_DAMM::OpenFile(const char* basename) {
2626
_histogram = new HisDrr(drr, his, input);
2727
cout << Display::OkayStr() << endl;
2828

29-
return true;
29+
return (_isOpen = true);
3030
}
3131

3232
bool MCA_DAMM::StoreData(int mod, int ch) {

Poll/include/poll2_core.h

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,49 @@
3333
typedef PixieInterface::word_t word_t;
3434
typedef word_t eventdata_t[maxEventSize];
3535

36-
struct MCA_args{
36+
class MCA;
37+
38+
class MCA_args{
39+
private:
40+
bool running;
3741
bool useRoot;
3842
int totalTime;
3943
std::string basename;
4044

45+
MCA *mca;
46+
47+
public:
4148
MCA_args();
49+
4250
MCA_args(bool useRoot_, int totalTime_, std::string basename_);
4351

52+
~MCA_args();
53+
54+
bool IsRunning(){ return running; }
55+
56+
bool UseRoot(){ return useRoot; }
57+
58+
int GetTotalTime(){ return totalTime; }
59+
60+
std::string GetBasename(){ return basename; }
61+
62+
MCA *GetMCA(){ return mca; }
63+
64+
void SetUseRoot(bool state_=true){ useRoot = state_; }
65+
66+
void SetTotalTime(int totalTime_){ totalTime = totalTime_; }
67+
68+
void SetBasename(std::string basename_){ basename = basename_; }
69+
70+
bool Initialize(PixieInterface *pif_);
71+
72+
bool Step();
73+
74+
bool CheckTime();
75+
4476
void Zero();
77+
78+
void Close(PixieInterface *pif_);
4579
};
4680

4781
struct UDP_Packet {

Poll/source/poll2.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ int main(int argc, char *argv[]){
166166
// Close the output file, if one is open
167167
poll.Close();
168168

169+
// Close the terminal.
170+
poll_term.Close();
171+
169172
//Reprint the leader as the carriage was returned
170173
Display::LeaderPrint(std::string("Running poll2 v").append(POLL2_CORE_VERSION));
171174
std::cout << Display::OkayStr("[Done]") << std::endl;

0 commit comments

Comments
 (0)