LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
BoDataFrameInput_module.cc
Go to the documentation of this file.
1 // \file BoDataFrameInput.h
2 // \author Ben Jones, MIT, Jan 2013
3 // bjpjones@mit.edu
4 //
5 
6 #ifndef BoDataFrameInput_h
7 #define BoDataFrameInput_h 1
8 
9 // C/C++ standard libraries
10 #include <fstream>
11 
16 
17 
18 // ROOT includes.
19 #include <Rtypes.h>
20 
21 
22 
23 namespace opdet {
24 
26  public:
27 
29  virtual ~BoDataFrameInput();
30 
31  void produce(art::Event&);
32 
33  void beginJob();
34 
35 
36  private:
37 
38  // The parameters we'll read from the .fcl file.
39  std::string fInputFile;
40  std::ifstream fTextFile;
41 
42  };
43 }
44 
45 #endif
46 
47 
48 
54 // Framework includes
56 
57 namespace opdet{
58 
60 
61 }//end namespace opdet
63 
64 // \file BoDataFrameInput.cxx
65 //
66 //
67 
68 // FMWK includes
70 #include "fhiclcpp/ParameterSet.h"
78 
79 // LArSoft includes
83 
84 // ROOT includes
85 #include <TH1D.h>
86 #include <TF1.h>
87 #include <TTree.h>
88 #include <TRandom.h>
89 
90 // C++ language includes
91 #include <iostream>
92 #include <sstream>
93 #include <cstring>
94 #include <vector>
95 
96 
97 // Debug flag; only used during code development.
98 // const bool debug = true;
99 
100 namespace opdet {
101 
102 
104  {
105  // Infrastructure piece
106  produces<std::vector< raw::OpDetPulse> >();
107 
108  // Input filename read from fcl
109  fInputFile = pset.get<std::string>("InputFile");
110  std::string FullFilePath("");
111  cet::search_path sp("FW_SEARCH_PATH");
112  if( !sp.find_file(fInputFile, FullFilePath) )
113  throw cet::exception("BoDataFrameInput") << "Unable to find optical data file in " << sp.to_string() << "\n";
114 
115 
116  // We want to throw away the first command, as it will be "evt"
117  std::string sub;
118  getline(fTextFile, sub, ',');
119  if(sub.substr(0,3)!=std::string("evt"))
120  {
121  mf::LogInfo("BoDataFrameInput")<<"Warning: first command in text file is not an evt block as expected. Trying to persevere anyway : " << sub;
122  }
123 
124 
125 
126 
127  // Open file for reading
128  fTextFile.open(FullFilePath);
129 
130  }
131 
132  //-------------------------------------------------
133 
134 
136  {
137  }
138 
139 
140  //-------------------------------------------------
141 
143  {
144  }
145 
146 
147  //-------------------------------------------------
148 
150  {
151  // Infrastructure piece
152  std::unique_ptr<std::vector< raw::OpDetPulse > > StoragePtr (new std::vector<raw::OpDetPulse>);
153 
154  // Create vector of pointers to our waveform pulses for each OpDet
155  std::vector<raw::OpDetPulse*> ThePulses;
156 
157  if(!fTextFile)
158  {
159  mf::LogError("BoDataFrameInput")<<"Error in reading input file : " << fInputFile;
160  }
161 
162  // Build an array of comma separated commands
163 
164  mf::LogInfo("BoDataFrameInput")<<"Reading optical pulses for event " <<evt.id().event() ;
165  std::vector<std::string> Commands;
166  std::vector<unsigned int> Values;
167 
168  bool ContinueRead = true;
169  std::string sub("");
170  std::string line("");
171 
172  unsigned int CurrentChannel=0;
173 
174 
175  unsigned int ChannelsThisEvent=0;
176  unsigned int FEMModule=0;
177  unsigned int TriggerID=0;
178  unsigned int EventFrameNumber=0;
179  unsigned int TriggerTypeThisChannel=0;
180  unsigned int PMTFrameNumber=0;
181 
182  unsigned int FirstSample=0;
183 
184  std::map<int, std::vector<raw::OpDetPulse*> > PulsesThisEvent;
185 
186  while(fTextFile.good() && ContinueRead)
187  {
188  getline(fTextFile, line);
189  std::stringstream linestream(line);
190  while(linestream.good() && ContinueRead)
191  {
192  getline(linestream, sub, ',');
193  std::stringstream ss(sub);
194  std::string Command("");
195  int Value=0;
196  ss>>Command;
197  ss>>Value;
198  mf::LogInfo("BoDataFrameInput")<<"Parsed a single line as C: " << Command<< " V: "<<Value;
199 
200  // Structural commands
201  if(Command=="evt")
202  {
203  // This command specifies the beginning of the next readout event.
204  // if we find it, finish processing for now.
205  mf::LogInfo("BoDataFrameInput")<<"Found start of next event, ID = " << Value<<", finish frame readout";
206  ContinueRead=false;
207  }
208  else if(Command=="nch")
209  {
210  // Number of channels in this event readout
211  if(ChannelsThisEvent==0)
212  ChannelsThisEvent=Value;
213  else
214  mf::LogInfo("BoDataFrameInput")<<"Confused by data input: nch specified twice for the same event. Persevering anyway...";
215  }
216  else if(Command=="mod")
217  {
218  // ID of the FEM module
219  FEMModule=Value;
220  Value=FEMModule; // This line just suppresses a compiler warning
221  }
222  else if(Command=="tid")
223  {
224  // Trigger type (cosmic, michel, etc)
225  TriggerID=Value;
226  Value = TriggerID; // This line just suppresses a compiler warning
227  }
228  else if(Command=="efr")
229  {
230  // The event frame number - see longer note under pfr
231  EventFrameNumber=Value;
232  }
233 
234 
235  // Per readout window commands
236 
237  else if(Command=="chn")
238  {
239  // chn tells us the channel number on the shaper board
240  mf::LogInfo("BoDataFrameInput")<<"Beginning channel " <<Value;
241 
242  CurrentChannel = Value;
243  PulsesThisEvent[CurrentChannel].push_back( new raw::OpDetPulse(CurrentChannel) );
244 
245  FirstSample = 0;
246  PMTFrameNumber = 0;
247  }
248  else if(Command=="cid")
249  {
250  // This gives the trigger type. Encoding not totally clear,
251  // but we know 16 = external trigger
252  TriggerTypeThisChannel=Value;
253  Value =TriggerTypeThisChannel; // This line just suppresses a compiler warning
254  }
255  else if(Command=="smp")
256  {
257  // smp tells us which sample to start at within the window
258  FirstSample=Value;
259  PulsesThisEvent[CurrentChannel].at(PulsesThisEvent[CurrentChannel].size()-1)->SetFirstSample(FirstSample);
260  }
261  else if(Command=="pfr")
262  {
263  // This part is a bit complicated.
264  // The PMT frame number tells us which readout frame
265  // this signal occupies.
266  // The PMT frame always occurs after the event frame.
267  // The pfr value is the last 3 bits of the PMT frame number
268  // So the goal is to find the first number after efr with
269  // these last 3 bits.
270 
271  // In practice :
272  // if the last 3 bits of efr are > pfr
273  // the PMT frame is (all but 3 lsb of)efr + 8 + pfr
274  // if the last 3 bits of efr are < pfr
275  // the PMT frame is (all but 3 lsb of)efr + pfr
276 
277  // This mask gets the 3LSB
278  int mask = 0x007;
279 
280  int EvLowBits = EventFrameNumber & mask;
281  int EvHighBits = EventFrameNumber & ~mask;
282 
283  if(Value < EvLowBits)
284  {
285  PMTFrameNumber = EvHighBits + 8 + Value;
286  }
287  else if(Value > EvLowBits)
288  {
289  PMTFrameNumber = EvHighBits + Value;
290  }
291  else if(Value == EvLowBits)
292  {
293  PMTFrameNumber = EvHighBits + EvLowBits;
294  }
295  else
296  {
297  mf::LogError("BoDataFrameInput") << "pfr routine confused - this should be impossible";
298  }
299 
300  PulsesThisEvent[CurrentChannel].at(PulsesThisEvent[CurrentChannel].size()-1)->SetPMTFrame(PMTFrameNumber);
301 
302  }
303  else if(Command=="adc")
304  {
305  // Add one sample to the pulse object
306  PulsesThisEvent[CurrentChannel].at(PulsesThisEvent[CurrentChannel].size()-1)->Waveform().push_back(Value);
307  }
308  }
309  }
310 
311 
312  // Now pack away the pulses we found into the event
313  for(std::map<int, std::vector<raw::OpDetPulse*> >::const_iterator it = PulsesThisEvent.begin();
314  it!=PulsesThisEvent.end(); ++it)
315  {
316  for(size_t pulse=0; pulse!=it->second.size(); ++pulse)
317  {
318  StoragePtr->push_back( *(it->second.at(pulse)) );
319  }
320  }
321 
322  evt.put(std::move(StoragePtr));
323  }
324 }
325 
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
Float_t ss
Definition: plot.C:23
MaybeLogger_< ELseverityLevel::ELsev_error, false > LogError
ProductID put(std::unique_ptr< PROD > &&product)
Definition: Event.h:102
contains objects relating to OpDet hits
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:42
BoDataFrameInput(const fhicl::ParameterSet &)
T get(std::string const &key) const
Definition: ParameterSet.h:231
EventNumber_t event() const
Definition: EventID.h:117
TCEvent evt
Definition: DataStructs.cxx:5
Tools and modules for checking out the basics of the Monte Carlo.
EventID id() const
Definition: Event.h:56
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33