Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MicromegasCombinedDataEvaluation.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file MicromegasCombinedDataEvaluation.cc
1 
7 #include "MicromegasDefs.h"
8 
11 
13 
14 #include <phool/getClass.h>
15 #include <phool/PHCompositeNode.h>
16 
17 #include <TFile.h>
18 #include <TH1.h>
19 #include <TH2.h>
20 #include <TProfile.h>
21 
22 #include <cassert>
23 #include <fstream>
24 #include <list>
25 #include <memory>
26 
27 namespace
28 {
29 
30  // streamer for lists
31  template< class T >
32  std::ostream& operator << ( std::ostream& out, const std::list<T>& list )
33  {
34  if( list.empty() ) out << "{}";
35  else
36  {
37  out << "{ ";
38  bool first = true;
39  for( const auto& value:list )
40  {
41  if( !first ) out << ", ";
42  out << value;
43  first = false;
44  }
45 
46  out << " }";
47  }
48 
49  return out;
50  }
51 
52 }
53 
54 
55 //_________________________________________________________
57 {
58  packet_id = sample.packet_id;
59  lvl1_bco = sample.lvl1_bco;
60  fee_bco = sample.fee_bco;
61  checksum = sample.checksum;
63  fee_id = sample.fee_id;
64  layer = sample.layer;
65  tile = sample.tile;
68  channel = sample.channel;
69  strip = sample.strip;
70  sample_max = sample.sample;
71  adc_max = sample.adc;
72  pedestal = sample.pedestal;
73  rms = sample.rms;
74 }
75 
76 //_________________________________________________________
78 {
79  n_tagger.clear();
80  n_waveform.clear();
81  samples.clear();
82  waveforms.clear();
83  lvl1_bco_list.clear();
84  lvl1_count_list.clear();
85 }
86 
87 //_________________________________________________________
89  SubsysReco( name )
90 {}
91 
92 //_____________________________________________________________________
94 {
95  // read calibrations
97 
98  m_evaluation_file.reset( new TFile( m_evaluation_filename.c_str(), "RECREATE" ) );
99  m_evaluation_tree = new TTree( "T", "T" );
100  m_evaluation_tree->SetAutoSave( 5000 );
101  m_container = new Container;
102  m_evaluation_tree->Branch( "Event", &m_container );
104 }
105 
106 //____________________________________________________________________________..
109 
110 //___________________________________________________________________________
112 {
113 
114  // load raw hits container
115  auto rawhitcontainer = findNode::getClass<MicromegasRawHitContainer>(topNode, m_rawhitnodename);
116  assert( rawhitcontainer );
117 
118  // reset container
119  m_container->Reset();
120 
121  // temporary storage for samples and waveforms, sorted by lvl1 bco
122  std::multimap<uint64_t, Sample> sample_map;
123  std::multimap<uint64_t, Waveform> waveform_map;
124 
125  // map number of waveforms per packet
126  std::map<unsigned int, size_t> packet_waveforms;
127 
128  // loop over raw hits
129  if( Verbosity() )
130  { std::cout << "MicromegasCombinedDataEvaluation::process_event - hits: " << rawhitcontainer->get_nhits() << std::endl; }
131 
132  bool first = true;
133  uint64_t first_lvl1_bco = 0;
134 
135 
136  for( unsigned int ihit = 0; ihit < rawhitcontainer->get_nhits(); ++ihit )
137  {
138  const auto rawhit = rawhitcontainer->get_hit(ihit);
139  const auto packet_id = rawhit->get_packetid();
140 
141  // make sure packet is valid
143  {
144  std::cout << "MicromegasCombinedDataEvaluation::process_event - invalid packet: " << packet_id << std::endl;
145  continue;
146  }
147 
148  ++packet_waveforms[packet_id];
149 
150  // create running sample, assign packet, fee, layer and tile id
151  Sample sample;
152  sample.packet_id = packet_id;
153  sample.fee_id = rawhit->get_fee();
154 
155  const auto hitsetkey = m_mapping.get_hitsetkey(sample.fee_id);
156  sample.layer = TrkrDefs::getLayer( hitsetkey );
158 
159  // beam crossing
160  sample.fee_bco = rawhit->get_bco();
161  sample.lvl1_bco = rawhit->get_gtm_bco();
162 
163  if( first )
164  {
165  first = false;
166  first_lvl1_bco = rawhit->get_gtm_bco();
167  }
168 
169  // increment bco map
170  // ++m_bco_map[sample.lvl1_bco];
171 
172  ++m_bco_map[first_lvl1_bco];
173 
174 // // checksum and checksum error
175 // sample.checksum = rawhit->get_checksum();
176 // sample.checksum_error = rawhit->get_checksum_error();
177 
178  // channel, sampa_channel, sampa address and strip
179  sample.sampa_address = rawhit->get_sampaaddress();
180  sample.sampa_channel = rawhit->get_sampachannel();
181  sample.channel = rawhit->get_channel();
182  sample.strip = m_mapping.get_physical_strip(sample.fee_id, sample.channel);
183 
184  // get channel rms and pedestal from calibration data
185  const double pedestal = m_calibration_data.get_pedestal( sample.fee_id, sample.channel );
186  const double rms = m_calibration_data.get_rms( sample.fee_id, sample.channel );
187  sample.pedestal = pedestal;
188  sample.rms = rms;
189 
190  // get number of samples and loop
191  const auto samples = rawhit->get_samples();
192  if( Verbosity() > 1 )
193  {
194  std::cout << "MicromegasCombinedDataEvaluation::process_event -"
195  << " fee: " << sample.fee_id
196  << " tile: " << sample.tile
197  << " layer: " << sample.layer
198  << " tile: " << sample.tile
199  << " lvl1_bco: " << sample.lvl1_bco
200  << " fee_bco: " << sample.fee_bco
201  << " error: " << sample.checksum_error
202  << " channel: " << sample.channel
203  << " strip: " << sample.strip
204  << " samples: " << samples
205  << std::endl;
206  }
207 
208  Sample sample_max;
209  for( unsigned short is = 0; is < std::min<unsigned short>( samples, 100 ); ++is )
210  {
211  // assign sample id and corresponding adc, save copy in container
212  auto adc = rawhit->get_adc(is);
213  sample.sample = is;
214  sample.adc = adc;
215  sample_map.emplace( sample.lvl1_bco, sample );
216 
217  if( sample.adc > sample_max.adc )
218  { sample_max = sample; }
219 
220  }
221 
222  // create waveform
223  Waveform waveform( sample_max );
224  waveform.is_signal =
225  rms > 0 &&
226  waveform.adc_max >= m_min_adc &&
227  waveform.sample_max >= m_sample_min &&
228  waveform.sample_max < m_sample_max &&
229  waveform.adc_max > pedestal+m_n_sigma * rms;
230 
231  waveform_map.emplace( waveform.lvl1_bco, waveform );
232  }
233 
234  // copy all samples and waveform to container
235  for( auto&& [lvl_bco, sample]:sample_map )
236  { m_container->samples.push_back(std::move(sample)); }
237 
238  for( auto&& [lvl1_bco, waveform]:waveform_map )
239  { m_container->waveforms.push_back(std::move(waveform)); }
240 
241  // store number of waveforms
242  for( const auto& [packet_id, n_waveforms]:packet_waveforms )
243  { m_container->n_waveform.push_back(n_waveforms); }
244 
245  // fill evaluation tree
246  m_evaluation_tree->Fill();
247 
249 }
250 
251 //_____________________________________________________________________
253 {
255  {
256  m_evaluation_file->cd();
257  m_evaluation_tree->Write();
258  m_evaluation_file->Close();
259  }
260 
261  // print bco map
262  if( Verbosity() )
263  for( const auto& [bco,nwaveforms]:m_bco_map )
264  { std::cout << "MicromegasCombinedDataEvaluation::End - bco: 0x" << std::hex << bco << std::dec << ", nwaveforms: " << nwaveforms << std::endl; }
265 
266  // print bco list, for offline processing
267  if( Verbosity() )
268  {
269  std::cout << "const std::vector<uint64_t> lvl1_bco_list = {" << std::endl;
270  bool first = true;
271  int count = 0;
272  for( const auto& [bco,nwaveforms]:m_bco_map )
273  {
274  if( !first ) std::cout << ", ";
275  first = false;
276  if( count == 10 )
277  {
278  count = 0;
279  std::cout << std::endl;
280  }
281  std::cout << " 0x" << std::hex << bco << std::dec;
282  ++count;
283  }
284  std::cout << std::endl << "};" << std::endl;
285  }
286 
288 }