eTrainFinder.cc
00001 #include "EventHandler.hh"
00002 #include "eTrainFinder.hh"
00003 #include "ConvertData.hh"
00004 #include "RootWriter.hh"
00005 #include "TFile.h"
00006 #include "TTree.h"
00007 #include "eTrain.hh"
00008 #include <vector>
00009 #include <numeric>
00010 #include <iostream>
00011 #include <string>
00012 #include <sstream>
00013 #include <cmath>
00014
00015 using namespace std;
00016
00017 eTrainFinder::eTrainFinder():
00018 ChannelModule(GetDefaultName(), "Checks for pulses in the pre-s1 window")
00019 {
00020
00021 RegisterParameter("search_start_time", search_start_time = -20.,
00022 "Time from start of pulse to begin search [us]");
00023 RegisterParameter("search_stop_time", search_stop_time = -0.5,
00024 "Time from start of pulse to begin search [us]");
00025 RegisterParameter("rough_threshold", rough_threshold = 25,
00026 "Value by which waveform must drop over 1 sample");
00027 RegisterParameter("distance", distance = 100,
00028 "Minumum number of samples between spikes");
00029 RegisterParameter("coinc_dist", coinc_dist = 3,
00030 "Maximum number of events (between bad events) for ++coincidence");
00031 RegisterParameter("eMin", eMin = 1,
00032 "Minimum number of spikes for a bad event");
00033 RegisterParameter("eMax", eMax = 100,
00034 "Maximum number of spikes for a bad event");
00035 AddDependency("BaselineFinder");
00036 }
00037
00038 eTrainFinder::~eTrainFinder()
00039 {
00040 }
00041
00042
00043 int eTrainFinder::Initialize()
00044 {
00045 const int nchans=14;
00046 for(int i=0; i<nchans; i++)
00047 {
00048 _nbad.push_back(0);
00049 _coinc_track.push_back(0);
00050 _coinc.push_back(0);
00051 _first.push_back(0);
00052 _last.push_back(0);
00053 }
00054
00055 _n=0;
00056
00057 return 0;
00058 }
00059
00060
00061 int eTrainFinder::Finalize()
00062 {
00063 if(gFile && gFile->IsOpen())
00064 {
00065 unsigned int nchans=14;
00066 int maxbad=0;
00067 int bchan=0;
00068
00069 if( _coinc.size()!=nchans
00070 || _first.size()!=nchans
00071 || _last.size()!=nchans
00072 || _coinc_track.size()!=nchans
00073 || _nbad.size()!=nchans)
00074 Message(ERROR)<<"a vector is not sized aright"<<endl;
00075
00076 for(unsigned int i=0; i<_coinc.size(); i++)
00077 {
00078 if(_coinc.at(i)>maxbad)
00079 {
00080 maxbad=_coinc.at(i);
00081 bchan=i;
00082 }
00083 }
00084
00085 int RunID = EventHandler::GetInstance()->GetRunID();
00086 string id;
00087 stringstream convert;
00088 convert << RunID;
00089 id=convert.str();
00090 string treename0, filename0, filename1;
00091 filename0 = "Run00";
00092 filename1 = ".root";
00093 treename0 = "Found_eTrain_Run_";
00094 string treename, filename;
00095 filename = filename0 + id + filename1;
00096 treename = treename0 + id;
00097
00098 TTree *eTrains = new TTree("eTrains",treename.c_str());
00099 eTrain* caught= new eTrain();
00100 eTrains->Branch("caught.",&caught);
00101
00102 caught->bright_channel = bchan;
00103 caught->first_coinc = _first.at(bchan);
00104 caught->last_coinc = _last.at(bchan);
00105 for(unsigned int i=0; i<_coinc.size(); i++)
00106 caught->coincidences.push_back(_coinc.at(i));
00107
00108 eTrains->Fill();
00109 gFile->Write();
00110 delete caught;
00111 delete eTrains;
00112
00113 _nbad.clear();
00114 _coinc_track.clear();
00115 _coinc.clear();
00116 _first.clear();
00117 _last.clear();
00118
00119 }
00120 return 0;
00121 }
00122
00123
00124 int eTrainFinder::Process(ChannelData* chdata)
00125 {
00126 _n++;
00127 EventDataPtr current_event_data = _current_event->GetEventData();
00128 const int startscan = chdata->TimeToSample(search_start_time);
00129 const int stopscan = chdata->TimeToSample(search_stop_time);
00130 const int nsamps = 1 + stopscan - startscan;
00131 const int thresh = rough_threshold;
00132 const int min_sep = distance;
00133 const int eventID = current_event_data->event_id;
00134 const int channelID = chdata->channel_id;
00135 double* wave = chdata->GetBaselineSubtractedWaveform();
00136 int lastbad = startscan;
00137
00138 if(channelID<0)
00139 return 0;
00140
00141 const int old_nbad = _nbad.at(channelID);
00142
00143 for(int test_sample = startscan+1; test_sample < nsamps; test_sample++)
00144 {
00145 if( ( (wave[test_sample]-wave[test_sample-1]) > thresh )
00146 &&
00147 ( test_sample - lastbad >= min_sep ) )
00148 {
00149 _nbad.at(channelID)++;
00150 lastbad = test_sample;
00151 }
00152 }
00153 if( (_nbad.at(channelID)-old_nbad >= eMin)
00154 && (_nbad.at(channelID)-old_nbad <= eMax) )
00155 {
00156 if( (_coinc_track.at(channelID)>0)
00157 && (_coinc_track.at(channelID) <= coinc_dist) )
00158 {
00159 _coinc.at(channelID)++;
00160 _last.at(channelID)=eventID;
00161 if(_first.at(channelID)==0)
00162 _first.at(channelID)=eventID;
00163 }
00164 _coinc_track.at(channelID)=0;
00165 }
00166 else
00167 _coinc_track.at(channelID)++;
00168
00169 Unspikes found_unspikes;
00170 found_unspikes.nbad = _nbad.at(channelID) - old_nbad;
00171 chdata->unspikes.push_back(found_unspikes);
00172
00173 return 0;
00174 }