[go: up one dir, main page]

Menu

[5c152e]: / test / tstmonitor.cc  Maximize  Restore  History

Download this file

186 lines (150 with data), 4.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include <iostream>
#include <iomanip>
#include <csignal>
#include <cstdlib>
#include <DAQ++/DAQmanager.h>
#include <DAQ++/logger.h>
#include <sys/resource.h>
#include "random.h"
#include "utils.h"
#include "MyModule.h"
bool DoRun = true;
void quit(int )
{
// signal(SIGINT,SIG_DFL);
std::cout << std::endl << "Run stop forced" << std::endl;
DoRun = false;
}
class MyMonitor : public DAQpp::Monitor
{
private:
unsigned long counter;
DAQpp::RunManager *rm;
public:
MyMonitor(DAQpp::RunManager *r, int size = 50)
: DAQpp::Monitor(size), counter(0), rm(r)
{}
unsigned long counts() const
{
return counter;
}
void on_start();
void on_stop();
void reset_counts() { counter=0; }
bool user_run(DAQpp::Event *evt);
};
void MyMonitor::on_start()
{
std::cout << "\n\n*** Monitor starting" << std::endl;
reset_counts();
}
void MyMonitor::on_stop()
{
std::cout << "\n\n*** Monitor stopped" << std::endl;
}
bool MyMonitor::user_run(DAQpp::Event *evt)
{
DAQpp::ostream os;
double rate, dead_time, period;
DAQpp::RunState status = rm->GetStatus();
rate = status.rate(true);
if (rate==0.0)
rate = 1;
period = 1.0/rate;
dead_time = randflat(2.0*period, 5.0*period);
DAQpp::wait(dead_time);
counter++;
if ( !(counter%100) )
os << DAQpp::loglevel(DAQpp::Log::none)
<< std::setiosflags(std::ios::fixed | std::ios::right)
<< "\rcounter " << std::setw(8) << counter
<< " triggers " << std::setw(8) << status.n_trigger()
<< " monitored " << std::setw(7) << std::setprecision(2)
<< 100.*((double)counter) / ((double)status.n_trigger()) << '%'
<< " rate "
<< std::setw(8) << std::setprecision(2)
<< status.rate(true) / 1000.
<< " kHz"
<< std::flush;
return true;
}
/**
This is an implementation of a run manager.
a new run starts.
*/
class Rmanager : public DAQpp::RunManager
{
public:
Rmanager(DAQpp::DAQid id, DAQpp::RunCommand *rc = 0) ;
~Rmanager();
void Start();
void Stop();
int writeData(int size, const char *data)
{
return size;
}
};
Rmanager::Rmanager(DAQpp::DAQid id, DAQpp::RunCommand *rc)
: DAQpp::RunManager(id, rc)
{}
Rmanager::~Rmanager()
{
std::cout << "...destroying RunManager " << get_id() << std::endl;
}
void Rmanager::Start()
{
std::cout << "Starting the run" << std::endl;
DAQpp::RunManager::Start();
}
void Rmanager::Stop()
{
DAQpp::RunManager::Stop();
std::cout << "\nEnd of run" << std::endl;
}
void run(Rmanager *run_manager, int nevts, bool mnt = false)
{
DAQpp::DAQmanager *daq_manager
= DAQpp::DAQmanager::theDAQmanager();
run_manager->set_max_events(nevts);
run_manager->Activate();
daq_manager->StartRun(DAQpp::DAQmanager::Threaded);
// daq_manager->StartRun(0);
while ( DoRun && daq_manager->isRunning())
{
DAQpp::wait(0.25);
if ( !daq_manager->isRunning() )
break;
if (mnt)
std:: cout << '\r' << run_manager->GetStatus() << std::flush;
}
daq_manager->StopRun();
std::cout << std::endl
<< "--> Run stats. \n"
<< "---------------\n"
<< std::endl;
std::cout << *run_manager << std::endl;
}
int main (int argc, char **argv)
{
int nwords = 131;
if (argv[1])
nwords = atoi(argv[1]);
std::cout << "Each event will have " << nwords << " words" << std::endl;
// catch control-c
signal(SIGINT, quit);
Rmanager *run_manager = new Rmanager("main");
run_manager->addModule( new MyModule("100", nwords) );
//---------------------------------------------------------------------
// Start the DAQ
//---------------------------------------------------------------------
// We first run with a monitor data
std::cout << "\n\n### Start a run monitoring data" << std::endl;
run_manager->set_monitor( new MyMonitor(run_manager, 250) );
int itimes, ntimes = 10;
for (itimes = 0;itimes < ntimes;itimes++)
run(run_manager, 50000);
std::cout << "\n\n### Start a run without monitoring data" << std::endl;
run_manager->set_monitor( 0 );
run(run_manager, 50000, true);
return 0;
}