Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MbdEvent.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file MbdEvent.cc
1 #include "MbdEvent.h"
2 #include "MbdCalib.h"
3 #include "MbdGeomV1.h"
4 #include "MbdOut.h"
5 #include "MbdPmtContainer.h"
6 #include "MbdPmtHit.h"
7 
9 
10 #include <phool/phool.h>
11 #include <phool/recoConsts.h>
12 
13 #include <sphenixnpc/CDBUtils.h>
14 
15 #include <Event/Event.h>
16 #include <Event/EventTypes.h>
17 
18 #include <TCanvas.h>
19 #include <TF1.h>
20 #include <TH1.h>
21 #include <TH2.h>
22 
23 #include <cmath>
24 #include <iomanip>
25 #include <iostream>
26 
28  : _tres(0.05)
29 {
30  // set default values
31 
32  int nsamples = 31;
34  if (rc->FlagExist("MBD_TEMPLATEFIT"))
35  {
36  do_templatefit = rc->get_IntFlag("MBD_TEMPLATEFIT");
37  }
38  else
39  {
40  do_templatefit = 1;
41  }
42 
43  for (int ifeech = 0; ifeech < MbdDefs::BBC_N_FEECH; ifeech++)
44  {
45  // std::cout << PHWHERE << "Creating _mbdsig " << ifeech << std::endl;
46  _mbdsig.emplace_back(ifeech, nsamples);
47 
48  // Do evt-by-evt pedestal using sample range below
49  //_mbdsig[ifeech].SetEventPed0Range(0,1);
50  _mbdsig[ifeech].SetEventPed0PreSamp(6, 2);
51  }
52 
55  for (int iarm = 0; iarm < 2; iarm++)
56  {
57  //
58  name = "hevt_bbct";
59  name += std::to_string(iarm);
60  title = "bbc times, arm ";
61  title += std::to_string(iarm);
62  hevt_bbct[iarm] = new TH1F(name.c_str(), title.c_str(), 2000, -50., 50.);
63  hevt_bbct[iarm]->SetLineColor(4);
64  }
65  h2_tmax[0] = new TH2F("h2_ttmax", "time tmax vs ch", MbdDefs::MAX_SAMPLES, -0.5, MbdDefs::MAX_SAMPLES - 0.5, 128, 0, 128);
66  h2_tmax[0]->SetXTitle("sample");
67  h2_tmax[0]->SetYTitle("ch");
68  h2_tmax[1] = new TH2F("h2_qtmax", "chg tmax vs ch", MbdDefs::MAX_SAMPLES, -0.5, MbdDefs::MAX_SAMPLES - 0.5, 128, 0, 128);
69  h2_tmax[1]->SetXTitle("sample");
70  h2_tmax[1]->SetYTitle("ch");
71 
72  for (float &iboard : TRIG_SAMP)
73  {
74  iboard = -1;
75  }
76 
77  // BBCCALIB is used in offline to read in our calibrations
78  const char *bbccaldir = getenv("BBCCALIB");
79  if (bbccaldir)
80  {
81  // Online calibrations
82  std::string gainfile = std::string(bbccaldir) + "/" + "bbc_mip.calib";
83  Read_Charge_Calib(gainfile);
84 
85  std::string tq_t0_offsetfile = std::string(bbccaldir) + "/" + "bbc_tq_t0.calib";
86  Read_TQ_T0_Offsets(tq_t0_offsetfile);
87 
88  std::string tq_clk_offsetfile = std::string(bbccaldir) + "/" + "bbc_tq_clk.calib";
89  Read_TQ_CLK_Offsets(tq_clk_offsetfile);
90 
91  std::string tt_clk_offsetfile = std::string(bbccaldir) + "/" + "bbc_tt_clk.calib";
92  Read_TT_CLK_Offsets(tt_clk_offsetfile);
93 
94  /*
95  std::string mondata_fname = std::string(bbccaldir) + "/" + "BbcMonData.dat";
96  std::ifstream mondatafile( mondata_fname );
97  string label;
98  mondatafile >> label >> bz_offset;
99  std::cout << PHWHERE << label << "\t" << bz_offset << std::endl;
100  mondatafile.close();
101  */
102  }
103 
104  // Debug stuff
105  // debugintt = 1;
106  if (debugintt)
107  {
108  ReadSyncFile();
109  }
110 
111  Clear();
112 }
113 
116 {
117  for (auto &iarm : hevt_bbct)
118  {
119  delete iarm;
120  }
121 
122  delete h2_tmax[0];
123  delete h2_tmax[1];
124  delete ac;
125  delete gausfit[0];
126  delete gausfit[1];
127  delete _mbdgeom;
128  delete _mbdcal;
129  delete _syncttree;
130 }
131 
133 {
134  h2_tmax[0]->Reset();
135  h2_tmax[1]->Reset();
136 
137  Clear();
138 
140  _runnum = rc->get_IntFlag("RUNNUMBER");
141  if (_verbose)
142  {
143  std::cout << PHWHERE << "RUNNUMBER " << _runnum << std::endl;
144  }
145 
146  if (_mbdgeom == nullptr)
147  {
148  _mbdgeom = new MbdGeomV1();
149  }
150 
151  // Always reload calibrations on InitRun()
152  if (_mbdcal != nullptr)
153  {
154  delete _mbdcal;
155  }
156  _mbdcal = new MbdCalib();
157  std::cout << "SIMFLAG IS " << _simflag << std::endl;
158  if (!_simflag)
159  {
161  }
162 
163  // Read in template if specified
164  if (do_templatefit)
165  {
166  for (int ifeech = 0; ifeech < MbdDefs::BBC_N_FEECH; ifeech++)
167  {
168  if (_mbdgeom->get_type(ifeech) == 0)
169  {
170  continue;
171  }
172  // std::cout << PHWHERE << "Reading template " << ifeech << std::endl;
173  // std::cout << "SIZES0 " << _mbdcal->get_shape(ifeech).size() << std::endl;
174  // Should set template size automatically here
175  _mbdsig[ifeech].SetTemplate(_mbdcal->get_shape(ifeech), _mbdcal->get_sherr(ifeech));
176  _mbdsig[ifeech].SetMinMaxFitTime(_mbdcal->get_sampmax(ifeech) - 2 - 3, _mbdcal->get_sampmax(ifeech) - 2 + 3);
177  //_mbdsig[ifeech].SetMinMaxFitTime( 0, 31 );
178  }
179  }
180 
181  return 0;
182 }
183 
186 {
187  // Reset BBC/MBD raw data
188  std::fill_n(m_pmttt, 128, 1e12);
189  std::fill_n(m_pmttq, 128, 1e12);
190  std::fill_n(m_pmtq, 128, 0.);
191 
192  // Reset BBC/MBD Arm Data
193  for (int iarm = 0; iarm < 2; iarm++)
194  {
195  m_bbcn[iarm] = 0;
196  m_bbcq[iarm] = 0.;
197  m_bbct[iarm] = -9999.;
198  m_bbcte[iarm] = -9999.;
199  m_bbctl[iarm] = -9999.;
200  hevt_bbct[iarm]->Reset();
201  hevt_bbct[iarm]->GetXaxis()->SetRangeUser(-50, 50);
202  }
203 
204  // Reset end product to prepare next event
205  m_bbcz = std::numeric_limits<Float_t>::quiet_NaN();
206  m_bbczerr = std::numeric_limits<Float_t>::quiet_NaN();
207  m_bbct0 = std::numeric_limits<Float_t>::quiet_NaN();
208  m_bbct0err = std::numeric_limits<Float_t>::quiet_NaN();
209 }
210 
212 {
213  // First check if there is any event (ie, reading from PRDF)
214  if (event == nullptr || bbcpmts == nullptr)
215  {
217  }
218 
219  int evt_type = event->getEvtType();
220  if (evt_type != DATAEVENT)
221  {
222  std::cout << PHWHERE << "MbdEvent: Event type is not DATAEVENT, skipping" << std::endl;
224  }
225 
226  m_evt = event->getEvtSequence();
227  UShort_t xmitclocks[2]; // [ipkt]
228  UShort_t femclocks[2][2]; // [ipkt][iadc]
229 
230  // Get the relevant packets from the Event object and transfer the
231  // data to the subsystem-specific table.
232 
233  // int flag_err = 0;
234  for (int ipkt = 0; ipkt < 2; ipkt++)
235  {
236  int pktid = 1001 + ipkt; // packet id
237  p[ipkt] = event->getPacket(pktid);
238 
239  if (Verbosity() > 0)
240  {
241  static int counter = 0;
242  if (counter < 4)
243  {
244  std::cout << "Found packet " << pktid << "\t" << p[ipkt] << std::endl;
245  counter++;
246  }
247  }
248  if (p[ipkt])
249  {
250  xmitclocks[ipkt] = static_cast<UShort_t>(p[ipkt]->iValue(0, "CLOCK"));
251 
252  femclocks[ipkt][0] = static_cast<UShort_t>(p[ipkt]->iValue(0, "FEMCLOCK"));
253  femclocks[ipkt][1] = static_cast<UShort_t>(p[ipkt]->iValue(1, "FEMCLOCK"));
254 
255  for (int ich = 0; ich < NCHPERPKT; ich++)
256  {
257  int feech = ipkt * NCHPERPKT + ich;
258  // std::cout << "feech " << feech << std::endl;
259  for (int isamp = 0; isamp < MbdDefs::MAX_SAMPLES; isamp++)
260  {
261  m_adc[feech][isamp] = p[ipkt]->iValue(isamp, ich);
262  m_samp[feech][isamp] = isamp;
263 
264  /*
265  if ( m_adc[feech][isamp] <= 100 )
266  {
267  //flag_err = 1;
268  //std::cout << "BAD " << m_evt << "\t" << feech << "\t" << m_samp[feech][isamp]
269  // << "\t" << m_adc[feech][isamp] << std::endl;
270  }
271  */
272  }
273 
274  _mbdsig[feech].SetXY(m_samp[feech], m_adc[feech]);
275  }
276 
277  delete p[ipkt];
278  p[ipkt] = nullptr;
279  }
280  else
281  {
282  // flag_err = 1;
283  std::cout << PHWHERE << " ERROR, evt " << m_evt << " Missing Packet " << pktid << std::endl;
285  }
286  }
287 
288  // Do a quick sanity check that all fem counters agree
289  if (xmitclocks[0] != xmitclocks[1])
290  {
291  std::cout << __FILE__ << ":" << __LINE__ << " ERROR, xmitclocks don't agree" << std::endl;
292  }
293  for (auto &femclock : femclocks)
294  {
295  for (unsigned short iadc : femclock)
296  {
297  if (iadc != femclocks[0][0])
298  {
299  std::cout << __FILE__ << ":" << __LINE__ << " ERROR, femclocks don't agree" << std::endl;
300  }
301  }
302  }
303 
304  // Store the clock info. We use just the first one, and assume all are consistent.
305  m_clk = xmitclocks[0];
306  m_femclk = femclocks[0][0];
307 
308  for (int ifeech = 0; ifeech < MbdDefs::BBC_N_FEECH; ifeech++)
309  {
310  int pmtch = _mbdgeom->get_pmt(ifeech);
311  int type = _mbdgeom->get_type(ifeech); // 0 = T-channel, 1 = Q-channel
312 
313  if (type == 0)
314  {
315  Double_t tdc = _mbdsig[ifeech].MBD(_mbdcal->get_sampmax(ifeech));
316 
317  if (tdc < 40)
318  {
319  m_pmttt[pmtch] = NAN; // no hit
320  }
321  else
322  {
323  m_pmttt[pmtch] = 26.5 - tdc * 0.00189; // simple linear correction
324  }
325  }
326 
327  if (type == 1)
328  {
329  // Use dCFD method to get time for now in charge channels
330  // std::cout << "getspline " << ifeech << std::endl;
331  _mbdsig[ifeech].GetSplineAmpl();
332  Double_t threshold = 0.5;
333  m_pmttq[pmtch] = _mbdsig[ifeech].dCFD(threshold);
334  m_ampl[ifeech] = _mbdsig[ifeech].GetAmpl();
335  if (do_templatefit)
336  {
337  _mbdsig[ifeech].FitTemplate();
338 
339  // m_pmttq[pmtch] = _mbdsig[ifeech].GetTime();
340  m_ampl[ifeech] = _mbdsig[ifeech].GetAmpl();
341  }
342 
343  if (m_ampl[ifeech] < _mbdcal->get_qgain(pmtch) * 0.25)
344  {
345  // m_t0[ifeech] = -9999.;
346  m_pmttq[pmtch] = std::numeric_limits<Float_t>::quiet_NaN();
347  }
348  else
349  {
350  // if ( m_pmttq[pmtch]<-50. && ifeech==255 ) std::cout << "hit_times " << ifeech << "\t" << m_pmttq[pmtch] << std::endl;
351  // if ( arm==1 ) std::cout << "hit_times " << ifeech << "\t" << setw(10) << m_pmttq[pmtch] << "\t" << board << "\t" << TRIG_SAMP[board] << std::endl;
352  m_pmttq[pmtch] -= (_mbdcal->get_sampmax(ifeech) - 2);
353  m_pmttq[pmtch] *= 17.7623; // convert from sample to ns (1 sample = 1/56.299 MHz)
354  m_pmttq[pmtch] = m_pmttq[pmtch] - _mbdcal->get_tq0(pmtch);
355  }
356 
357  m_pmtq[pmtch] = m_ampl[ifeech] / _mbdcal->get_qgain(pmtch);
358 
359  if (m_pmtq[pmtch] < 0.25)
360  {
361  m_pmtq[pmtch] = 0.;
362  m_pmttq[pmtch] = std::numeric_limits<Float_t>::quiet_NaN();
363  }
364 
365  /*
366  if ( m_evt<3 && ifeech==255 && m_ampl[ifeech] )
367  {
368  std::cout << "dcfdcalc " << m_evt << "\t" << ifeech << "\t" << m_pmttq[pmtch] << "\t" << m_ampl[ifeech] << std::endl;
369  }
370  */
371  }
372  }
373 
374  // bbcpmts->Reset();
375  // std::cout << "q10 " << bbcpmts->get_tower_at_channel(10)->get_q() << std::endl;
376 
377  // Copy to output
378  for (int ipmt = 0; ipmt < MbdDefs::BBC_N_PMT; ipmt++)
379  {
380  bbcpmts->get_pmt(ipmt)->set_pmt(ipmt, m_pmtq[ipmt], m_pmttt[ipmt], m_pmttq[ipmt]);
381  }
382  bbcpmts->set_npmt(MbdDefs::BBC_N_PMT);
383 
384  m_evt++;
385  return m_evt;
386 }
387 
390 {
391  if (debugintt)
392  {
393  _verbose = 100;
394  }
395  //_verbose = 100;
396  if (_verbose >= 10)
397  {
398  std::cout << "In MbdEvent::Calculate() " << m_evt << std::endl;
399  }
400  Clear();
401  if (bbcout != nullptr)
402  {
403  bbcout->Reset();
404  }
405 
406  // Debug stuff
407  if (debugintt && (bbevt[_syncevt] != (m_evt - 1)))
408  {
409  _verbose = 0;
410  return 1;
411  }
412 
413  if (gausfit[0] == nullptr)
414  {
415  TString name;
416  for (int iarm = 0; iarm < 2; iarm++)
417  {
418  name = "gausfit";
419  name += iarm;
420  gausfit[iarm] = new TF1(name, "gaus", 0, 20);
421  gausfit[iarm]->FixParameter(2, _tres); // set sigma to timing resolution
422  gausfit[iarm]->SetLineColor(2);
423  }
424  }
425 
426  std::vector<float> hit_times[2]; // times of the hits in each [arm]
427 
428  // calculate bbc global variables
429  if (_verbose >= 10)
430  {
431  std::cout << "Hit PMT info " << std::endl;
432  }
433 
434  int epmt[2]{-1, -1}; // pmt of earliest time
435  // int lpmt[2] {-1,-1}; // pmt of latest time
436  double tepmt[2]{1e9, 1e9}; // earliest time
437  double tlpmt[2]{-1e9, -1e9}; // latest time
438 
439  for (int ipmt = 0; ipmt < MbdDefs::BBC_N_PMT; ipmt++)
440  {
441  MbdPmtHit *bbcpmt = bbcpmts->get_pmt(ipmt);
442  int arm = ipmt / 64;
443 
444  float t_pmt = bbcpmt->get_time(); // hit time of pmt
445  float q_pmt = bbcpmt->get_q(); // charge in pmt
446 
447  if (_verbose >= 10)
448  {
449  std::cout << ipmt << "\t" << t_pmt << std::endl;
450  }
451 
452  if (fabs(t_pmt) < 25. && q_pmt > 0.)
453  {
454  hit_times[arm].push_back(t_pmt);
455  hevt_bbct[arm]->Fill(t_pmt);
456 
457  m_bbcn[arm]++;
458  m_bbcq[arm] += q_pmt;
459 
460  if (_verbose >= 10)
461  {
462  std::cout << ipmt << "\t" << t_pmt << "\t" << q_pmt << std::endl;
463 
464  if (t_pmt < tepmt[arm])
465  {
466  epmt[arm] = ipmt;
467  tepmt[arm] = t_pmt;
468  }
469  if (t_pmt > tlpmt[arm])
470  {
471  // lpmt[arm] = ipmt;
472  tlpmt[arm] = t_pmt;
473  }
474  }
475  }
476  }
477 
478  if (_verbose >= 10)
479  {
480  std::cout << "nhits " << m_bbcn[0] << "\t" << m_bbcn[1] << std::endl;
481  }
482  // std::cout << "bbcte " << m_bbcte[0] << "\t" << m_bbcte[1] << std::endl;
483 
484  for (int iarm = 0; iarm < 2; iarm++)
485  {
486  if (hit_times[iarm].empty())
487  {
488  // std::cout << "hit_times size == 0" << std::endl;
489  continue;
490  }
491 
492  // std::cout << "EARLIEST " << iarm << std::endl;
493  // std::cout << "ERROR hit_times size == " << hit_times[iarm].size() << std::endl;
494 
495  std::sort(hit_times[iarm].begin(), hit_times[iarm].end());
496  float earliest = hit_times[iarm].at(0);
497  float latest = hit_times[iarm].back();
498  // std::cout << "earliest" << iarm << "\t" << earliest << std::endl;
499 
500  gausfit[iarm]->SetParameter(0, 5);
501  // gausfit[iarm]->SetParameter(1, earliest);
502  // gausfit[iarm]->SetRange(6, earliest + 5 * 0.05);
503  gausfit[iarm]->SetParameter(1, hevt_bbct[iarm]->GetMean());
504  gausfit[iarm]->SetParameter(2, hevt_bbct[iarm]->GetRMS());
505  gausfit[iarm]->SetRange(hevt_bbct[iarm]->GetMean() - 5, hevt_bbct[iarm]->GetMean() + 5);
506 
507  if (_verbose)
508  {
509  if (ac == nullptr)
510  {
511  ac = new TCanvas("ac", "ac", 550 * 1.5, 425 * 1.5);
512  ac->Divide(2, 1);
513  }
514  ac->cd(iarm + 1);
515  }
516 
517  hevt_bbct[iarm]->Fit(gausfit[iarm], "BNQLR");
518 
519  // m_bbct[iarm] = m_bbct[iarm] / m_bbcn[iarm];
520  m_bbct[iarm] = gausfit[iarm]->GetParameter(1);
521  m_bbcte[iarm] = earliest;
522  m_bbctl[iarm] = latest;
523 
524  //_bbcout->set_arm(iarm, m_bbcn[iarm], m_bbcq[iarm], m_bbct[iarm]);
525 
526  // if ( _verbose && mybbz[_syncevt]< -40. )
527  if (_verbose)
528  {
529  hevt_bbct[iarm]->GetXaxis()->SetRangeUser(tepmt[iarm] - 3., tlpmt[iarm] + 3.);
530  // hevt_bbct[iarm]->GetXaxis()->SetRangeUser(-20,20);
531  hevt_bbct[iarm]->Draw();
532  gausfit[iarm]->Draw("same");
533  gPad->Modified();
534  gPad->Update();
535  if (iarm == 1)
536  {
537  double zearly = (tepmt[0] - tepmt[1]) * MbdDefs::C / 2.0;
538  double znew = (m_bbct[0] - m_bbct[1]) * MbdDefs::C / 2.0;
539 
540  if (debugintt)
541  {
542  double intzdiff = intz[_syncevt] / 10. - mybbz[_syncevt];
543  double intzediff = intz[_syncevt] / 10. - zearly;
544  if (fabs(znew - mybbz[_syncevt]) > 0.1)
545  {
546  std::cout << "**ERR** " << znew << "\t" << mybbz[_syncevt] << std::endl;
547  }
548  std::string junk;
549  std::cout << m_evt << "\t" << bbevt[_syncevt] << "\t" << m_bbct[0] << "\t" << m_bbct[1] << std::endl;
550  std::cout << m_evt << " gmean " << gausfit[0]->GetParameter(1) << "\t" << gausfit[1]->GetParameter(1) << std::endl;
551  std::cout << m_evt << " mean " << hevt_bbct[0]->GetMean(1) << "\t" << hevt_bbct[1]->GetMean(1) << std::endl;
552  std::cout << m_evt << " gsigma " << gausfit[0]->GetParameter(2) << "\t" << gausfit[1]->GetParameter(2) << std::endl;
553  std::cout << m_evt << " rms " << hevt_bbct[0]->GetRMS() << "\t" << hevt_bbct[1]->GetRMS() << std::endl;
554  std::cout << m_evt << " te ch " << epmt[0] << "\t" << epmt[1] << "\t" << tepmt[0] << "\t" << tepmt[1] << std::endl;
555  std::cout << m_evt << " tetl " << m_bbcte[0] << "\t" << m_bbctl[0] << "\t" << m_bbcte[1] << "\t" << m_bbctl[1] << std::endl;
556  std::cout << m_evt << " bz intz " << mybbz[_syncevt] << "\t" << intz[_syncevt] / 10. << "\t" << intzdiff << "\t" << intzdiff * 2.0 / MbdDefs::C << std::endl;
557  std::cout << m_evt << " bze " << zearly << "\t" << intzediff << std::endl;
558  std::cout << "? ";
559  std::cin >> junk;
560  }
561  }
562  }
563  }
564 
565  // Get Zvertex, T0
566  if (m_bbcn[0] > 0 && m_bbcn[1] > 0)
567  {
568  // Now calculate zvtx, t0 from best times
569  if (_verbose >= 10)
570  {
571  std::cout << "Evt " << m_evt << "\tt0\t" << m_bbct[0] << "\t" << m_bbct[1] << std::endl;
572  std::cout << "bbcn " << m_bbcn[0] << "\t" << m_bbcn[1] << std::endl;
573  std::cout << "bbcq " << m_bbcq[0] << "\t" << m_bbcq[1] << std::endl;
574  }
575  m_bbcz = (m_bbct[0] - m_bbct[1]) * TMath::C() * 1e-7 / 2.0; // in cm
576  m_bbct0 = (m_bbct[0] + m_bbct[1]) / 2.0;
577 
578  // correct z-vertex
579  // m_bbcz += bz_offset;
580 
581  // hard code these for now
582  // need study to determine muliplicity dependence
583  m_bbczerr = 1.0; // cm
584  m_bbct0err = 0.05; // ns
585 
586  /*
587  // Use earliest time
588  //cout << "t0\t" << m_bbct[0] << "\t" << m_bbct[1] << std::endl;
589  //cout << "te\t" << m_bbcte[0] << "\t" << m_bbcte[1] << std::endl;
590  m_bbcz = (m_bbcte[0] - m_bbcte[1]) * TMath::C() * 1e-7 / 2.0; // in cm
591  m_bbct0 = (m_bbcte[0] + m_bbcte[1]) / 2.0;
592  */
593 
594  // if (_verbose > 10)
595  // if ( _verbose && mybbz[_syncevt]< -40. )
596  if (_verbose)
597  {
598  std::cout << "bbcz " << m_bbcz << std::endl;
599  std::string junk;
600  std::cout << "? ";
601  std::cin >> junk;
602  }
603  }
604 
605  // Fill rest of MbdOut
606  if (bbcout != nullptr)
607  {
608  for (int iarm = 0; iarm < 2; iarm++)
609  {
610  bbcout->set_arm(iarm, get_bbcn(iarm), get_bbcq(iarm), get_bbct(iarm));
611  bbcout->set_clocks(m_evt, m_clk, m_femclk); // only for V2
612  if (_verbose > 10)
613  {
614  std::cout << get_bbcn(iarm) << "\t" << get_bbcq(iarm) << "\t" << get_bbct(iarm) << std::endl;
615  }
616  }
617 
618  if (get_bbcn(0) > 0 && get_bbcn(1) > 0)
619  {
620  bbcout->set_t0(get_bbct0(), get_bbct0err());
621  bbcout->set_zvtx(get_bbcz(), get_bbczerr());
622  }
623  }
624 
625  if (debugintt)
626  {
627  _syncevt++;
628  _verbose = 0;
629  }
630  _verbose = 0;
631  return 1;
632 }
633 
634 // This needs to be reconsidered for 2024 run, hopefully timing instability is fixed by then!
635 // Only used in online monitoring
637 {
638  for (int ifeech = 0; ifeech < 256; ifeech++)
639  {
640  _mbdsig[ifeech].SetXY(m_samp[ifeech], m_adc[ifeech]);
641 
642  // determine the trig_samp board by board
643  int tq = (ifeech / 8) % 2; // 0 = T-channel, 1 = Q-channel
644  int pmtch = (ifeech / 16) * 8 + ifeech % 8;
645 
646  double x, y;
647  _mbdsig[ifeech].LocMax(x, y);
648  h2_tmax[tq]->Fill(x, pmtch);
649  }
650 
651  if (h2_tmax[1]->GetEntries() == 128 * 100)
652  {
653  TString name;
654  TH1 *h_trigsamp[16]{};
655  for (int iboard = 0; iboard < 16; iboard++)
656  {
657  name = "h_trigsamp";
658  name += iboard;
659  h_trigsamp[iboard] = h2_tmax[1]->ProjectionX(name, iboard * 8 + 1, (iboard + 1) * 8);
660  int maxbin = h_trigsamp[iboard]->GetMaximumBin();
661  TRIG_SAMP[iboard] = h_trigsamp[iboard]->GetBinCenter(maxbin);
662  // std::cout << "iboard " << iboard << "\t" << iboard*8+1 << "\t" << (iboard+1)*8 << "\t" << h_trigsamp[iboard]->GetEntries() << std::endl;
663  std::cout << "TRIG_SAMP" << iboard << "\t" << TRIG_SAMP[iboard] << std::endl;
664  }
665  }
666 
667  return 1;
668 }
669 
671 {
672  std::ifstream gainfile(gainfname);
673 
674  std::cout << "Reading gains from " << gainfname << std::endl;
675  int ch;
676  float integ, integerr;
677  float peak, peakerr;
678  float width, widtherr;
679  float chi2ndf;
680  while (gainfile >> ch >> integ >> peak >> width >> integerr >> peakerr >> widtherr >> chi2ndf)
681  {
682  gaincorr[ch] = 1.0 / peak;
683 
684  // std::cout << ch << "\t" << peak << std::endl;
685  }
686 
687  gainfile.close();
688 
689  return 1;
690 }
691 
692 // Read in tq t0 offset calibrations
694 {
695  std::ifstream tcalibfile(t0cal_fname);
696 
697  std::cout << "Reading tq_t0 offset calibrations from " << t0cal_fname << std::endl;
698 
699  int pmtnum;
700  float meanerr;
701  float sigma;
702  float sigmaerr;
703  for (int ipmt = 0; ipmt < MbdDefs::BBC_N_PMT; ipmt++)
704  {
705  tcalibfile >> pmtnum >> tq_t0_offsets[ipmt] >> meanerr >> sigma >> sigmaerr;
706  if (pmtnum != ipmt)
707  {
708  std::cout << "ERROR, pmtnum != ipmt, " << pmtnum << "\t" << ipmt << std::endl;
709  }
710  }
711 
712  tcalibfile.close();
713 
714  return 1;
715 }
716 
717 // Read in tq clk offset calibrations
719 {
720  std::ifstream tcalibfile(t0cal_fname);
721 
722  std::cout << "Reading tq_clk offset calibrations from " << t0cal_fname << std::endl;
723 
724  int pmtnum;
725  for (int ipmt = 0; ipmt < MbdDefs::BBC_N_PMT; ipmt++)
726  {
727  tcalibfile >> pmtnum >> tq_clk_offsets[ipmt];
728  if (pmtnum != ipmt)
729  {
730  std::cout << "ERROR, pmtnum != ipmt, " << pmtnum << "\t" << ipmt << std::endl;
731  }
732  }
733 
734  tcalibfile.close();
735 
736  return 1;
737 }
738 
739 // Read in tt clk offset calibrations
741 {
742  std::ifstream tcalibfile(t0cal_fname);
743 
744  std::cout << "Reading tq_clk offset calibrations from " << t0cal_fname << std::endl;
745 
746  int pmtnum;
747  for (int ipmt = 0; ipmt < MbdDefs::BBC_N_PMT; ipmt++)
748  {
749  tcalibfile >> pmtnum >> tt_clk_offsets[ipmt];
750  if (pmtnum != ipmt)
751  {
752  std::cout << "ERROR, pmtnum != ipmt, " << pmtnum << "\t" << ipmt << std::endl;
753  }
754  }
755 
756  tcalibfile.close();
757 
758  return 1;
759 }
760 
761 void MbdEvent::ReadSyncFile(const char *fname)
762 {
763  Int_t f_evt{0};
764  UShort_t f_femclk{0};
765  Float_t f_bz{0.};
766  Long64_t bco_full{0};
767  Double_t ES_zvtx{0.};
768  Double_t mbd_bz{0.};
769 
770  _synctfile = std::make_unique<TFile>(fname, "READ");
771  _syncttree = (TTree *) _synctfile->Get("t2");
772  _syncttree->SetBranchAddress("evt", &f_evt);
773  _syncttree->SetBranchAddress("femclk", &f_femclk);
774  _syncttree->SetBranchAddress("bz", &f_bz);
775  _syncttree->SetBranchAddress("bco_full", &bco_full);
776  _syncttree->SetBranchAddress("ES_zvtx", &ES_zvtx);
777  _syncttree->SetBranchAddress("mbd_bz", &mbd_bz);
778 
779  Stat_t nentries = _syncttree->GetEntries();
780  for (int ientry = 0; ientry < nentries; ientry++)
781  {
782  _syncttree->GetEntry(ientry);
783 
784  bbevt.push_back(f_evt);
785  bbclk.push_back(f_femclk);
786  mybbz.push_back(f_bz);
787  bco.push_back(bco_full);
788  intz.push_back(ES_zvtx);
789  bbz.push_back(mbd_bz);
790  }
791 
792  std::cout << "Read in " << bbevt.size() << " INTT sync events" << std::endl;
793 }