SumChannels.cc

00001 #include "SumChannels.hh"
00002 #include "ConvertData.hh"
00003 #include "EventHandler.hh"
00004 #include <algorithm>
00005 #include "RootWriter.hh"
00006 
00007 SumChannels::SumChannels() :
00008   BaseModule(GetDefaultName(),"Create a virtual channel whose waveform is the sum of all other channels in the event")
00009 {
00010   AddDependency<ConvertData>();
00011 }
00012 
00013 SumChannels::~SumChannels()
00014 {
00015   Finalize();
00016 }
00017 
00018 int SumChannels::Initialize()
00019 {
00020   return 0;
00021 }
00022 
00023 int SumChannels::Finalize()
00024 {
00025   return 0;
00026 }
00027 
00028 class add_with_scaling{
00029   double _scale_factor;
00030 public:
00031   add_with_scaling(double scale_factor) : _scale_factor(scale_factor) {}
00032   double operator()(double x, double y) { return x + y*_scale_factor; }
00033 };
00034 
00035 int SumChannels::Process(EventPtr event)
00036 {
00037   ChannelData sumdata;
00038   sumdata.channel_id = ChannelData::CH_SUM;
00039   sumdata.label = "sum";
00040   sumdata.sample_bits = 32;
00041   int n_channels_summed = 0;
00042   EventDataPtr data = event->GetEventData();
00043   if (data->channels.size() < 2)
00044     //No point in summing channels
00045     return 0;
00046   
00047   for(size_t i=0; i<data->channels.size(); i++){
00048     const ChannelData& chdata = data->channels[i];
00049     if( _skip_channels.find( chdata.channel_id) != _skip_channels.end() ||
00050         chdata.channel_id < 0 ){
00051       // we told it to skip this channel
00052       continue;
00053     }
00054     if(n_channels_summed == 0){
00055       sumdata.sample_rate = chdata.sample_rate;
00056       sumdata.trigger_index = chdata.trigger_index;
00057       sumdata.nsamps = chdata.nsamps;
00058       
00059       sumdata.waveform.resize(chdata.nsamps);
00060       
00061     }
00062     
00063     //line up the waveforms of the channels
00064     const int presamps = std::min(sumdata.trigger_index, chdata.trigger_index);
00065     const int postsamps = std::min(sumdata.nsamps - sumdata.trigger_index,
00066                                    chdata.nsamps - chdata.trigger_index);
00067     //load the scale factor from the calibration database
00068     double scale_factor = 1./chdata.spe_mean;;
00069     
00070     //sum the two channels into a temporary vector
00071     std::vector<double> tempsum(presamps+postsamps);
00072     std::vector<double>::const_iterator sumit = sumdata.waveform.begin();
00073     std::vector<double>::const_iterator chit = chdata.waveform.begin();
00074     //sum from trigger_index - presamps to trigger_index+postsamps
00075     std::transform(sumit+sumdata.trigger_index-presamps,
00076                    sumit+sumdata.trigger_index+postsamps, 
00077                    chit+chdata.trigger_index-presamps,
00078                    tempsum.begin(),
00079                    add_with_scaling(scale_factor));
00080     //copy the temporary into the sumdata waveform vector
00081     sumdata.waveform.assign(tempsum.begin(), tempsum.end());
00082        
00083     //reset the historical channel_start and end pointers
00084     sumdata.trigger_index = presamps;
00085     sumdata.nsamps = presamps+postsamps;
00086     sumdata.channel_start = (char*)(sumdata.GetWaveform());
00087     sumdata.channel_end = (char*)( sumdata.GetWaveform() + chdata.nsamps );
00088     n_channels_summed++;
00089     // the sum is "saturated" if any single channel is
00090     if(chdata.saturated) sumdata.saturated = true;
00091   } //end loop over channels
00092   if (n_channels_summed > 0){
00093     //find the max and min of the channel
00094     double* wave = sumdata.GetWaveform();
00095     double* max_samp = std::max_element(wave, wave+sumdata.nsamps);
00096     double* min_samp = std::min_element(wave, wave+sumdata.nsamps);
00097     //data is saturated if it hit 0 or maximum range
00098     sumdata.maximum = *max_samp;
00099     sumdata.minimum = *min_samp;
00100     sumdata.max_time = sumdata.SampleToTime(max_samp - wave);
00101     sumdata.min_time = sumdata.SampleToTime(min_samp - wave);
00102     data->channels.push_back(sumdata);
00103   }
00104   return 0;
00105   
00106 }
00107 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines

Generated on 20 Jun 2014 for daqman by  doxygen 1.6.1