7 #include "../newicp/icputils.h"
8 #include "../newicp/event_store.h"
9 #include "../newicp/TCPEventStreamDefs.h"
10 #include "../newicp/isisraw.h"
11 #include "../nexus/NeXusFile.hpp"
17 virtual int main(
const std::vector<std::string>& args);
22 Application::initialize(
self);
28 Poco::Util::Application::uninitialize();
33 void handleHelp(
const std::string& name,
const std::string& value);
36 int liveData(
const std::string& host);
50 Application::defineOptions(options);
52 options.addOption(Poco::Util::Option(
"help",
"h",
"display help information")
58 options.addOption(Poco::Util::Option(
"run-number",
"r",
"specify run number")
62 .binding(
"events_tool.run_number")
65 options.addOption(Poco::Util::Option(
"file",
"f",
"specify raw file")
69 .binding(
"events_tool.raw_file")
72 options.addOption(Poco::Util::Option(
"event-dir",
"d",
"specify directory for event files")
76 .binding(
"events_tool.event_dir")
79 options.addOption(Poco::Util::Option(
"nxfile",
"n",
"specify nexus file")
83 .binding(
"events_tool.nexus_file")
86 options.addOption(Poco::Util::Option(
"live",
"l",
"live data host")
90 .binding(
"events_tool.live_host")
108 Poco::Util::HelpFormatter helpFormatter(options());
109 helpFormatter.setCommand(commandName());
110 helpFormatter.setUsage(
"OPTIONS");
111 helpFormatter.setHeader(
112 "A sample application that demonstrates some of the features "
113 "of the Poco::Util::Application class.");
114 helpFormatter.format(std::cout);
121 stopOptionsProcessing();
137 Poco::Mutex::ScopedLock _lock(
the_mutex);
145 Poco::Mutex::ScopedLock _lock(
the_mutex);
147 for(
int i=0; i<n; ++i)
149 code = 1000 * det_ev[i].
spectrum + event_source_id;
150 if ( first_data_frame.find(code) == first_data_frame.end() )
161 Poco::Mutex::ScopedLock _lock(
the_mutex);
174 Poco::Mutex::ScopedLock _lock(
the_mutex);
182 Poco::Mutex::ScopedLock _lock(
the_mutex);
187 #define PPP_TO_UAMPH 1.738E-6
192 Poco::Logger::root().setLevel(Poco::Message::PRIO_INFORMATION);
193 logger().setLevel(Poco::Message::PRIO_TRACE);
197 return Poco::Util::Application::EXIT_CONFIG;
201 return Poco::Util::Application::EXIT_CONFIG;
204 int run_number = Poco::Util::Application::instance().config().getInt(
"events_tool.run_number", -1);
205 std::string raw_file = Poco::Util::Application::instance().config().getString(
"events_tool.raw_file",
"");
206 std::string nexus_file = Poco::Util::Application::instance().config().getString(
"events_tool.nexus_file",
"");
207 std::string live_host = Poco::Util::Application::instance().config().getString(
"events_tool.live_host",
"");
209 if (live_host.size() > 0)
213 if (raw_file.size() > 0)
217 if (nexus_file.size() > 0)
235 LOGSTR_INFORMATION(
"Found " << raw_protons.size() <<
" raw frames and " << good_protons.size() <<
" good frames");
241 for(std::map<int,int>::const_iterator it = first_data_frame.begin(); it != first_data_frame.end(); ++it)
243 int es = it->first % 1000;
244 int spec = it->first / 1000;
245 LOGSTR_INFORMATION(
"Source id " << es <<
" spectrum " << spec <<
" first data frame " << it->second);
259 std::vector<int> cards(ndet);
260 std::copy(ir->
crat, ir->
crat + ndet, cards.begin());
261 std::sort(cards.begin(), cards.end());
262 std::vector<int>::iterator new_end = std::unique(cards.begin(), cards.end());
263 cards.resize(new_end - cards.begin());
264 time_t run_start = time(NULL);
265 for(i=0; i<cards.size(); ++i)
284 NeXus::File nxfile(nexus_file);
285 std::vector<uint64_t> event_index;
286 nxfile.openPath(
"/raw_data_1/detector_1_events/event_index");
287 nxfile.getData(event_index);
288 int nframes = event_index.size();
289 nframes = std::min(nframes, 2400);
290 int nframes_stuff = std::min(nframes, 100);
291 uint64_t iend = event_index[nframes-1];
292 std::cerr <<
"Reading " << nframes <<
" frames and " << iend <<
" events from " << nexus_file << std::endl;
293 std::vector<uint32_t> event_id(iend);
294 nxfile.openPath(
"/raw_data_1/detector_1_events/event_id");
296 std::vector<int> istart, isize;
298 isize.push_back(iend);
299 nxfile.getSlab(&(event_id[0]), istart, isize);
301 std::vector<int> crat, spec;
302 nxfile.openPath(
"/raw_data_1/isis_vms_compat/CRAT");
303 nxfile.getData(crat);
304 nxfile.openPath(
"/raw_data_1/isis_vms_compat/SPEC");
305 nxfile.getData(spec);
309 std::map<int,int> crat_map;
310 for(
int i=0; i<spec.size(); ++i)
312 crat_map[spec[i]] = crat[i];
316 std::cerr <<
"Starting to process events" << std::endl;
318 uint32_t maxspec = 0;
319 for(uint32_t i=0; i < nframes-1; ++i)
321 for(uint64_t j=event_index[i]; j < event_index[i+1]; ++j)
323 maxspec = std::max(maxspec, event_id[j]);
326 std::cout <<
" Frames " << nframes <<
" max spec number " << maxspec << std::endl;
328 typedef std::vector< std::vector<uint32_t> > stuff_t;
329 stuff_t stuff(nframes_stuff, 0);
330 std::vector<int> first_counts(maxspec+1, -1), last_counts(maxspec+1, -1), counts_per_frame(nframes, 0);
331 for(uint32_t i=0; i<nframes_stuff; ++i)
333 stuff[i].resize(maxspec+1, 0);
335 for(uint32_t i=0; i < nframes-1; ++i)
337 for(uint64_t j=event_index[i]; j < event_index[i+1]; ++j)
339 ++(counts_per_frame[i]);
340 if ( first_counts[event_id[j]] == -1 )
342 first_counts[event_id[j]] = i;
344 last_counts[event_id[j]] = i;
345 if ( i < nframes_stuff )
347 ++(stuff[i][event_id[j]]);
351 for(uint32_t i=0; i < nframes; ++i)
354 std::cout <<
"Total counts in frame " << i <<
" = " << counts_per_frame[i] << std::endl;
356 for(uint32_t j=0; j <= maxspec; ++j)
358 std::cout <<
"Spec " << j <<
" card " << crat_map[j] <<
" first counts frame " << first_counts[j] <<
" last counts frame " << last_counts[j] << std::endl;
382 static char* junk_buffer[10000];
383 Poco::Net::StreamSocket s;
384 unsigned port = 10000;
385 Poco::Net::SocketAddress address(host, port);
388 while( s.available() <
sizeof(setup) )
390 Poco::Thread::sleep(1000);
392 s.receiveBytes(&setup,
sizeof(setup));
395 throw std::runtime_error(
"version wrong");
401 while(s.available() <
sizeof(events.
head))
403 Poco::Thread::sleep(100);
405 s.receiveBytes(&events.
head,
sizeof(events.
head));
408 throw std::runtime_error(
"corrupt stream - you should reconnect");
412 throw std::runtime_error(
"corrupt stream - you should reconnect");
414 s.receiveBytes(junk_buffer, events.
head.
length -
sizeof(events.
head));
415 while(s.available() <
sizeof(events.
head_n))
417 Poco::Thread::sleep(100);
422 throw std::runtime_error(
"corrupt stream - you should reconnect");
441 Poco::Thread::sleep(100);
446 throw std::runtime_error(
"corrupt stream - you should reconnect");
450 std::cerr <<
"Read " << nread <<
" events for frame number " << head_n.
frame_number <<
" time " << head_n.
frame_time_zero << std::endl;
451 for(
int i=0; i<std::min(10,nread); ++i)
453 std::cerr << events.
data[i].time_of_flight <<
" " << events.
data[i].spectrum << std::endl;
std::list< boost::signals2::connection > addGoodFrameCallback(const FrameCallbackSlotType &slot)
int readFromFile(const char *filename)
int printInfo(std::ostream &os)
void setOutputFileName(int run_number, bool new_run, DAEstatus &status)
void stop(bool immediate)
std::list< boost::signals2::connection > addAllFrameCallback(const FrameCallbackSlotType &slot)
std::list< boost::signals2::connection > addGoodEventCallback(const EventCallbackSlotType &slot)
#define LOGSTR_INFORMATION(__arg)
structure describing an individual neutron event following on from a TCPStreamEventHeaderNeutron ...
int loadAppConfig(const std::string &app_name)
load a config file from ../../.. i.e. from c:/labview modules/dae if we are in c:/labview modules/dae...
TCPStreamEventHeaderNeutron head_n
details of ISIS frame data was collected in and the number of neutron events in this packet ...
std::vector< TCPStreamEventNeutron > data
list of neutron events
U sum_map_values(const std::map< T, U > &m)
void setLoggerName(const std::string &logger_name)
TCPStreamEventHeaderSetup head_setup
int _tmain(int argc, _TCHAR *argv[])
void addRawFileInputSource(ISISRAW *iraw, int source_id, time_t run_start)
layout of initial data packet send on initial connection and on a state change e.g. run number changes
void addFileInputSource(int run_number, FileEventSourceInfo &info)
std::list< boost::signals2::connection > addAllEventCallback(const EventCallbackSlotType &slot)
TCPStreamEventHeader head