Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:17:18

0001 
0002 // Copyright 2020, Jefferson Science Associates, LLC.
0003 // Subject to the terms in the LICENSE file found in the top-level directory.
0004 
0005 #include "DecodeDASSource.h"
0006 #include "ADCSample.h"
0007 
0008 #include <iostream>
0009 #include <vector>
0010 
0011 //---------------------------------
0012 // DecodeDASSource    (Constructor)
0013 //---------------------------------
0014 DecodeDASSource::DecodeDASSource(std::string source_name, JApplication* app) : JEventSource(source_name, app) {
0015     SetCallbackStyle(CallbackStyle::ExpertMode);
0016 }
0017 
0018 DecodeDASSource::~DecodeDASSource() {
0019 }
0020 
0021 void DecodeDASSource::Open() {
0022 
0023     // open the file stream
0024     ifs.open(GetResourceName());
0025     if (!ifs) throw JException("Unable to open '%s'", GetResourceName().c_str());
0026 
0027 }
0028 
0029 void DecodeDASSource::Close() {
0030     // Close the file/stream here.
0031     std::cout << "Closing " << GetResourceName() << std::endl;
0032     ifs.close();
0033 }
0034 
0035 JEventSource::Result DecodeDASSource::Emit(JEvent& event) {
0036 
0037     size_t MAX_CHANNELS = 80;
0038     size_t MAX_SAMPLES  = 1024;
0039     // open the file stream and parse the data
0040     // adc sample data are stored as a vector of ADCSample objects
0041     if (ifs.is_open()) {
0042         if (!ifs.eof()) {
0043             // each iteration of this becomes one new event
0044             // assumes file contains no partial events
0045             std::vector<ADCSample*> hits;
0046             for (uint16_t sample = 0; sample < MAX_SAMPLES && !ifs.eof(); ++sample) {
0047                 for (uint16_t channel = 0; channel < MAX_CHANNELS; ++channel) {
0048                     auto hit = new ADCSample;
0049                     hit->sample_id = sample;
0050                     hit->channel_id = channel;
0051                     ifs >> hit->adc_value;
0052                     hits.push_back(hit);
0053                 }
0054             }
0055             // populate the jevent with the adc samples
0056             event.Insert(hits);
0057             event.SetEventNumber(current_event_nr++);
0058             return Result::Success;
0059         }
0060         // close file stream when the end of file is reached
0061         std::cout << "Reached end of file/stream " << GetResourceName() << std::endl;
0062     }
0063     return Result::FailureFinished;
0064 
0065 }
0066 
0067