File indexing completed on 2025-08-06 08:17:27
0001 #include "Fun4AllStreamingLumiCountingInputManager.h"
0002
0003 #include <fun4allraw/InputManagerType.h>
0004 #include "SingleStreamingInputv2.h"
0005
0006 #include <ffarawobjects/Gl1Packet.h>
0007
0008 #include <fun4all/Fun4AllHistoManager.h>
0009 #include <fun4all/Fun4AllInputManager.h> // for Fun4AllInputManager
0010 #include <fun4all/Fun4AllReturnCodes.h>
0011 #include <fun4all/Fun4AllServer.h>
0012 #include <fun4all/Fun4AllSyncManager.h>
0013
0014 #include <ffaobjects/SyncObject.h> // for SyncObject
0015 #include <ffaobjects/SyncObjectv1.h>
0016
0017 #include <frog/FROG.h>
0018 #include <qautils/QAHistManagerDef.h>
0019 #include <qautils/QAUtil.h>
0020
0021 #include <phool/PHObject.h> // for PHObject
0022 #include <phool/getClass.h>
0023 #include <phool/phool.h> // for PHWHERE
0024 #include <boost/format.hpp>
0025
0026 #include <TFile.h>
0027 #include <TH1.h>
0028 #include <TSystem.h>
0029 #include <TTree.h>
0030
0031 #include <algorithm> // for max
0032 #include <cassert>
0033 #include <cstdint> // for uint64_t, uint16_t
0034 #include <cstdlib>
0035 #include <iostream> // for operator<<, basic_ostream, endl
0036 #include <utility> // for pair
0037
0038 Fun4AllStreamingLumiCountingInputManager::Fun4AllStreamingLumiCountingInputManager(const std::string &name, const std::string &dstnodename, const std::string &topnodename)
0039 : Fun4AllInputManager(name, dstnodename, topnodename)
0040 , m_SyncObject(new SyncObjectv1())
0041 {
0042 Fun4AllServer *se = Fun4AllServer::instance();
0043 m_topNode = se->topNode(TopNodeName());
0044
0045 createLuminosityHistos();
0046 return;
0047 }
0048
0049 Fun4AllStreamingLumiCountingInputManager::~Fun4AllStreamingLumiCountingInputManager()
0050 {
0051 if (IsOpen())
0052 {
0053 fileclose();
0054 }
0055
0056
0057 delete m_SyncObject;
0058
0059
0060 for (auto iter : m_Gl1InputVector)
0061 {
0062 delete iter;
0063 }
0064
0065 m_Gl1InputVector.clear();
0066 }
0067
0068 int Fun4AllStreamingLumiCountingInputManager::run(const int )
0069 {
0070 int iret = 0;
0071 if (m_gl1_registered_flag)
0072 {
0073 iret += FillGl1();
0074 }
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084 return iret;
0085 }
0086
0087 void Fun4AllStreamingLumiCountingInputManager::SetOutputFileName(const std::string &fileName)
0088 {
0089 m_outputFileName = fileName;
0090 }
0091
0092 int Fun4AllStreamingLumiCountingInputManager::fileclose()
0093 {
0094
0095
0096 return 0;
0097 }
0098
0099 void Fun4AllStreamingLumiCountingInputManager::Print(const std::string &what) const
0100 {
0101 if (what == "ALL" || what == "INPUTFILES")
0102 {
0103 std::cout << "-----------------------------" << std::endl;
0104 for (const auto &iter : m_Gl1InputVector)
0105 {
0106 std::cout << "Single Streaming Input Manager " << iter->Name() << " reads run "
0107 << iter->RunNumber()
0108 << " from file " << iter->FileName()
0109 << std::endl;
0110 }
0111 }
0112 Fun4AllInputManager::Print(what);
0113 return;
0114 }
0115
0116 int Fun4AllStreamingLumiCountingInputManager::ResetEvent()
0117 {
0118
0119
0120 return 0;
0121 }
0122
0123 int Fun4AllStreamingLumiCountingInputManager::PushBackEvents(const int )
0124 {
0125 return 0;
0126 }
0127
0128 int Fun4AllStreamingLumiCountingInputManager::GetSyncObject(SyncObject **mastersync)
0129 {
0130
0131
0132
0133
0134 if (!(*mastersync))
0135 {
0136 if (m_SyncObject)
0137 {
0138 *mastersync = dynamic_cast<SyncObject *>(m_SyncObject->CloneMe());
0139 assert(*mastersync);
0140 }
0141 }
0142 else
0143 {
0144 *(*mastersync) = *m_SyncObject;
0145 }
0146 return Fun4AllReturnCodes::SYNC_OK;
0147 }
0148
0149 int Fun4AllStreamingLumiCountingInputManager::SyncIt(const SyncObject *mastersync)
0150 {
0151 if (!mastersync)
0152 {
0153 std::cout << PHWHERE << Name() << " No MasterSync object, cannot perform synchronization" << std::endl;
0154 std::cout << "Most likely your first file does not contain a SyncObject and the file" << std::endl;
0155 std::cout << "opened by the Fun4AllDstInputManager with Name " << Name() << " has one" << std::endl;
0156 std::cout << "Change your macro and use the file opened by this input manager as first input" << std::endl;
0157 std::cout << "and you will be okay. Fun4All will not process the current configuration" << std::endl
0158 << std::endl;
0159 return Fun4AllReturnCodes::SYNC_FAIL;
0160 }
0161 int iret = m_SyncObject->Different(mastersync);
0162 if (iret)
0163 {
0164 std::cout << "big problem" << std::endl;
0165 exit(1);
0166 }
0167 return Fun4AllReturnCodes::SYNC_OK;
0168 }
0169
0170 std::string Fun4AllStreamingLumiCountingInputManager::GetString(const std::string &what) const
0171 {
0172 std::cout << PHWHERE << " called with " << what << " , returning empty string" << std::endl;
0173 return "";
0174 }
0175
0176 void Fun4AllStreamingLumiCountingInputManager::registerStreamingInput(SingleStreamingInputv2 *evtin, InputManagerType::enu_subsystem system)
0177 {
0178 evtin->StreamingLumiInputManager(this);
0179
0180
0181
0182
0183 if (system == InputManagerType::GL1)
0184 {
0185 if (!m_StreamingFlag)
0186 {
0187 evtin->CreateDSTNode(m_topNode);
0188 }
0189 }
0190 else
0191 {
0192 evtin->CreateDSTNode(m_topNode);
0193 }
0194 evtin->ConfigureStreamingInputManager();
0195 if (system == InputManagerType::GL1)
0196 {
0197 m_gl1_registered_flag = true;
0198 m_Gl1InputVector.push_back(evtin);
0199 }
0200 else
0201 {
0202 std::cout << "invalid subsystem flag " << system << std::endl;
0203 gSystem->Exit(1);
0204 exit(1);
0205 }
0206 if (Verbosity() > 3)
0207 {
0208 std::cout << "registering " << evtin->Name()
0209 << " number of registered inputs: "
0210 << m_Gl1InputVector.size()
0211 << std::endl;
0212 }
0213 std::cout << m_Gl1InputVector.size() << std::endl;
0214 }
0215
0216 void Fun4AllStreamingLumiCountingInputManager::AddGl1RawHit(uint64_t bclk, Gl1Packet *hit)
0217 {
0218 m_Gl1RawHitMap[bclk].Gl1RawHitVector.push_back(hit);
0219 }
0220
0221 void Fun4AllStreamingLumiCountingInputManager::AddGl1Window(uint64_t bco_trim, int negative_window, int positive_window)
0222 {
0223 m_BCOWindows[bco_trim] = std::make_pair(bco_trim - negative_window, bco_trim + positive_window);
0224 }
0225
0226 void Fun4AllStreamingLumiCountingInputManager::AddGl1BunchNumber(uint64_t bco_trim, int bunch_number)
0227 {
0228 m_BCOBunchNumber[bco_trim] = bunch_number;
0229 }
0230
0231 int Fun4AllStreamingLumiCountingInputManager::FillGl1()
0232 {
0233
0234 for (auto iter : m_Gl1InputVector)
0235 {
0236 if (Verbosity() > 0)
0237 {
0238 std::cout << "Fun4AllStreamingLumiCountingInputManager::FillGl1 - fill pool for " << iter->Name() << std::endl;
0239 std::cout << "Run number " << iter->RunNumber() << std::endl;
0240 }
0241 iter->FillPool();
0242
0243 if (m_RunNumber == 0)
0244 {
0245 m_RunNumber = iter->RunNumber();
0246 SetRunNumber(m_RunNumber);
0247 }
0248 else
0249 {
0250 if (m_RunNumber != iter->RunNumber())
0251 {
0252 std::cout << PHWHERE << " Run Number mismatch, run is "
0253 << m_RunNumber << ", " << iter->Name() << " reads "
0254 << iter->RunNumber() << std::endl;
0255 std::cout << "You are likely reading files from different runs, do not do that" << std::endl;
0256 Print("INPUTFILES");
0257 gSystem->Exit(1);
0258 exit(1);
0259 }
0260 }
0261 }
0262
0263 if (Verbosity() > 0)
0264 {
0265 std::cout << "Here BCO " << m_BCOWindows.begin()->first << " left " << m_BCOWindows.begin()->second.first << " right " << m_BCOWindows.begin()->second.second << std::endl;
0266 }
0267
0268
0269
0270
0271
0272
0273
0274
0275
0276
0277
0278
0279
0280
0281
0282
0283
0284 if (m_BCOWindows.size() > 1)
0285 {
0286 auto first_element = m_BCOWindows.begin();
0287 auto second_element = std::next(m_BCOWindows.begin());
0288
0289
0290
0291
0292
0293
0294 m_diffBCO = second_element->first - first_element->first;
0295
0296 if (second_element->first - first_element->first > 1099510000000)
0297 {
0298 flat_overflow = true;
0299
0300 bco_temp = first_element->first;
0301 m_BCOWindows.erase(m_BCOWindows.begin());
0302 bco_temp += 1099511627775 + 1;
0303 m_BCOWindows[bco_temp] = std::make_pair(bco_temp - m_negative_bco_window, bco_temp + m_positive_bco_window);
0304 first_element = m_BCOWindows.begin();
0305 second_element = std::next(m_BCOWindows.begin());
0306 m_diffBCO = second_element->first - first_element->first;
0307 std::cout << "overflow new diff " << m_diffBCO << " new first element " << first_element->first << " new second element " << second_element->first << std::endl;
0308 }
0309 h_diffbco->Fill(m_diffBCO);
0310 if (m_diffBCO < static_cast<int>(m_negative_bco_window + m_positive_bco_window))
0311 {
0312 m_BCOWindows.begin()->second.second = second_element->second.first;
0313 std::cout << "*** new Key 1 BCO " << m_BCOWindows.begin()->first << " left " << m_BCOWindows.begin()->second.first << " right " << m_BCOWindows.begin()->second.second << std::endl;
0314 }
0315 }
0316
0317 m_bco_trim = m_BCOWindows.begin()->first;
0318 m_lower_bound = m_BCOWindows.begin()->second.first;
0319 m_upper_bound = m_BCOWindows.begin()->second.second;
0320 m_bunch_number = m_BCOBunchNumber[m_BCOWindows.begin()->first];
0321
0322 h_bunchnumber->Fill(m_BCOBunchNumber[m_BCOWindows.begin()->first]);
0323 h_lumibco->Fill(m_BCOWindows.begin()->second.second - m_BCOWindows.begin()->second.first);
0324
0325 int lower = -1 * static_cast<int>(m_bco_trim - m_lower_bound);
0326 int upper = (m_upper_bound > m_bco_trim) ? static_cast<int>(m_upper_bound - m_bco_trim) : -1 * static_cast<int>(m_bco_trim - m_upper_bound);
0327
0328 for (int i = lower; i < upper; i++)
0329 {
0330 int adjusted_bunch = m_bunch_number + i;
0331 while (adjusted_bunch < 0)
0332 {
0333 adjusted_bunch += 120;
0334 }
0335 while (adjusted_bunch > 119)
0336 {
0337 adjusted_bunch -= 120;
0338 }
0339 if (i != 0)
0340 {
0341 h_bunchnumber_occur->Fill(adjusted_bunch);
0342 }
0343 }
0344
0345 if (!m_BCOBunchNumber.empty())
0346 {
0347 m_BCOBunchNumber.erase(m_BCOWindows.begin()->first);
0348
0349 }
0350 if (!m_BCOWindows.empty())
0351 {
0352 m_BCOWindows.erase(m_BCOWindows.begin());
0353 }
0354 if (flat_overflow)
0355 {
0356 m_BCOWindows.erase(m_BCOWindows.begin());
0357 bco_temp -= 1099511627775 + 1;
0358 m_BCOWindows[bco_temp] = std::make_pair(bco_temp - m_negative_bco_window, bco_temp + m_positive_bco_window);
0359 std::cout << " Change back, new bco window map " << m_BCOBunchNumber.begin()->first << std::endl;
0360 flat_overflow = false;
0361 }
0362
0363
0364
0365 Gl1Packet *gl1packet = findNode::getClass<Gl1Packet>(m_topNode, "GL1RAWHIT");
0366 for (auto gl1hititer : m_Gl1RawHitMap.begin()->second.Gl1RawHitVector)
0367 {
0368 if (!m_StreamingFlag)
0369 {
0370 gl1packet->FillFrom(gl1hititer);
0371 MySyncManager()->CurrentEvent(gl1packet->getEvtSequence());
0372 }
0373 }
0374
0375
0376 Gl1Packet *p_gl1 = findNode::getClass<Gl1Packet>(m_topNode, "GL1RAWHIT");
0377 if (!p_gl1)
0378 {
0379 std::cout << "CAN not find this Gl1Packet" << std::endl;
0380 }
0381 else
0382 {
0383 int bunchnumber = p_gl1->getBunchNumber();
0384
0385
0386
0387 if (p_gl1->lValue(0, "GL1PRAW"))
0388 {
0389
0390
0391 m_bunchnumber_MBDNS_raw[bunchnumber] = p_gl1->lValue(0, "GL1PRAW");
0392 m_bunchnumber_MBDNS_live[bunchnumber] = p_gl1->lValue(0, "GL1PLIVE");
0393 m_bunchnumber_MBDNS_scaled[bunchnumber] = p_gl1->lValue(0, "GL1PSCALED");
0394 m_bunchnumber_ZDCCoin_raw[bunchnumber] = p_gl1->lValue(5, "GL1PRAW");
0395
0396
0397 }
0398
0399 if (p_gl1->lValue(0, 0))
0400 {
0401
0402
0403 m_rawgl1scaler = p_gl1->lValue(0, 0);
0404 }
0405 }
0406 ttree->Fill();
0407
0408 if (m_lastevent_flag)
0409 {
0410 for (const auto &[bunchnumber, mbdns_value] : m_bunchnumber_MBDNS_raw)
0411 {
0412 h_gl1p_MBDSN_bunchid_raw->Fill(bunchnumber, mbdns_value);
0413 }
0414 for (const auto &[bunchnumber, mbdns_value] : m_bunchnumber_MBDNS_live)
0415 {
0416 h_gl1p_MBDSN_bunchid_live->Fill(bunchnumber, mbdns_value);
0417 }
0418 for (const auto &[bunchnumber, mbdns_value] : m_bunchnumber_MBDNS_scaled)
0419 {
0420 h_gl1p_MBDSN_bunchid_scaled->Fill(bunchnumber, mbdns_value);
0421 }
0422
0423 h_gl1p_rawgl1scaler->Fill(1, m_rawgl1scaler);
0424
0425 for (const auto &[bunchnumber, mbdns_value] : m_bunchnumber_ZDCCoin_raw)
0426 {
0427 h_gl1p_ZDCCoin_bunchid_raw->Fill(bunchnumber, mbdns_value);
0428 }
0429 }
0430
0431
0432
0433
0434
0435
0436 if (m_StreamingFlag)
0437 {
0438 for (auto iter : m_Gl1InputVector)
0439 {
0440 delete iter;
0441 }
0442 m_gl1_registered_flag = false;
0443 m_Gl1InputVector.clear();
0444 }
0445 else
0446 {
0447 for (auto iter : m_Gl1InputVector)
0448 {
0449 iter->CleanupUsedPackets(m_Gl1RawHitMap.begin()->first);
0450 }
0451 m_Gl1RawHitMap.begin()->second.Gl1RawHitVector.clear();
0452 m_Gl1RawHitMap.erase(m_Gl1RawHitMap.begin());
0453 }
0454
0455
0456
0457 if (Verbosity() > 0)
0458 {
0459 if (m_alldone_flag)
0460 {
0461 std::cout << "all done is true" << std::endl;
0462 }
0463 }
0464
0465 if (m_alldone_flag)
0466 {
0467 std::cout << m_event_number << " Events -- Storing files to output.root" << std::endl;
0468 std::string updatedFileName = m_outputFileName + "_" + std::to_string(m_event_number) + ".root";
0469 if (TFile::Open(updatedFileName.c_str(), "READ"))
0470 {
0471 updatedFileName = m_outputFileName + "_" + std::to_string(m_event_number + 1) + ".root";
0472 }
0473 tfile = TFile::Open(updatedFileName.c_str(), "RECREATE", "");
0474 ttree->Write("", TObject::kOverwrite);
0475 h_lumibco->Write("", TObject::kOverwrite);
0476 h_bunchnumber->Write("", TObject::kOverwrite);
0477 h_bunchnumber_occur->Write("", TObject::kOverwrite);
0478 h_diffbco->Write("", TObject::kOverwrite);
0479 h_gl1p_MBDSN_bunchid_raw->Write("", TObject::kOverwrite);
0480 h_gl1p_MBDSN_bunchid_live->Write("", TObject::kOverwrite);
0481 h_gl1p_MBDSN_bunchid_scaled->Write("", TObject::kOverwrite);
0482 h_gl1p_rawgl1scaler->Write("", TObject::kOverwrite);
0483 h_gl1p_ZDCCoin_bunchid_raw->Write("", TObject::kOverwrite);
0484 tfile->Close();
0485 delete tfile;
0486
0487 ttree->Reset();
0488 h_lumibco->Reset();
0489 h_bunchnumber->Reset();
0490 h_bunchnumber_occur->Reset();
0491 h_diffbco->Reset();
0492 h_gl1p_MBDSN_bunchid_raw->Reset();
0493 h_gl1p_MBDSN_bunchid_live->Reset();
0494 h_gl1p_MBDSN_bunchid_scaled->Reset();
0495 h_gl1p_rawgl1scaler->Reset();
0496 h_gl1p_ZDCCoin_bunchid_raw->Reset();
0497 }
0498
0499 return 0;
0500 }
0501
0502 void Fun4AllStreamingLumiCountingInputManager::SetNegativeWindow(const unsigned int i)
0503 {
0504 m_negative_bco_window = std::max(i, m_negative_bco_window);
0505 }
0506
0507 void Fun4AllStreamingLumiCountingInputManager::SetPositiveWindow(const unsigned int i)
0508 {
0509 m_positive_bco_window = std::max(i, m_positive_bco_window);
0510 }
0511
0512 void Fun4AllStreamingLumiCountingInputManager::createLuminosityHistos()
0513 {
0514 auto hm = QAHistManagerDef::getHistoManager();
0515 assert(hm);
0516
0517 {
0518 auto tr = new TTree("BCOWindowTree", "BCO Window Data");
0519 tr->Branch("bco_trim", &m_bco_trim);
0520 tr->Branch("lower_bound", &m_lower_bound);
0521 tr->Branch("upper_bound", &m_upper_bound);
0522 tr->Branch("bunch_number", &m_bunch_number);
0523
0524 tr->SetAutoFlush(100000);
0525 hm->registerHisto(tr);
0526 }
0527
0528 {
0529 auto h = new TH1I("h_LumiBCO", "Lumi BCO", 500, 0, 500);
0530 h->GetXaxis()->SetTitle(" Lumi BCO per event");
0531 h->SetTitle("Number of BCO matched");
0532 hm->registerHisto(h);
0533 }
0534 {
0535 auto h = new TH1I("h_BunchNumber", "Bunch Number Lumi BCO", 121, -0.5, 120.5);
0536 h->GetXaxis()->SetTitle("Bunch Number per event");
0537 h->SetTitle("Number of crossing");
0538 hm->registerHisto(h);
0539 }
0540 {
0541 auto h = new TH1D("h_BunchNumberOccurance", "Bunch Number Lumi BCO", 120, -0.5, 119.5);
0542 h->GetXaxis()->SetTitle("Bunch Number per time window");
0543 h->SetTitle("Number of crossing");
0544 hm->registerHisto(h);
0545 }
0546 {
0547 auto h = new TH1I("h_diffBCO", "gl1 bco 1-2", 3500, 0, 3500);
0548 h->GetXaxis()->SetTitle("GL1 BCO difference");
0549 h->SetTitle("Number of crossing");
0550 hm->registerHisto(h);
0551 }
0552 {
0553 auto h = new TH1D("h_MBDSNraw_BunchID", "Bunch Number Lumi BCO", 121, -0.5, 120.5);
0554 h->GetXaxis()->SetTitle("Bunch Number per event");
0555 h->SetTitle("MBDSN Number of crossing");
0556 hm->registerHisto(h);
0557 }
0558 {
0559 auto h = new TH1D("h_MBDSNlive_BunchID", "Bunch Number Lumi BCO", 121, -0.5, 120.5);
0560 h->GetXaxis()->SetTitle("Bunch Number per event");
0561 h->SetTitle("MBDSN Number of crossing");
0562 hm->registerHisto(h);
0563 }
0564 {
0565 auto h = new TH1D("h_MBDSNscaled_BunchID", "Bunch Number Lumi BCO", 121, -0.5, 120.5);
0566 h->GetXaxis()->SetTitle("Bunch Number per event");
0567 h->SetTitle("MBDSN Number of crossing");
0568 hm->registerHisto(h);
0569 }
0570 {
0571 auto h = new TH1D("h_rawgl1scalerBunchID", "Bunch Number Lumi BCO", 10, -0.5, 9.5);
0572 h->GetXaxis()->SetTitle("Bunch Number per event");
0573 h->SetTitle("raw GL1 scaler");
0574 hm->registerHisto(h);
0575 }
0576 {
0577 auto h = new TH1D("h_gl1p_ZDCCoin_BunchID", "Bunch Number Lumi BCO", 121, -0.5, 120.5);
0578 h->GetXaxis()->SetTitle("Bunch Number per event");
0579 h->SetTitle("raw GL1 scaler");
0580 hm->registerHisto(h);
0581 }
0582
0583 h_lumibco = dynamic_cast<TH1 *>(hm->getHisto("h_LumiBCO"));
0584 h_bunchnumber = dynamic_cast<TH1 *>(hm->getHisto("h_BunchNumber"));
0585 h_bunchnumber_occur = dynamic_cast<TH1 *>(hm->getHisto("h_BunchNumberOccurance"));
0586 ttree = dynamic_cast<TTree *>(hm->getHisto("BCOWindowTree"));
0587 h_diffbco = dynamic_cast<TH1 *>(hm->getHisto("h_diffBCO"));
0588 h_gl1p_MBDSN_bunchid_raw = dynamic_cast<TH1 *>(hm->getHisto("h_MBDSNraw_BunchID"));
0589 h_gl1p_MBDSN_bunchid_live = dynamic_cast<TH1 *>(hm->getHisto("h_MBDSNlive_BunchID"));
0590 h_gl1p_MBDSN_bunchid_scaled = dynamic_cast<TH1 *>(hm->getHisto("h_MBDSNscaled_BunchID"));
0591 h_gl1p_rawgl1scaler = dynamic_cast<TH1 *>(hm->getHisto("h_rawgl1scalerBunchID"));
0592 h_gl1p_ZDCCoin_bunchid_raw = dynamic_cast<TH1 *>(hm->getHisto("h_gl1p_ZDCCoin_BunchID"));
0593 }