tpcFragmentToRawDigits.cc
Go to the documentation of this file.
1 // tpcFragmentToRawDigits.cc
2 
5 
6 std::vector<raw::RawDigit>
7 DAQToOffline::tpcFragmentToRawDigits(artdaq::Fragments const& rawFragments,
8  std::vector<std::pair <std::pair<unsigned int,unsigned int>,lbne::TpcNanoSlice::Header::nova_timestamp_t> > &DigitsIndexList,
9  lbne::TpcNanoSlice::Header::nova_timestamp_t& firstTimestamp,
10  art::ServiceHandle<lbne::ChannelMapService> const& channelMap, bool useChannelMap,
11  bool debug, raw::Compress_t compression, unsigned int zeroThreshold) {
12 
13  DigitsIndexList.clear();
14  //Create a map containing (fragmentID, fragIndex) for the event, will be used to check if each channel is present
15  unsigned int numFragments = rawFragments.size();
16  bool TimestampSet = false;
17 
18  std::map < unsigned int, unsigned int > mapFragID;
19 
20  for(size_t fragIndex = 0; fragIndex < rawFragments.size(); fragIndex++){
21 
22  const artdaq::Fragment &singleFragment = rawFragments[fragIndex];
23 
24  unsigned int fragmentID = singleFragment.fragmentID();
25 
26  mapFragID.insert(std::pair<unsigned int, unsigned int>(fragmentID,fragIndex));
27  }
28 
29 
30  if(debug){
31  std::cout << numFragments<< " rawFragments" << std::endl;
32  }
33 
34  std::vector<raw::RawDigit> rawDigitVector;
35 
36  //JPD -- first go at unpacking the information
37  // -- seems to make sense to look through channel number,
38  // -- then we'll create a rawDigit object for each channel
39  // -- will need some helper functions to do this for us, so I created a utilites directory
40 
42  size_t numChans = geometry->Nchannels();
43  for(size_t chan=0;chan < numChans;chan++){
44 
45  //Each channel is uniquely identified by (fragmentID, group, sample) in an online event
46 
47  unsigned int fragmentID = UnpackFragment::getFragIDForChan(chan);
48  unsigned int sample = UnpackFragment::getNanoSliceSampleForChan(chan);
49 
50  if (debug) {
51  std::cout << "channel: " << chan
52  << "\tfragment: " << fragmentID
53  //<< "\tgroup: " << group
54  << "\tsample: " << sample
55  << std::endl;
56  }
57 
58  //Check that the necessary fragmentID is present in the event
59  //i.e. do we have data for this channel?
60 
61  if( mapFragID.find(fragmentID) == mapFragID.end() ){
62 
63  if (debug) std::cout << "Fragment not found" << std::endl;
64  continue;
65 
66  }
67 
68  unsigned int fragIndex = mapFragID[fragmentID];
69 
70  if (debug) std::cout << "fragIndex: " << fragIndex << std::endl;
71 
72  std::vector<short> adcvec;
73 
74 
75  const artdaq::Fragment &singleFragment = rawFragments[fragIndex];
76  lbne::TpcMilliSliceFragment millisliceFragment(singleFragment);
77 
78  //Properties of fragment
79  auto numMicroSlices = millisliceFragment.microSliceCount();
80  bool FirstMicro = false;
81  unsigned int FirstGoodIndex = 0;
82  unsigned int LastGoodIndex = 0;
83  bool PrevMicroRCE = false;
84  lbne::TpcNanoSlice::Header::nova_timestamp_t ThisTimestamp = 0;
85  bool MadeList = DigitsIndexList.size();
86  for(unsigned int i_micro=0;i_micro<numMicroSlices;i_micro++){
87  std::unique_ptr <const lbne::TpcMicroSlice> microSlice = millisliceFragment.microSlice(i_micro);
88  auto numNanoSlices = microSlice->nanoSliceCount();
89 
90  // Push back adcvec with the ADC values.
91  for(uint32_t i_nano=0; i_nano < numNanoSlices; i_nano++){
93  bool success = microSlice->nanosliceSampleValue(i_nano, sample, val);
94  if(success) adcvec.push_back(short(val));
95  }
96 
97  if ( chan%128==0 ) {
98  //std::cout << "Looking at microslice " << i_micro << ", it has " << numNanoSlices << " nanoslices." << std::endl;
99  // Get the First Timestamp for this channel
100  if (!FirstMicro && numNanoSlices) {
101  lbne::TpcNanoSlice::Header::nova_timestamp_t Timestamp = microSlice->nanoSlice(0)->nova_timestamp();
102  FirstMicro=true;
103  if (!TimestampSet || Timestamp < firstTimestamp) {
104  //std::cout << "!!!Resetting timestamp to " << Timestamp << " on Chan " << chan << ",Micro " << i_micro << "!!!" << std::endl;
105  firstTimestamp = Timestamp;
106  TimestampSet = true;
107  }
108  }
109  // Which indexes have RCE information?
110  if (!MadeList) {
111  if (numNanoSlices) {
112  LastGoodIndex = LastGoodIndex + numNanoSlices;
113  if (PrevMicroRCE == false)
114  ThisTimestamp = microSlice->nanoSlice(0)->nova_timestamp();
115  }
116  if (PrevMicroRCE == true && ( !numNanoSlices || i_micro==numMicroSlices-1 ) ) {
117  //std::cout << "This is the end of a good set of RCEs, so want to add a pair to my vector...." << std::endl;
118  DigitsIndexList.push_back( std::make_pair( std::make_pair(FirstGoodIndex,LastGoodIndex-1), ThisTimestamp) );
119  FirstGoodIndex = LastGoodIndex;
120  }
121  PrevMicroRCE = (bool)numNanoSlices;
122  } // If not made index list
123  } // Do stuff once for each RCE...
124  }
125  // Make my list of good RCEs
126  if (DigitsIndexList.size() && !MadeList ) {
127  //std::cout << "Finished looking through microslices. DigitsIndexList has size " << DigitsIndexList.size() << ", the useful things were in these microslices." << std::endl;
128  //for (size_t qq=0; qq<DigitsIndexList.size(); ++qq)
129  //std::cout << "Looking at element " << qq << " start " << DigitsIndexList[qq].first.first << ", end " << DigitsIndexList[qq].first.second << " timestamp " << DigitsIndexList[qq].second << std::endl;
130  }
131 
132  if (debug) std::cout << "adcvec->size(): " << adcvec.size() << std::endl;
133  unsigned int numTicks = adcvec.size();
134  raw::Compress(adcvec, compression, zeroThreshold);
135  int offlineChannel = -1;
136  if (useChannelMap) offlineChannel = channelMap->Offline(chan);
137  else offlineChannel = chan;
138  raw::RawDigit theRawDigit(offlineChannel, numTicks, adcvec, compression);
139  rawDigitVector.push_back(theRawDigit); // add this digit to the collection
140  }
141 
142  return rawDigitVector;
143 }
144 
145 // NOvA time standard is a 56 bit timestamp at an LSB resolution of 15.6 ns (64 MHz)
146 // and a starting epoch of January 1, 2010 at 00:00:00.
147 // D. Adams March 2016: Update this to store the sec in the high word and remaing ns in
148 // the low word. Previous version stored 0 in the high word and sec in the low word.
150 make_art_timestamp_from_nova_timestamp(lbne::TpcNanoSlice::Header::nova_timestamp_t novaTime) {
151  return DuneTimeConverter:: fromNova(novaTime);
152 }
153 
155 old_make_art_timestamp_from_nova_timestamp(lbne::TpcNanoSlice::Header::nova_timestamp_t this_nova_timestamp) {
156  lbne::TpcNanoSlice::Header::nova_timestamp_t seconds_since_nova_epoch = (this_nova_timestamp/nova_time_ticks_per_second);
157  TTimeStamp time_of_event(20100101u,
158  0u,
159  0u,
160  true,
161  seconds_since_nova_epoch);
162  return art::Timestamp(time_of_event.GetSec());
163 }
enum raw::_compress Compress_t
static art::Timestamp fromNova(uint64_t tnova)
Collection of charge vs time digitized from a single readout channel.
Definition: RawDigit.h:69
unsigned int getNanoSliceSampleForChan(unsigned int channel)
struct vector vector
unsigned short uint16_t
Definition: stdint.h:125
std::vector< raw::RawDigit > tpcFragmentToRawDigits(artdaq::Fragments const &rawFragments, std::vector< std::pair< std::pair< unsigned int, unsigned int >, lbne::TpcNanoSlice::Header::nova_timestamp_t > > &DigitsIndexList, lbne::TpcNanoSlice::Header::nova_timestamp_t &firstTimestamp, art::ServiceHandle< lbne::ChannelMapService > const &channelMap, bool useChannelMap, bool debug, raw::Compress_t compression, unsigned int zeroThreshold)
unsigned int Nchannels() const
Returns the number of TPC readout channels in the detector.
unsigned int getFragIDForChan(unsigned int channel)
unsigned int uint32_t
Definition: stdint.h:126
art::Timestamp make_art_timestamp_from_nova_timestamp(lbne::TpcNanoSlice::Header::nova_timestamp_t this_nova_timestamp)
art::Timestamp old_make_art_timestamp_from_nova_timestamp(lbne::TpcNanoSlice::Header::nova_timestamp_t this_nova_timestamp)
static int max(int a, int b)
void Compress(std::vector< short > &adc, raw::Compress_t compress)
Compresses a raw data buffer.
Definition: raw.cxx:19
int bool
Definition: qglobal.h:345
const lbne::TpcNanoSlice::Header::nova_timestamp_t nova_time_ticks_per_second
QTextStream & endl(QTextStream &s)