Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TpcCombinedRawDataUnpacker.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file TpcCombinedRawDataUnpacker.cc
2 
3 #include <trackbase/TpcDefs.h>
4 #include <trackbase/TrkrDefs.h> // for hitkey, hitsetkey
5 #include <trackbase/TrkrHit.h>
6 #include <trackbase/TrkrHitSet.h>
10 #include <trackbase/TrkrHitv2.h>
11 
16 
17 #include <cdbobjects/CDBTTree.h>
19 
21 
22 #include <Event/Event.h>
23 #include <Event/EventTypes.h>
24 #include <Event/packet.h>
25 
30 
31 #include <phool/PHCompositeNode.h>
32 #include <phool/PHIODataNode.h> // for PHIODataNode
33 #include <phool/PHNodeIterator.h>
34 #include <phool/PHObject.h> // for PHObject
35 #include <phool/getClass.h>
36 #include <phool/phool.h> // for PHWHERE
37 
38 #include <TSystem.h>
39 
40 #include <TFile.h>
41 #include <TH1.h>
42 #include <TNtuple.h>
43 #include <cstdlib> // for exit
44 #include <iostream> // for operator<<, endl, bas...
45 #include <map> // for _Rb_tree_iterator
46 
48  : SubsysReco(name)
49  , outfile_name(outF)
50 {
51  // Do nothing
52 }
53 
55 {
56  std::cout << "TpcRawDataDecoder::Init(PHCompositeNode *topNode) Initializing" << std::endl;
57 
59  std::string calibdir = m_cdb->getUrl("TPC_FEE_CHANNEL_MAP");
60 
61  if (calibdir[0] == '/')
62  {
63  // use generic CDBTree to load
64  m_cdbttree = new CDBTTree(calibdir.c_str());
66  }
67  else
68  {
69  std::cout << "TpcRawDataDecoder::::InitRun No calibration file found" << std::endl;
70  exit(1);
71  }
72 
74 }
75 
77 {
78  if (!topNode)
79  {
80  std::cout << "TpcCombinedRawDataUnpacker::InitRun(PHCompositeNode* topNode)" << std::endl;
81  std::cout << "\tCould not retrieve topNode; doing nothing" << std::endl;
82  exit(1);
83  gSystem->Exit(1);
84 
85  return 1;
86  }
87 
88  PHNodeIterator dst_itr(topNode);
89  PHCompositeNode* dst_node = dynamic_cast<PHCompositeNode*>(dst_itr.findFirst("PHCompositeNode", "DST"));
90  if (!dst_node)
91  {
92  if (Verbosity())
93  {
94  std::cout << "TpcCombinedRawDataUnpacker::InitRun(PHCompositeNode* topNode)" << std::endl;
95  }
96  if (Verbosity())
97  {
98  std::cout << "\tCould not retrieve dst_node; doing nothing" << std::endl;
99  }
100  exit(1);
101  gSystem->Exit(1);
102 
103  return 1;
104  }
105 
106  PHNodeIterator trkr_itr(dst_node);
107  PHCompositeNode* trkr_node = dynamic_cast<PHCompositeNode*>(trkr_itr.findFirst("PHCompositeNode", "TRKR"));
108  if (!trkr_node)
109  {
110  trkr_node = new PHCompositeNode("TRKR");
111  dst_node->addNode(trkr_node);
112  }
113 
114  TrkrHitSetContainer* trkr_hit_set_container = findNode::getClass<TrkrHitSetContainer>(topNode, "TRKR_HITSET");
115  if (!trkr_hit_set_container)
116  {
117  if (Verbosity())
118  {
119  std::cout << "TpcCombinedRawDataUnpacker::InitRun(PHCompositeNode* topNode)" << std::endl;
120  }
121  if (Verbosity())
122  {
123  std::cout << "\tMaking TrkrHitSetContainer" << std::endl;
124  }
125 
126  trkr_hit_set_container = new TrkrHitSetContainerv1;
127  PHIODataNode<PHObject>* new_node = new PHIODataNode<PHObject>(trkr_hit_set_container, "TRKR_HITSET", "PHObject");
128  trkr_node->addNode(new_node);
129  }
130  if (m_writeTree)
131  {
132  m_file = new TFile(outfile_name.c_str(), "RECREATE");
133  m_ntup = new TNtuple("NT", "NT", "event:gtmbco:packid:ep:sector:side:fee:chan:sampadd:sampch:nsamples");
134  }
136 }
137 
139 {
140  _ievent++;
141  TH1F pedhist("pedhist", "pedhist", 251, -0.5, 1000.5);
142 
143  TrkrHitSetContainer* trkr_hit_set_container = findNode::getClass<TrkrHitSetContainer>(topNode, "TRKR_HITSET");
144  if (!trkr_hit_set_container)
145  {
146  std::cout << PHWHERE << std::endl;
147  std::cout << "TpcCombinedRawDataUnpacker::process_event(PHCompositeNode* topNode)" << std::endl;
148  std::cout << "Could not get \"TRKR_HITSET\" from Node Tree" << std::endl;
149  std::cout << "Exiting" << std::endl;
150  gSystem->Exit(1);
151  exit(1);
152 
154  }
155 
156  TpcRawHitContainerv1* tpccont = findNode::getClass<TpcRawHitContainerv1>(topNode, m_TpcRawNodeName);
157  if (!tpccont)
158  {
159  std::cout << PHWHERE << std::endl;
160  std::cout << "TpcCombinedRawDataUnpacker::process_event(PHCompositeNode* topNode)" << std::endl;
161  std::cout << "Could not get \"" << m_TpcRawNodeName << "\" from Node Tree" << std::endl;
162  std::cout << "Exiting" << std::endl;
163 
164  gSystem->Exit(1);
165  exit(1);
166  }
167 
168  PHG4TpcCylinderGeomContainer* geom_container =
169  findNode::getClass<PHG4TpcCylinderGeomContainer>(topNode, "CYLINDERCELLGEOM_SVTX");
170  if (!geom_container)
171  {
172  std::cout << PHWHERE << "ERROR: Can't find node CYLINDERCELLGEOM_SVTX" << std::endl;
174  }
175 
176  TrkrDefs::hitsetkey hit_set_key = 0;
177  TrkrDefs::hitkey hit_key = 0;
178  TrkrHitSetContainer::Iterator hit_set_container_itr;
179  TrkrHit* hit = nullptr;
180 
181  uint64_t bco_min = UINT64_MAX;
182  uint64_t bco_max = 0;
183 
184  const auto nhits = tpccont->get_nhits();
185 
186  for (unsigned int i = 0; i < nhits; i++)
187  {
188  TpcRawHit* tpchit = tpccont->get_hit(i);
189  uint64_t gtm_bco = tpchit->get_gtm_bco();
190 
191  if (gtm_bco < bco_min)
192  {
193  bco_min = gtm_bco;
194  }
195  if (gtm_bco > bco_max)
196  {
197  bco_max = gtm_bco;
198  }
199 
200  int fee = tpchit->get_fee();
201  int channel = tpchit->get_channel();
202  int feeM = FEE_map[fee];
203  if (FEE_R[fee] == 2) feeM += 6;
204  if (FEE_R[fee] == 3) feeM += 14;
205 
206  int side = 1;
207  int32_t packet_id = tpchit->get_packetid();
208  int ep = (packet_id - 4000) % 10;
209  int sector = (packet_id - 4000 - ep) / 10;
210  if (sector > 11) side = 0;
211 
212  unsigned int key = 256 * (feeM) + channel;
213  std::string varname = "layer";
214  int layer = m_cdbttree->GetIntValue(key, varname);
215  // antenna pads will be in 0 layer
216  if (layer == 0) continue;
217 
218  uint16_t sampadd = tpchit->get_sampaaddress();
219  uint16_t sampch = tpchit->get_sampachannel();
220  uint16_t sam = tpchit->get_samples();
221 
222  varname = "phi"; // + std::to_string(key);
223  double phi = -1 * pow(-1, side) * m_cdbttree->GetDoubleValue(key, varname) + (sector % 12) * M_PI / 6;
224  PHG4TpcCylinderGeom* layergeom = geom_container->GetLayerCellGeom(layer);
225  unsigned int phibin = layergeom->find_phibin(phi);
226  if (m_writeTree)
227  {
228  float fX[12];
229  int n = 0;
230 
231  fX[n++] = _ievent - 1;
232  fX[n++] = gtm_bco;
233  fX[n++] = packet_id;
234  fX[n++] = ep;
235  fX[n++] = sector;
236  fX[n++] = side;
237  fX[n++] = fee;
238  fX[n++] = channel;
239  fX[n++] = sampadd;
240  fX[n++] = sampch;
241  fX[n++] = sam;
242  m_ntup->Fill(fX);
243  }
244  hit_set_key = TpcDefs::genHitSetKey(layer, (mc_sectors[sector % 12]), side);
245  hit_set_container_itr = trkr_hit_set_container->findOrAddHitSet(hit_set_key);
246 
247  float hpedestal = 0;
248  float hpedwidth = 0;
249  pedhist.Reset();
250 
251  for (uint16_t sampleNum = 0; sampleNum < sam; sampleNum++)
252  {
253  uint16_t adc = tpchit->get_adc(sampleNum);
254  pedhist.Fill(adc);
255  }
256  int hmax = 0;
257  int hmaxbin = 0;
258  for (int nbin = 1; nbin <= pedhist.GetNbinsX(); nbin++)
259  {
260  float val = pedhist.GetBinContent(nbin);
261  if (val > hmax)
262  {
263  hmaxbin = nbin;
264  hmax = val;
265  }
266  }
267 
268  // calc peak position
269  double adc_sum = 0.0;
270  double ibin_sum = 0.0;
271  double ibin2_sum = 0.0;
272 
273  for (int isum = -3; isum <= 3; isum++)
274  {
275  float val = pedhist.GetBinContent(hmaxbin + isum);
276  float center = pedhist.GetBinCenter(hmaxbin + isum);
277  ibin_sum += center * val;
278  ibin2_sum += center * center * val;
279  adc_sum += val;
280  }
281 
282  hpedestal = ibin_sum / adc_sum;
283  hpedwidth = sqrt(ibin2_sum / adc_sum - (hpedestal * hpedestal));
284 
285  for (uint16_t s = 0; s < sam; s++)
286  {
287  uint16_t adc = tpchit->get_adc(s);
288  int t = s;
289 
290  if ((float(adc) - hpedestal) > (hpedwidth * 4))
291  {
292  hit_key = TpcDefs::genHitKey(phibin, (unsigned int) t);
293  // find existing hit, or create new one
294  hit = hit_set_container_itr->second->getHit(hit_key);
295  if (!hit)
296  {
297  hit = new TrkrHitv2();
298  hit->setAdc(float(adc) - hpedestal);
299 
300  hit_set_container_itr->second->addHitSpecificKey(hit_key, hit);
301  }
302  }
303  }
304  }
305 
306  if (Verbosity())
307  {
308  std::cout << " event BCO: " << bco_min << " - " << bco_max << std::endl;
309  std::cout << "TpcCombinedRawDataUnpacker:: done" << std::endl;
310  }
311 
313 }
314 
316 {
317  if (m_writeTree)
318  {
319  m_file->cd();
320  m_ntup->Write();
321  m_file->Close();
322  }
323  if (Verbosity()) std::cout << "TpcCombinedRawDataUnpacker::End(PHCompositeNode *topNode) This is the End..." << std::endl;
324  // if(m_Debug==1) hm->dumpHistos(m_filename, "RECREATE");
325 
327 }