Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MicromegasRawDataDecoder.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file MicromegasRawDataDecoder.cc
1 
7 #include "MicromegasDefs.h"
8 
9 #include <Event/Event.h>
10 #include <Event/EventTypes.h>
11 #include <Event/packet.h>
12 
14 
15 #include <phool/getClass.h>
16 #include <phool/PHCompositeNode.h>
17 #include <phool/PHNodeIterator.h>
18 
19 #include <trackbase/TrkrHitv2.h>
20 #include <trackbase/TrkrHitSet.h>
22 
23 #include <algorithm>
24 #include <cassert>
25 #include <memory>
26 
27 //_________________________________________________________
29  SubsysReco( name )
30 {}
31 
32 //_____________________________________________________________________
34 {
35  // read calibrations
38 }
39 
40 //____________________________________________________________________________..
42 {
43 
44  // get dst node
45  PHNodeIterator iter(topNode);
46  auto dstNode = dynamic_cast<PHCompositeNode *>(iter.findFirst("PHCompositeNode", "DST"));
47  if (!dstNode)
48  {
49  std::cout << "MicromegasRawDataDecoder::InitRun - DST Node missing, doing nothing." << std::endl;
50  exit(1);
51  }
52 
53  // create hitset container if needed
54  auto hitsetcontainer = findNode::getClass<TrkrHitSetContainer>(topNode, "TRKR_HITSET");
55  if (!hitsetcontainer)
56  {
57  std::cout << "MicromegasRawDataDecoder::InitRun - creating TRKR_HITSET." << std::endl;
58 
59  // find or create TRKR node
60  PHNodeIterator dstiter(dstNode);
61  auto trkrnode = dynamic_cast<PHCompositeNode *>(dstiter.findFirst("PHCompositeNode", "TRKR"));
62  if (!trkrnode)
63  {
64  trkrnode = new PHCompositeNode("TRKR");
65  dstNode->addNode(trkrnode);
66  }
67 
68  // create container and add to the tree
69  hitsetcontainer = new TrkrHitSetContainerv1;
70  auto newNode = new PHIODataNode<PHObject>(hitsetcontainer, "TRKR_HITSET", "PHObject");
71  trkrnode->addNode(newNode);
72  }
73 
75 
76 }
77 
78 //___________________________________________________________________________
80 {
81 
82  // load relevant nodes
83  // Get the TrkrHitSetContainer node
84  auto trkrhitsetcontainer = findNode::getClass<TrkrHitSetContainer>(topNode, "TRKR_HITSET");
85  assert(trkrhitsetcontainer);
86 
87  // PRDF node
88  auto event = findNode::getClass<Event>(topNode, "PRDF");
89  assert( event );
90 
91  // check event type
92  if(event->getEvtType() >= 8)
94 
95  // loop over TPOT packets
96  for( const auto& packet_id:MicromegasDefs::m_packet_ids )
97  {
98  std::unique_ptr<Packet> packet( event->getPacket(packet_id) );
99  if( !packet )
100  {
101  // no data
102  if( Verbosity() )
103  { std::cout << "MicromegasRawDataDecoder::process_event - event contains no TPOT data" << std::endl; }
104  continue;
105  }
106 
107  // get number of datasets (also call waveforms)
108  const auto n_waveforms = packet->iValue(0, "NR_WF" );
109  if( Verbosity() )
110  { std::cout << "MicromegasRawDataDecoder::process_event - n_waveforms: " << n_waveforms << std::endl; }
111  for( int iwf=0; iwf<n_waveforms; ++iwf )
112  {
113 
114  const int fee = packet->iValue(iwf, "FEE" );
115  const auto channel = packet->iValue( iwf, "CHANNEL" );
116  const int samples = packet->iValue( iwf, "SAMPLES" );
117 
118  // beam crossing, checksum, checksum error
119  [[maybe_unused]] const int bco = packet->iValue(iwf, "BCO");
120  [[maybe_unused]] const int checksum = packet->iValue(iwf, "CHECKSUM");
121  [[maybe_unused]] const int checksum_error = packet->iValue(iwf, "CHECKSUMERROR");
122 
123  // get channel rms and pedestal from calibration data
124  const double pedestal = m_calibration_data.get_pedestal( fee, channel );
125  const double rms = m_calibration_data.get_rms( fee, channel );
126 
127  // a rms of zero means the calibration has failed. the data is unusable
128  if( rms <= 0 ) continue;
129 
130  // loop over samples find maximum
131  std::vector<int> adc;
132  for( int is = std::max( m_sample_min,0 ); is < std::min( m_sample_max,samples ); ++ is )
133  { adc.push_back( packet->iValue( iwf, is ) ); }
134 
135  if( adc.empty() ) continue;
136 
137  // get max adc value in range
138  /* TODO: use more advanced signal processing */
139  auto max_adc = *std::max_element( adc.begin(), adc.end() );
140 
141  // compare to hard min_adc value
142  if( max_adc < m_min_adc ) continue;
143 
144  // compare to threshold
145  if( max_adc < pedestal + m_n_sigma * rms ) continue;
146 
147  // subtract pedestal
148  max_adc -= pedestal;
149 
150  // map fee and channel to physical hitsetid and physical strip
151  // get hitset key matching this fee
153  if( !hitsetkey ) continue;
154 
155  // get matching physical strip
156  int strip = m_mapping.get_physical_strip( fee, channel );
157  if( strip < 0 ) continue;
158 
159  if( Verbosity() )
160  {
161  std::cout << "MicromegasRawDataDecoder::process_event -"
162  << " bco: " << bco
163  << " errir: " << checksum_error
164  << " layer: " << int(TrkrDefs::getLayer(hitsetkey))
165  << " tile: " << int( MicromegasDefs::getTileId( hitsetkey ))
166  << " channel: " << channel
167  << " strip: " << strip
168  << " adc: " << max_adc
169  << std::endl;
170  }
171 
172  // get matching hitset
173  const auto hitset_it = trkrhitsetcontainer->findOrAddHitSet(hitsetkey);
174 
175  // generate hit key
177 
178  // find existing hit, or create
179  auto hit = hitset_it->second->getHit(hitkey);
180  if( hit )
181  {
182  // std::cout << "MicromegasRawDataDecoder::process_event - duplicated hit, hitsetkey: " << hitsetkey << " strip: " << strip << std::endl;
183  continue;
184  }
185 
186  // create hit, assign adc and insert in hitset
187  hit = new TrkrHitv2;
188  hit->setAdc( max_adc );
189  hitset_it->second->addHitSpecificKey(hitkey, hit);
190 
191  // increment counter
193  }
194  }
196 }
197 
198 //_____________________________________________________________________
200 {
201  // if( Verbosity() )
202  {
203  for( const auto& [hitsetkey, count]:m_hitcounts )
204  { std::cout << "MicromegasRawDataDecoder::End - hitsetkey: " << hitsetkey << ", count: " << count << std::endl; }
205  }
206 
208 }