File indexing completed on 2025-12-16 09:28:19
0001 #include "HGCROC_Convert.h"
0002
0003 #include "Analyses.h"
0004 #include "Event.h"
0005 #include "Tile.h"
0006 #include "HGCROC.h"
0007 #include "CommonHelperFunctions.h"
0008 #include "Setup.h"
0009 #include "TileSpectra.h"
0010 #include "hgc_decoder.h"
0011
0012
0013 int run_hgcroc_conversion(Analyses *analysis, waveform_fit_base *waveform_builder) {
0014 #ifdef DECODE_HGCROC
0015 std::cout << "Setting up event parameters for HGCROC data" << std::endl;
0016
0017 if (waveform_builder == nullptr) {
0018 std::cout << "No waveform builder specified" << std::endl;
0019 return 1;
0020 }
0021
0022
0023 if (analysis->MapInputName.IsNull()) {
0024 std::cout << "No mapping file specified" << std::endl;
0025
0026 }
0027 analysis->setup->Initialize(analysis->MapInputName.Data(), analysis->debug);
0028
0029
0030 if (analysis->RunListInputName.IsNull()) {
0031 std::cout << "No run list file specified" << std::endl;
0032
0033 }
0034 std::map<int, RunInfo> ri = readRunInfosFromFile(analysis->RunListInputName.Data(), analysis->debug, 0);
0035
0036
0037 if (analysis->ASCIIinputName.IsNull()) {
0038 std::cout << "No input file specified" << std::endl;
0039
0040 }
0041
0042
0043 std::map<int,TileSpectra> hSpectra;
0044 std::map<int, TileSpectra>::iterator ithSpectra;
0045
0046
0047
0048
0049
0050 TObjArray* tok = analysis->ASCIIinputName.Tokenize("/");
0051
0052 TString file = ((TObjString*)tok->At(tok->GetEntries() - 1))->String();
0053 delete tok;
0054 tok=file.Tokenize("_");
0055 TString header = ((TObjString*)tok->At(0))->String();
0056 delete tok;
0057
0058
0059 TString RunString=header(3,header.Sizeof());
0060 std::map<int,RunInfo>::iterator it=ri.find(RunString.Atoi());
0061 std::cout<<RunString.Atoi()<<"\t"<<it->second.species<<std::endl;
0062 analysis->event.SetRunNumber(RunString.Atoi());
0063 analysis->event.SetBeamName(it->second.species);
0064 analysis->event.SetBeamID(it->second.pdg);
0065 analysis->event.SetBeamEnergy(it->second.energy);
0066 analysis->event.SetVop(it->second.vop);
0067 analysis->event.SetVov(it->second.vop-it->second.vbr);
0068 analysis->event.SetBeamPosX(it->second.posX);
0069 analysis->event.SetBeamPosY(it->second.posY);
0070 analysis->calib.SetRunNumber(RunString.Atoi());
0071 analysis->calib.SetVop(it->second.vop);
0072 analysis->calib.SetVov(it->second.vop-it->second.vbr);
0073 analysis->calib.SetBCCalib(false);
0074 analysis->event.SetROtype(ReadOut::Type::Hgcroc);
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087 int event_number = 0;
0088 if (analysis->maxEvents != -1){
0089 std::cout << "Event conversion will be stopped at " << analysis->maxEvents << std::endl;
0090 }
0091
0092 auto decoder = new hgc_decoder( (char*)analysis->ASCIIinputName.Data(),
0093 1,
0094 it->second.nFPGA,
0095 it->second.nASIC,
0096 3,
0097 analysis->GetHGCROCTrunctation());
0098 for (auto ae : *decoder) {
0099 if (analysis->maxEvents != -1 && event_number > analysis->maxEvents ){
0100 break;
0101 }
0102
0103 analysis->event.SetEventID(event_number);
0104 event_number++;
0105 if (event_number % 500 == 0){
0106 std::cout << "\nEvent: " << event_number << "\t"<< decoder->get_num_proc_events() << std::endl;
0107 for (int i = 0; i < (int)it->second.nFPGA; i++) {
0108 std::cout << "\t KCU: " << i << "\t att:" << decoder->get_attempted_waveforms(i) << "\t rec:" << decoder->get_completed_waveforms(i) << "\t in progress: " << decoder->get_inprogress_waveforms(i) << "\t aborted: " << decoder->get_discarded_waveforms(i) << std::endl;
0109 }
0110 }
0111
0112
0113 for (int i = 0; i < ae->get_num_fpga(); i++) {
0114
0115 auto single_kcu = ae->get_event(i);
0116
0117 for (int j = 0; j < ae->get_channels_per_fpga(); j++) {
0118
0119 int channel_number = i * ae->get_channels_per_fpga() + j;
0120
0121 int asic = i * it->second.nASIC + (j / 72);
0122
0123 auto cell_id = analysis->setup->GetCellID(asic, j % 72);
0124 if (analysis->debug > 0 && event_number == 1) {
0125 std::cout << Form("KCU: %d, asic: %d , channel %d, %s", i, int(j / 72), j % 72, (analysis->setup->DecodeCellID(cell_id)).Data()) << std::endl;
0126 }
0127
0128 if (cell_id != -1) {
0129
0130 int asic = i * it->second.nASIC + (j / 72);
0131
0132 auto cell_id = analysis->setup->GetCellID(asic, j % 72);
0133 if (analysis->debug > 0 && event_number == 1) {
0134 std::cout << Form("KCU: %d, asic: %d , channel %d, %s", i, int(j / 72), j % 72, (analysis->setup->DecodeCellID(cell_id)).Data()) << std::endl;
0135 }
0136
0137 if (cell_id != -1) {
0138 Hgcroc *tile = new Hgcroc();
0139 tile->SetCellID(cell_id);
0140 tile->SetCellID(cell_id);
0141 tile->SetROtype(ReadOut::Type::Hgcroc);
0142 tile->SetLocalTriggerBit(0);
0143 tile->SetLocalTriggerPrimitive(0);
0144 tile->SetE(0);
0145 tile->SetTOA(0);
0146 tile->SetTOT(0);
0147
0148 tile->SetNsample(single_kcu->get_n_samples());
0149 for (int sample = 0; sample < single_kcu->get_n_samples(); sample++) {
0150
0151
0152
0153
0154
0155
0156
0157
0158 tile->AppendWaveformADC(single_kcu->get_sample_adc(j, sample));
0159 tile->AppendWaveformTOA(single_kcu->get_sample_toa(j, sample));
0160 tile->AppendWaveformTOT(single_kcu->get_sample_tot(j, sample));
0161 if (single_kcu->get_sample_toa(j, sample) > 0) {
0162 tile->SetTOA(single_kcu->get_sample_toa(j, sample));
0163 }
0164 if (single_kcu->get_sample_tot(j, sample) > 0) {
0165 tile->SetTOT(single_kcu->get_sample_tot(j, sample));
0166 }
0167 }
0168
0169
0170 waveform_builder->set_waveform(tile->GetADCWaveform());
0171 waveform_builder->fit();
0172 tile->SetIntegratedADC(waveform_builder->get_E());
0173 if (tile->GetTOT() != 1)
0174 tile->SetIntegratedTOT(tile->GetTOT());
0175 else
0176 tile->SetIntegratedTOT(0.);
0177 if (waveform_builder->is_saturated()) {
0178 tile->SetIntegratedValue(tile->GetIntegratedTOT());
0179 } else {
0180 tile->SetIntegratedValue(tile->GetIntegratedADC());
0181 }
0182 tile->SetIntegratedADC(waveform_builder->get_E());
0183 if (tile->GetTOT() != 1)
0184 tile->SetIntegratedTOT(tile->GetTOT());
0185 else
0186 tile->SetIntegratedTOT(0.);
0187 if (waveform_builder->is_saturated()) {
0188 tile->SetIntegratedValue(tile->GetIntegratedTOT());
0189 } else {
0190 tile->SetIntegratedValue(tile->GetIntegratedADC());
0191 }
0192 tile->SetPedestal(waveform_builder->get_pedestal());
0193
0194 analysis->event.AddTile(tile);
0195 }
0196 }
0197
0198 }
0199 }
0200 analysis->TdataOut->Fill();
0201 analysis->event.ClearTiles();
0202 }
0203
0204 std::cout << "\nFinished converting events\n" << std::endl;
0205 std::cout << "\nTotal Events: " << decoder->get_num_proc_events() << std::endl;
0206 for (int i = 0; i < (int)it->second.nFPGA; i++) {
0207 std::cout << "\t KCU: " << i << "\t att:" << decoder->get_attempted_waveforms(i) << "\t rec:" << decoder->get_completed_waveforms(i) << "\t in progress: " << decoder->get_inprogress_waveforms(i) << std::endl;
0208 }
0209 analysis->RootOutput->cd();
0210
0211
0212 RootSetupWrapper rswtmp=RootSetupWrapper(analysis->setup);
0213 analysis->rsw=rswtmp;
0214 analysis->TsetupOut->Fill();
0215
0216 analysis->TcalibOut->Fill();
0217 analysis->TcalibOut->Write();
0218
0219 analysis->TdataOut->Fill();
0220 analysis->TsetupOut->Write();
0221 analysis->TdataOut->Write();
0222
0223 TH1D* hEventsKCU[it->second.nFPGA];
0224 long maxAttempted = 0;
0225 for (Int_t i = 0; i < (int)it->second.nFPGA; i++){
0226 hEventsKCU[i] = new TH1D( Form("hNEventsKCU%i", i),"event KCU category; events",4,-0.5,3.5);
0227 hEventsKCU[i]->SetBinContent(1, decoder->get_attempted_waveforms(i));
0228 hEventsKCU[i]->SetBinContent(2, decoder->get_completed_waveforms(i));
0229 hEventsKCU[i]->SetBinContent(3, decoder->get_discarded_waveforms(i));
0230 hEventsKCU[i]->SetBinContent(4, decoder->get_inprogress_waveforms(i));
0231 hEventsKCU[i]->Write();
0232 if (maxAttempted < decoder->get_attempted_waveforms(i))
0233 maxAttempted = decoder->get_attempted_waveforms(i);
0234 }
0235
0236 TH1D* hEvents = new TH1D( "hNEvents","event category; events",2,-0.5,1.5);
0237 hEvents->SetBinContent(1, maxAttempted);
0238 hEvents->SetBinContent(2, decoder->get_num_proc_events());
0239 hEvents->Write();
0240
0241
0242 analysis->RootOutput->Close();
0243 return true;
0244
0245 #else
0246 std::cout << "This code is not built for HGCROC decoding" << std::endl;
0247 analysis->RootOutput->cd();
0248
0249 RootSetupWrapper rswtmp=RootSetupWrapper(analysis->setup);
0250 analysis->rsw=rswtmp;
0251 analysis->TsetupOut->Fill();
0252
0253 analysis->TcalibOut->Fill();
0254 analysis->TcalibOut->Write();
0255
0256 analysis->TdataOut->Fill();
0257 analysis->TsetupOut->Write();
0258 analysis->TdataOut->Write();
0259
0260 analysis->RootOutput->Close();
0261 return false;
0262
0263 #endif
0264 }