13 #include <tpc/TpcDefs.h>
48 #include <TDatabasePDG.h>
56 #include <HepMC/GenEvent.h>
58 #include <CLHEP/Units/SystemOfUnits.h>
62 #include <boost/format.hpp>
73 using namespace CLHEP;
77 unsigned int minLayer,
78 unsigned int m_maxLayer,
81 , m_svtxevalstack(nullptr)
83 , m_saveDataStreamFile(
true)
84 , m_outputFileNameBase(outputfilename)
86 , m_minLayer(minLayer)
87 , m_maxLayer(m_maxLayer)
89 , m_vertexZAcceptanceCut(10)
90 , m_etaAcceptanceCut(1.1)
92 , m_hDataSize(nullptr)
95 , m_hLayerWaveletSize(nullptr)
96 , m_hLayerHit(nullptr)
97 , m_hLayerZBinHit(nullptr)
98 , m_hLayerZBinADC(nullptr)
99 , m_hLayerDataSize(nullptr)
100 , m_hLayerSumHit(nullptr)
101 , m_hLayerSumDataSize(nullptr)
123 for (
unsigned int i = 0;
i < hm->
nHistos();
i++)
127 TTree* T_Index =
new TTree(
"T_Index",
"T_Index");
145 cout <<
"could not locate geo node "
146 <<
"CYLINDERCELLGEOM_SVTX" << endl;
164 if ((
int) nZBins != layerGeom->
get_zbins())
166 cout <<
"TPCMLDataInterface::InitRun - Fatal Error - nZBin at layer " <<
layer <<
" is " << layerGeom->
get_zbins()
167 <<
", which is different from previous layers of nZBin = " << nZBins << endl;
174 cout <<
"TPCMLDataInterface::get_HistoManager - Making PHTFileServer " <<
m_outputFileNameBase +
".root"
181 TH1D*
h =
new TH1D(
"hNormalization",
182 "Normalization;Items;Summed quantity", 10, .5, 10.5);
184 h->GetXaxis()->SetBinLabel(i++,
"Event count");
185 h->GetXaxis()->SetBinLabel(i++,
"Collision count");
186 h->GetXaxis()->SetBinLabel(i++,
"TPC G4Hit");
187 h->GetXaxis()->SetBinLabel(i++,
"TPC G4Hit Edep");
188 h->GetXaxis()->SetBinLabel(i++,
"TPC Hit");
189 h->GetXaxis()->SetBinLabel(i++,
"TPC Wavelet");
190 h->GetXaxis()->SetBinLabel(i++,
"TPC DataSize");
192 h->GetXaxis()->LabelsOption(
"v");
205 new TH1D(
"hDataSize",
206 "TPC Data Size per Event;Data size [Byte];Count",
211 "TPC Recorded Wavelet per Event;Wavelet count;Count",
216 "Charged particle #eta distribution;#eta;Count",
220 new TH2D(
"hLayerWaveletSize",
221 "Number of Recorded ADC sample per Wavelet;Layer ID;ADC Sample Count per Wavelet",
223 nZBins, -.5, nZBins - .5));
226 new TH2D(
"hLayerHit",
227 "Number of Recorded ADC sample per channel;Layer ID;ADC Sample Count",
229 nZBins, -.5, nZBins - .5));
232 new TH2D(
"hLayerDataSize",
233 "Data size per channel;Layer ID;Data size [Byte]",
235 2 * nZBins, 0, 2 * nZBins));
238 new TH2D(
"hLayerSumHit",
239 "Number of Recorded ADC sample per layer;Layer ID;ADC Sample Count",
241 10000, -.5, 99999.5));
244 new TH2D(
"hLayerSumDataSize",
245 "Data size per trigger per layer;Layer ID;Data size [Byte]",
250 new TH2D(
"hLayerZBinHit",
251 "Number of Recorded ADC sample per Time Bin;z bin ID;Layer ID",
252 nZBins, -.5, nZBins - .5,
256 new TH2D(
"hLayerZBinADC",
257 "Sum ADC per Time Bin;z bin ID;Layer ID",
258 nZBins, -.5, nZBins - .5,
279 TH1D* h_norm =
dynamic_cast<TH1D*
>(hm->
getHisto(
"hNormalization"));
281 h_norm->Fill(
"Event count", 1);
304 PHG4HitContainer* g4hit = findNode::getClass<PHG4HitContainer>(topNode,
"G4HIT_TPC");
305 cout <<
"TPCMLDataInterface::process_event - g4 hit node G4HIT_TPC" << endl;
308 cout <<
"TPCMLDataInterface::process_event - Could not locate g4 hit node G4HIT_TPC" << endl;
313 TrkrHitSetContainer* hits = findNode::getClass<TrkrHitSetContainer>(topNode,
"TRKR_HITSET");
316 cout <<
PHWHERE <<
"ERROR: Can't find node TRKR_HITSET" << endl;
331 cout <<
"TPCMLDataInterface::process_event - could not locate geo node "
332 <<
"CYLINDERCELLGEOM_SVTX" << endl;
336 PHHepMCGenEventMap* geneventmap = findNode::getClass<PHHepMCGenEventMap>(topNode,
"PHHepMCGenEventMap");
339 static bool once =
true;
344 cout <<
"TPCMLDataInterface::process_event - - missing node PHHepMCGenEventMap. Skipping HepMC stat." << std::endl;
349 h_norm->Fill(
"Collision count", geneventmap->
size());
356 cout <<
"TPCMLDataInterface::process_event - Fatal Error - "
357 <<
"unable to find DST node "
358 <<
"G4TruthInfo" << endl;
367 particle_iter != primary_range.second;
373 TParticlePDG* pdg_p = TDatabasePDG::Instance()->GetParticle(
377 if (fabs(pdg_p->Charge()) > 0)
381 if (pvec.Perp2() > 0)
396 const double edep = hiter->second->get_edep();
397 h_norm->Fill(
"TPC G4Hit Edep", edep);
398 h_norm->Fill(
"TPC G4Hit", 1);
405 unique_ptr<Group> h5Group(
new Group(
m_h5File->createGroup(
"/" + h5GroupName)));
408 cout <<
"TPCMLDataInterface::process_event - save to H5 group " << h5GroupName << endl;
409 map<int, shared_ptr<DataSet>> layerH5DataSetMap;
410 map<int, shared_ptr<DataSet>> layerH5SignalBackgroundMap;
411 map<int, shared_ptr<DataSpace>> layerH5DataSpaceMap;
412 map<int, shared_ptr<DataSpace>> layerH5SignalBackgroundDataSpaceMap;
415 vector<uint32_t> layerWaveletDataSize(
m_maxLayer + 1, 0);
416 vector<hsize_t> layerSize(1);
418 shared_ptr<DataSpace> H5DataSpaceLayerWaveletDataSize(
new DataSpace(1, layerSize.data()));
419 shared_ptr<DataSet> H5DataSetLayerWaveletDataSize(
new DataSet(h5Group->createDataSet(
420 "sPHENIXRawDataSizeBytePerLayer",
421 PredType::NATIVE_UINT32,
422 *(H5DataSpaceLayerWaveletDataSize))));
426 vector<array<vector<int>, 2>> layerChanHit(
m_maxLayer + 1);
427 vector<array<vector<int>, 2>> layerChanDataSize(
m_maxLayer + 1);
428 vector<vector<uint16_t>> layerDataBuffer(
m_maxLayer + 1);
429 vector<vector<uint8_t>> layerSignalBackgroundBuffer(
m_maxLayer + 1);
430 vector<vector<hsize_t>> layerDataBufferSize(
m_maxLayer + 1, vector<hsize_t>({0, 0}));
431 vector<vector<hsize_t>> layerSignalBackgroundDataBufferSize(
m_maxLayer + 1, vector<hsize_t>({0, 0}));
447 cout <<
"TPCMLDataInterface::process_event - init layer " <<
layer <<
" with "
448 <<
"nphibins = " << nphibins
449 <<
", layerGeom->get_zbins() = " << layerGeom->
get_zbins() << endl;
459 if ((
int) nZBins != layerGeom->
get_zbins())
461 cout <<
"TPCMLDataInterface::process_event - Fatal Error - nZBin at layer " <<
layer <<
" is " << layerGeom->
get_zbins()
462 <<
", which is different from previous layers of nZBin = " << nZBins << endl;
467 for (
unsigned int side = 0; side < 2; ++side)
469 layerChanHit[
layer][side].resize(nphibins, 0);
471 layerChanDataSize[
layer][side].resize(nphibins, 0);
475 layerDataBufferSize[
layer][0] = nphibins;
477 layerSignalBackgroundDataBufferSize[
layer][0] = nphibins;
478 layerSignalBackgroundDataBufferSize[
layer][1] = layerGeom->
get_zbins();
479 layerDataBuffer[
layer].resize(layerDataBufferSize[
layer][0] * layerDataBufferSize[
layer][1], 0);
480 layerSignalBackgroundBuffer[
layer].resize(layerSignalBackgroundDataBufferSize[layer][0] * layerSignalBackgroundDataBufferSize[layer][1], 0);
482 static const vector<hsize_t> cdims({32, 32});
483 DSetCreatPropList ds_creatplist;
484 ds_creatplist.setChunk(2, cdims.data());
485 ds_creatplist.setDeflate(6);
487 layerH5DataSpaceMap[
layer] = shared_ptr<DataSpace>(
new DataSpace(2, layerDataBufferSize[layer].
data()));
488 layerH5SignalBackgroundDataSpaceMap[
layer] = shared_ptr<DataSpace>(
new DataSpace(2, layerSignalBackgroundDataBufferSize[layer].
data()));
490 layerH5DataSetMap[
layer] = shared_ptr<DataSet>(
new DataSet(h5Group->createDataSet(
492 PredType::NATIVE_UINT16,
493 *(layerH5DataSpaceMap[layer]),
496 layerH5SignalBackgroundMap[
layer] = shared_ptr<DataSet>(
new DataSet(h5Group->createDataSet(
498 PredType::NATIVE_UINT8,
499 *(layerH5SignalBackgroundDataSpaceMap[layer]),
509 int last_phibin = -1;
511 vector<unsigned int> last_wavelet;
512 int last_wavelet_hittime = -1;
520 hitsetitr != hitsetrange.second;
533 cout <<
"TPCMLDataInterface::process_event - TrkrHitSet @ layer " << layer <<
" with " << hitset->
size() <<
" hits" << endl;
536 if (layer < m_minLayer or layer >
m_maxLayer)
continue;
542 hitr != hitrangei.second;
549 const int side = (zbin < nZBins / 2) ? 0 : 1;
553 cout <<
"TPCMLDataInterface::process_event - hit @ layer " << layer <<
" phibin = " << phibin <<
" zbin = " << zbin <<
" side = " << side << endl;
561 if (last_layer != layer or last_phibin != phibin or last_side != side or abs(last_zbin - zbin) != 1)
564 if (last_wavelet.size() > 0)
566 const int datasize =
writeWavelet(last_layer, last_side, last_phibin, last_wavelet_hittime, last_wavelet);
570 sumDataSize += datasize;
571 layerChanDataSize[last_layer][last_side][last_phibin] += datasize;
573 last_wavelet.clear();
582 const double z_abs = fabs(layerGeom->
get_zcenter(zbin));
585 const double eta = acceptanceVec.PseudoRapidity();
592 last_phibin = phibin;
595 last_wavelet_hittime = (side == 0) ? (zbin) : (nZBins - 1 - zbin);
596 assert(last_wavelet_hittime >= 0);
597 assert(last_wavelet_hittime <= nZBins / 2);
603 cout <<
"TPCMLDataInterface::process_event - layer " << layer <<
" hit with "
605 <<
"phibin = " << phibin
606 <<
",zbin = " << zbin
607 <<
",side = " << side
608 <<
",last_wavelet.size() = " << last_wavelet.size()
609 <<
",last_zbin = " << last_zbin
614 if (last_wavelet.size() > 0)
618 assert(zbin - last_zbin == 1);
627 unsigned int adc = hitr->second->getAdc();
628 last_wavelet.push_back(adc);
632 layerChanHit[
layer][side][phibin] += 1;
638 assert((hsize_t) phibin < layerDataBufferSize[layer][0]);
639 assert((hsize_t) zbin < layerDataBufferSize[layer][1]);
640 const size_t hitindex(layerDataBufferSize[layer][1] * phibin + zbin);
641 assert((hsize_t) phibin < layerSignalBackgroundDataBufferSize[layer][0]);
642 assert((hsize_t) zbin < layerSignalBackgroundDataBufferSize[layer][1]);
643 const size_t hitindexSB(layerSignalBackgroundDataBufferSize[layer][1] * phibin + zbin);
644 assert(hitindex < layerDataBuffer[layer].
size());
645 assert(hitindexSB < layerSignalBackgroundBuffer[layer].
size());
647 if (layerDataBuffer[layer][hitindex] != 0)
649 cout <<
"TPCMLDataInterface::process_event - WARNING - hit @ layer "
650 << layer <<
" phibin = " << phibin <<
" zbin = " << zbin <<
" side = " << side
651 <<
" overwriting previous hit with ADC = " << layerDataBuffer[
layer][hitindex]
654 layerDataBuffer[
layer][hitindex] = adc;
656 if (layerSignalBackgroundBuffer[layer][hitindexSB] != 0)
658 cout <<
"TPCMLDataInterface::process_event - WARNING - signal/background @ layer "
659 << layer <<
" phibin = " << phibin <<
" zbin = " << zbin <<
" side = " << side
660 <<
" overwriting previous hit with = " << layerSignalBackgroundBuffer[
layer][hitindexSB]
666 bool signal_or_bgd =
false;
672 for (
PHG4Hit* g4hit : g4hit_set)
676 if (g4particle !=
nullptr)
678 gpx = g4particle->
get_px();
679 gpy = g4particle->
get_py();
680 gpz = g4particle->
get_pz();
683 if (sqrt(gpx * gpx + gpy * gpy + gpz * gpz) >
m_momentumCut) signal_or_bgd =
true;
687 layerSignalBackgroundBuffer[
layer][hitindexSB] = signal_or_bgd;
694 if (last_wavelet.size() > 0)
696 const int datasize =
writeWavelet(last_layer, last_side, last_phibin, last_wavelet_hittime, last_wavelet);
700 sumDataSize += datasize;
701 layerChanDataSize[last_layer][last_side][last_phibin] += datasize;
707 for (
unsigned int side = 0; side < 2; ++side)
710 for (
const int& hit : layerChanHit[
layer][side])
716 h_norm->Fill(
"TPC Hit", hit);
720 cout <<
"TPCMLDataInterface::process_event - layerChanHit: "
722 <<
"at layer = " <<
layer
723 <<
", side = " << side
730 cout <<
"TPCMLDataInterface::process_event - hLayerSumCellHit->Fill(" <<
layer <<
", " << sumHit <<
")" << endl;
736 for (
const int&
data : layerChanDataSize[
layer][side])
752 assert(layerH5SignalBackgroundMap[layer]);
753 layerH5DataSetMap[
layer]->write(layerDataBuffer[layer].
data(), PredType::NATIVE_UINT16);
754 layerH5SignalBackgroundMap[
layer]->write(layerSignalBackgroundBuffer[layer].
data(), PredType::NATIVE_UINT8);
755 H5DataSetLayerWaveletDataSize->write(layerWaveletDataSize.data(), PredType::NATIVE_UINT32);
761 h_norm->Fill(
"TPC Wavelet", nWavelet);
764 h_norm->Fill(
"TPC DataSize", sumDataSize);
771 static const int headersize = 2;
774 const int datasizebit = wavelet.size() * 10;
775 int datasizebyte = datasizebit / 8;
776 if (datasizebyte * 8 < datasizebit) datasizebyte += 1;
781 return headersize + datasizebyte;
787 static string histname(
"TPCMLDataInterface_HISTOS");
795 <<
"TPCMLDataInterface::get_HistoManager - Making Fun4AllHistoManager " << histname