Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:18:37

0001 #include "CaloTriggerEmulator.h"
0002 
0003 #include "LL1Outv1.h"
0004 #include "TriggerPrimitiveContainerv1.h"
0005 #include "TriggerPrimitivev1.h"
0006 
0007 #include <calobase/TowerInfo.h>
0008 #include <calobase/TowerInfoContainer.h>
0009 #include <calobase/TowerInfoDefs.h>
0010 
0011 #include <ffamodules/CDBInterface.h>
0012 
0013 #include <ffarawobjects/CaloPacket.h>
0014 #include <ffarawobjects/CaloPacketContainer.h>
0015 
0016 #include <cdbobjects/CDBHistos.h>  // for CDBHistos
0017 #include <cdbobjects/CDBTTree.h>   // for CDBHistos
0018 
0019 #include <fun4all/Fun4AllReturnCodes.h>
0020 
0021 #include <phool/PHCompositeNode.h>
0022 #include <phool/PHIODataNode.h>
0023 #include <phool/PHNode.h>
0024 #include <phool/PHNodeIterator.h>
0025 #include <phool/PHObject.h>
0026 #include <phool/getClass.h>
0027 #include <phool/phool.h>
0028 
0029 #include <Event/Event.h>
0030 #include <Event/EventTypes.h>
0031 #include <Event/packet.h>
0032 
0033 #include <TFile.h>
0034 #include <TH1.h>
0035 #include <TNtuple.h>
0036 
0037 #include <algorithm>
0038 #include <cstdint>
0039 #include <cstdlib>
0040 #include <iostream>
0041 #include <string>
0042 #include <utility>
0043 
0044 // constructor
0045 CaloTriggerEmulator::CaloTriggerEmulator(const std::string &name)
0046   : SubsysReco(name)
0047 {
0048   // is data flag is not used right now
0049 
0050   // default nsamples is 16 for mbd, 12 for calos
0051 
0052   // for MBD, this is the peak sample in run-23 data
0053 
0054   // default values for the lookup tables.
0055   // TODO: to CDB the LUTs from the database
0056 
0057   // reset variables
0058   for (unsigned int i = 0; i < 1024; i++)
0059   {
0060     m_l1_8x8_table[i] = i;
0061     if (i >= 0xff)
0062     {
0063       m_l1_8x8_table[i] = 0xff;
0064     }
0065   }
0066 
0067   for (unsigned int i = 0; i < 1024; i++)
0068   {
0069     m_l1_adc_table[i] = (i) & 0x3ffU;
0070   }
0071 
0072   for (unsigned int i = 0; i < 4096; i++)
0073   {
0074     m_l1_slewing_table[i] = (i) & 0x3ffU;
0075   }
0076 
0077   for (int i = 0; i < 24576; i++)
0078   {
0079     unsigned int key = TowerInfoDefs::encode_emcal(i);
0080     h_emcal_lut[key] = nullptr;
0081   }
0082 
0083   for (int i = 0; i < 1536; i++)
0084   {
0085     unsigned int key = TowerInfoDefs::encode_hcal(i);
0086     h_hcalin_lut[key] = nullptr;
0087   }
0088   for (int i = 0; i < 1536; i++)
0089   {
0090     unsigned int key = TowerInfoDefs::encode_hcal(i);
0091     h_hcalout_lut[key] = nullptr;
0092   }
0093 
0094   // Set HCAL LL1 lookup table for the cosmic coincidence trigger.
0095   if (m_triggerid == TriggerDefs::TriggerId::cosmic_coinTId)
0096   {
0097     unsigned int bits1;
0098     unsigned int bits2;
0099     unsigned int sumbits1;
0100     unsigned int sumbits2;
0101     for (unsigned int i = 0; i < 4096; i++)
0102     {
0103       sumbits1 = 0;
0104       sumbits2 = 0;
0105 
0106       bits1 = (i & 0x3fU);
0107       bits2 = ((i >> 6U) & 0x3fU);
0108       for (unsigned int j = 0; j < 3; j++)
0109       {
0110         if (((bits1 >> j) & 0x1U) && ((bits2 >> j) & 0x1U))
0111         {
0112           sumbits1++;
0113         }
0114         if (((bits1 >> (j + 3U)) & 0x1U) && ((bits2 >> (j + 3U)) & 0x1U))
0115         {
0116           sumbits2++;
0117         }
0118       }
0119 
0120       m_l1_hcal_table[i] = 0;
0121       if (i == 0)
0122       {
0123         continue;
0124       }
0125       m_l1_hcal_table[i] |= (sumbits1 ? 0x1U : 0U);
0126       m_l1_hcal_table[i] |= (sumbits2 ? 0x2U : 0U);
0127     }
0128   }
0129 
0130   else if (m_triggerid == TriggerDefs::TriggerId::cosmicTId)
0131   {
0132     unsigned int bits1;
0133     unsigned int bits2;
0134     unsigned int sumbits1;
0135     unsigned int sumbits2;
0136     for (unsigned int i = 0; i < 4096; i++)
0137     {
0138       sumbits1 = 0;
0139       sumbits2 = 0;
0140 
0141       bits1 = (i & 0x3fU);
0142       bits2 = ((i >> 6U) & 0x3fU);
0143       for (unsigned int j = 0; j < 6; j++)
0144       {
0145         sumbits1 += ((bits1 >> j) & 0x1U);
0146         sumbits2 += ((bits2 >> j) & 0x1U);
0147       }
0148 
0149       m_l1_hcal_table[i] = 0;
0150       if (i == 0)
0151       {
0152         continue;
0153       }
0154       m_l1_hcal_table[i] |= (sumbits1 ? 0x1U : 0U);
0155       m_l1_hcal_table[i] |= (sumbits2 ? 0x2U : 0U);
0156     }
0157   }
0158 
0159   // point to null for all of these objects to be added to or grabbed from the node tree.
0160   // this will hold the ll1 info that goes through the emulator
0161   m_ll1out_pair = nullptr;
0162   m_ll1out_photon = nullptr;
0163   m_ll1out_jet = nullptr;
0164 
0165   // waveform containers to be grabbed from node tree.
0166   // Done int the CaloPacketGetter
0167 
0168   // _waveforms_emcal = nullptr;
0169   // _waveforms_hcalin = nullptr;
0170   // _waveforms_hcalout = nullptr;
0171   // _waveforms_mbd = nullptr;
0172 
0173   // // to hold the primitives constructed from the waveforms.
0174   // _primitiveprimitives = nullptr;
0175   // _primitives_emcal = nullptr;
0176   // _primitives_hcalin = nullptr;
0177   // _primitives_hcalout = nullptr;
0178   // _primitives_emcal_ll1 = nullptr;
0179   // _primitives_hcal_ll1 = nullptr;
0180 
0181   // _primitive = nullptr;
0182 
0183   m_n_primitives = 0;
0184   m_n_sums = 16;
0185   m_trig_sample = -1;
0186   m_trig_sub_delay = 4;
0187 
0188   // define a detector map for detectors included in a trigger
0189   m_det_map[TriggerDefs::TriggerId::noneTId] = {};
0190   m_det_map[TriggerDefs::TriggerId::jetTId] = {"EMCAL", "HCALIN", "HCALOUT"};
0191   m_det_map[TriggerDefs::TriggerId::cosmicTId] = {"HCALIN", "HCALOUT"};
0192   m_det_map[TriggerDefs::TriggerId::cosmic_coinTId] = {"HCALIN", "HCALOUT"};
0193   m_det_map[TriggerDefs::TriggerId::pairTId] = {"EMCAL"};
0194   m_det_map[TriggerDefs::TriggerId::photonTId] = {"EMCAL", "HCALIN", "HCALOUT"};
0195 
0196   // define primitive map as number of primitives in a detector.
0197   m_prim_map[TriggerDefs::DetectorId::noneDId] = 0;
0198   m_prim_map[TriggerDefs::DetectorId::emcalDId] = 384;
0199   m_prim_map[TriggerDefs::DetectorId::hcalinDId] = 24;
0200   m_prim_map[TriggerDefs::DetectorId::hcaloutDId] = 24;
0201   m_prim_map[TriggerDefs::DetectorId::hcalDId] = 24;
0202 
0203   m_prim_ll1_map[TriggerDefs::TriggerId::jetTId] = 16;
0204   m_prim_ll1_map[TriggerDefs::TriggerId::pairTId] = 16;
0205   m_prim_ll1_map[TriggerDefs::TriggerId::photonTId] = 384;
0206   // booleans to control the input of detector data
0207   m_do_emcal = false;
0208   m_do_hcalin = false;
0209   m_do_hcalout = false;
0210 
0211   m_masks_channel = {};  //, 70385703};
0212   m_masks_fiber = {};    //, 70385696};
0213 }
0214 
0215 // check whether a channel has been masked
0216 bool CaloTriggerEmulator::CheckChannelMasks(TriggerDefs::TriggerSumKey key)
0217 {
0218   for (unsigned int &it : m_masks_channel)
0219   {
0220     if (key == it)
0221     {
0222       return true;
0223     }
0224   }
0225   return false;
0226 }
0227 
0228 // cehck whether a fiber has been masked
0229 bool CaloTriggerEmulator::CheckFiberMasks(TriggerDefs::TriggerPrimKey key)
0230 {
0231   return (std::find(m_masks_fiber.begin(), m_masks_fiber.end(), key) != m_masks_fiber.end());
0232 }
0233 
0234 void CaloTriggerEmulator::LoadFiberMasks()
0235 {
0236   TFile *fin = new TFile(m_optmask_file.c_str(), "r");
0237   TNtuple *tn_keys = (TNtuple *) fin->Get("tn_optmask");
0238   float key;
0239   tn_keys->SetBranchAddress("primkey", &key);
0240   for (int i = 0; i < tn_keys->GetEntries(); i++)
0241   {
0242     tn_keys->GetEntry(i);
0243     unsigned int primkey = static_cast<unsigned int>((int) key);
0244     m_masks_fiber.push_back(primkey);
0245   }
0246 
0247   fin->Close();
0248   delete fin;
0249 }
0250 
0251 // setting the trigger type
0252 void CaloTriggerEmulator::setTriggerType(const std::string &name)
0253 {
0254   m_trigger = name;
0255   m_triggerid = TriggerDefs::GetTriggerId(m_trigger);
0256 }
0257 
0258 // setting the trigger typ
0259 void CaloTriggerEmulator::setTriggerType(TriggerDefs::TriggerId triggerid)
0260 {
0261   m_triggerid = triggerid;
0262 }
0263 
0264 // make file and histomanager (but nothing goes in the file at the moment)
0265 int CaloTriggerEmulator::Init(PHCompositeNode * /*topNode*/)
0266 {
0267   return 0;
0268 }
0269 
0270 // at the beginning of the run
0271 int CaloTriggerEmulator::InitRun(PHCompositeNode *topNode)
0272 {
0273   // Get the detectors that are used for a given trigger.
0274 
0275   if (m_triggerid == TriggerDefs::TriggerId::jetTId)
0276   {
0277     if (Verbosity() >= 2)
0278     {
0279       std::cout << "Using Jet Trigger." << std::endl;
0280     }
0281     if (!m_force_emcal)
0282     {
0283       m_do_emcal = true;
0284     }
0285     if (!m_force_hcalin)
0286     {
0287       m_do_hcalin = true;
0288     }
0289     if (!m_force_hcalout)
0290     {
0291       m_do_hcalout = true;
0292     }
0293   }
0294   else if (m_triggerid == TriggerDefs::TriggerId::photonTId)
0295   {
0296     if (Verbosity() >= 2)
0297     {
0298       std::cout << "Using Photon Trigger." << std::endl;
0299     }
0300     if (!m_force_emcal)
0301     {
0302       m_do_emcal = true;
0303     }
0304     if (!m_force_hcalin)
0305     {
0306       m_do_hcalin = false;
0307     }
0308     if (!m_force_hcalout)
0309     {
0310       m_do_hcalout = false;
0311     }
0312 
0313     if (m_do_emcal && (m_do_hcalin || m_do_hcalout))
0314     {
0315       std::cout << "Cannot run PHOTON with hcal and emcal" << std::endl;
0316       return Fun4AllReturnCodes::ABORTRUN;
0317     }
0318   }
0319   else if (m_triggerid == TriggerDefs::TriggerId::cosmicTId)
0320   {
0321     if (Verbosity() >= 2)
0322     {
0323       std::cout << "Using Cosmic Trigger." << std::endl;
0324     }
0325     if (!m_force_emcal)
0326     {
0327       m_do_emcal = false;
0328     }
0329     if (!m_force_hcalin)
0330     {
0331       m_do_hcalin = true;
0332     }
0333     if (!m_force_hcalout)
0334     {
0335       m_do_hcalout = true;
0336     }
0337   }
0338   else if (m_triggerid == TriggerDefs::TriggerId::cosmic_coinTId)
0339   {
0340     if (Verbosity() >= 2)
0341     {
0342       std::cout << "Using Cosmic Coincidence Trigger." << std::endl;
0343     }
0344     if (!m_force_emcal)
0345     {
0346       m_do_emcal = false;
0347     }
0348     if (!m_force_hcalin)
0349     {
0350       m_do_hcalin = true;
0351     }
0352     if (!m_force_hcalout)
0353     {
0354       m_do_hcalout = true;
0355     }
0356   }
0357   else if (m_triggerid == TriggerDefs::TriggerId::pairTId)
0358   {
0359     if (Verbosity() >= 2)
0360     {
0361       std::cout << "Using Pair Trigger." << std::endl;
0362     }
0363     if (!m_force_emcal)
0364     {
0365       m_do_emcal = true;
0366     }
0367     if (!m_force_hcalin)
0368     {
0369       m_do_hcalin = false;
0370     }
0371     if (!m_force_hcalout)
0372     {
0373       m_do_hcalout = false;
0374     }
0375   }
0376   else
0377   {
0378     std::cout << __FUNCTION__ << " : No trigger selected using ALL" << std::endl;
0379 
0380     m_trigger = "PHYSICS";
0381     m_triggerid = TriggerDefs::TriggerId::physicsTId;
0382 
0383     if (!m_force_emcal)
0384     {
0385       m_do_emcal = true;
0386     }
0387     if (!m_force_hcalin)
0388     {
0389       m_do_hcalin = true;
0390     }
0391     if (!m_force_hcalout)
0392     {
0393       m_do_hcalout = true;
0394     }
0395   }
0396 
0397   m_ll1_nodename = "LL1OUT_" + m_trigger;
0398   m_prim_nodename = "TRIGGERPRIMITIVES_" + m_trigger;
0399 
0400   // Get the calibrations and proroceduce the lookup tables;
0401 
0402   if (Download_Calibrations())
0403   {
0404     return Fun4AllReturnCodes::ABORTRUN;
0405   }
0406 
0407   CreateNodes(topNode);
0408 
0409   return 0;
0410 }
0411 
0412 int CaloTriggerEmulator::Download_Calibrations()
0413 {
0414   if (m_do_emcal)
0415   {
0416     std::string calibName = "cemc_adcskipmask";
0417 
0418     std::string calibdir = CDBInterface::instance()->getUrl(calibName);
0419     if (calibdir.empty())
0420     {
0421       std::cout << PHWHERE << "ADC Skip mask not found in CDB, not even in the default... " << std::endl;
0422       exit(1);
0423     }
0424     cdbttree_adcmask = new CDBTTree(calibdir);
0425   }
0426   if (!m_optmask_file.empty())
0427   {
0428     LoadFiberMasks();
0429   }
0430 
0431   if (m_do_emcal && !m_default_lut_emcal)
0432   {
0433     if (!m_emcal_lutname.empty())
0434     {
0435       cdbttree_emcal = new CDBHistos(m_emcal_lutname);
0436     }
0437     else
0438     {
0439       std::string calibdir = CDBInterface::instance()->getUrl("emcal_trigger_lut");
0440       if (calibdir.empty())
0441       {
0442         m_default_lut_emcal = true;
0443         std::cout << "Could not find and load histograms for EMCAL LUTs! defaulting to the identity table!" << std::endl;
0444       }
0445       else
0446       {
0447         cdbttree_emcal = new CDBHistos(calibdir);
0448       }
0449     }
0450 
0451     if (cdbttree_emcal)
0452     {
0453       cdbttree_emcal->LoadCalibrations();
0454 
0455       for (int i = 0; i < 24576; i++)
0456       {
0457         std::string histoname = "h_emcal_lut_" + std::to_string(i);
0458         unsigned int key = TowerInfoDefs::encode_emcal(i);
0459         h_emcal_lut[key] = cdbttree_emcal->getHisto(histoname);
0460       }
0461     }
0462   }
0463   if (m_do_hcalin && !m_default_lut_hcalin)
0464   {
0465     if (!m_hcalin_lutname.empty())
0466     {
0467       cdbttree_hcalin = new CDBHistos(m_hcalin_lutname);
0468     }
0469     else
0470     {
0471       std::string calibdir = CDBInterface::instance()->getUrl("hcalin_trigger_lut");
0472       if (calibdir.empty())
0473       {
0474         m_default_lut_hcalin = true;
0475         std::cout << "Could not find and load histograms for HCALIN LUTs! defaulting to the identity table!" << std::endl;
0476       }
0477       else
0478       {
0479         cdbttree_hcalin = new CDBHistos(calibdir);
0480       }
0481     }
0482     if (cdbttree_hcalin)
0483     {
0484       cdbttree_hcalin->LoadCalibrations();
0485 
0486       for (int i = 0; i < 1536; i++)
0487       {
0488         std::string histoname = "h_hcalin_lut_" + std::to_string(i);
0489         unsigned int key = TowerInfoDefs::encode_hcal(i);
0490         h_hcalin_lut[key] = cdbttree_hcalin->getHisto(histoname);
0491       }
0492     }
0493   }
0494   if (m_do_hcalout && !m_default_lut_hcalout)
0495   {
0496     if (!m_hcalout_lutname.empty())
0497     {
0498       cdbttree_hcalout = new CDBHistos(m_hcalout_lutname);
0499     }
0500     else
0501     {
0502       std::string calibdir = CDBInterface::instance()->getUrl("hcalout_trigger_lut");
0503       if (calibdir.empty())
0504       {
0505         m_default_lut_hcalout = true;
0506         std::cout << "Could not find and load histograms for HCALOUT LUTs! defaulting to the identity table!" << std::endl;
0507       }
0508       else
0509       {
0510         cdbttree_hcalout = new CDBHistos(calibdir);
0511       }
0512     }
0513     if (cdbttree_hcalout)
0514     {
0515       cdbttree_hcalout->LoadCalibrations();
0516 
0517       for (int i = 0; i < 1536; i++)
0518       {
0519         std::string histoname = "h_hcalout_lut_" + std::to_string(i);
0520         unsigned int key = TowerInfoDefs::encode_hcal(i);
0521         h_hcalout_lut[key] = cdbttree_hcalout->getHisto(histoname);
0522       }
0523     }
0524   }
0525   return 0;
0526 }
0527 // process event procedure
0528 int CaloTriggerEmulator::process_event(PHCompositeNode *topNode)
0529 {
0530   if (Verbosity() >= 1)
0531   {
0532     std::cout << __FUNCTION__ << ": event " << m_nevent << std::endl;
0533   }
0534 
0535   // Get all nodes needed fo
0536   GetNodes(topNode);
0537 
0538   // process waveforms from the waveform container into primitives
0539   if (process_waveforms())
0540   {
0541     return Fun4AllReturnCodes::EVENT_OK;
0542   }
0543 
0544   if (Verbosity())
0545   {
0546     std::cout << __FILE__ << __FUNCTION__ << __LINE__ << "::"
0547               << "done with waveforms" << std::endl;
0548   }
0549   // process all the primitives into sums.
0550   process_primitives();
0551 
0552   // calculate the true LL1 trigger at emcal and hcal.
0553   if (process_organizer())
0554   {
0555     return Fun4AllReturnCodes::EVENT_OK;
0556   }
0557 
0558   // calculate the true LL1 trigger algorithm.
0559   if (process_trigger())
0560   {
0561     return Fun4AllReturnCodes::EVENT_OK;
0562   }
0563 
0564   m_nevent++;
0565 
0566   if (Verbosity() >= 2)
0567   {
0568     identify();
0569   }
0570 
0571   return Fun4AllReturnCodes::EVENT_OK;
0572 }
0573 
0574 // RESET event procedure that takes all variables to 0 and clears the primitives.
0575 int CaloTriggerEmulator::ResetEvent(PHCompositeNode * /*topNode*/)
0576 {
0577   // here, the peak minus pedestal map is cleanly disposed of
0578   m_peak_sub_ped_emcal.clear();
0579   m_peak_sub_ped_hcalin.clear();
0580   m_peak_sub_ped_hcalout.clear();
0581 
0582   return 0;
0583 }
0584 int CaloTriggerEmulator::process_offline()
0585 {
0586   int sample_start = 1;
0587   int sample_end = m_nsamples;
0588   if (m_trig_sample > 0)
0589   {
0590     sample_start = m_trig_sample;
0591     sample_end = m_trig_sample + 1;
0592   }
0593 
0594   if (m_do_emcal)
0595   {
0596     if (Verbosity())
0597     {
0598       std::cout << __FILE__ << "::" << __FUNCTION__ << ":: emcal" << std::endl;
0599     }
0600 
0601     if (!m_emcal_packets)
0602     {
0603       return Fun4AllReturnCodes::ABORTRUN;
0604     }
0605     unsigned int iwave = 0;
0606 
0607     for (int pid = m_packet_low_emcal; pid <= m_packet_high_emcal; pid++)
0608     {
0609       CaloPacket *packet = m_emcal_packets->getPacketbyId(pid);
0610       if (packet)
0611       {
0612         int nchannels = packet->iValue(0, "CHANNELS");
0613         unsigned int adc_skip_mask = 0;
0614 
0615         adc_skip_mask = cdbttree_adcmask->GetIntValue(pid, m_fieldname);
0616 
0617         for (int channel = 0; channel < nchannels; channel++)
0618         {
0619           if (channel % 64 == 0)
0620           {
0621             unsigned int adcboard = (unsigned int) channel / 64;
0622             if ((adc_skip_mask >> adcboard) & 0x1U)
0623             {
0624               for (int iskip = 0; iskip < 64; iskip++)
0625               {
0626                 std::vector<unsigned int> v_peak_sub_ped;
0627                 for (int i = sample_start; i < sample_end; i++)
0628                 {
0629                   v_peak_sub_ped.push_back(0);
0630                 }
0631                 unsigned int key = TowerInfoDefs::encode_emcal(iwave);
0632                 m_peak_sub_ped_emcal[key] = v_peak_sub_ped;
0633                 iwave++;
0634               }
0635             }
0636           }
0637           std::vector<unsigned int> v_peak_sub_ped;
0638           if (packet->iValue(channel, "SUPPRESSED"))
0639           {
0640             for (int i = sample_start; i < sample_end; i++)
0641             {
0642               v_peak_sub_ped.push_back(0);
0643             }
0644           }
0645           else
0646           {
0647             for (int i = sample_start; i < sample_end; i++)
0648             {
0649               int16_t maxim = (packet->iValue(i, channel) > packet->iValue(i + 1, channel) ? packet->iValue(i, channel) : packet->iValue(i + 1, channel));
0650               maxim = (maxim > packet->iValue(i + 2, channel) ? maxim : packet->iValue(i + 2, channel));
0651               uint16_t sam = 0;
0652               if (i >= m_trig_sub_delay)
0653               {
0654                 sam = i - m_trig_sub_delay;
0655               }
0656               else
0657               {
0658                 sam = 0;
0659               }
0660               unsigned int sub = 0;
0661               if (maxim > packet->iValue(sam, channel))
0662               {
0663                 sub = (((uint16_t) (maxim - packet->iValue(sam, channel))) & 0x3fffU);
0664               }
0665 
0666               v_peak_sub_ped.push_back(sub);
0667             }
0668           }
0669           unsigned int key = TowerInfoDefs::encode_emcal(iwave);
0670           m_peak_sub_ped_emcal[key] = v_peak_sub_ped;
0671           iwave++;
0672         }
0673         if (nchannels < 192 && !(adc_skip_mask < 4))
0674         {
0675           for (int iskip = 0; iskip < 192 - nchannels; iskip++)
0676           {
0677             std::vector<unsigned int> v_peak_sub_ped;
0678             for (int i = sample_start; i < sample_end; i++)
0679             {
0680               v_peak_sub_ped.push_back(0);
0681             }
0682             unsigned int key = TowerInfoDefs::encode_emcal(iwave);
0683             m_peak_sub_ped_emcal[key] = v_peak_sub_ped;
0684             iwave++;
0685           }
0686         }
0687       }
0688     }
0689   }
0690   if (m_do_hcalout)
0691   {
0692     if (Verbosity())
0693     {
0694       std::cout << __FILE__ << "::" << __FUNCTION__ << ":: ohcal" << std::endl;
0695     }
0696 
0697     if (!m_hcal_packets)
0698     {
0699       return Fun4AllReturnCodes::ABORTRUN;
0700     }
0701 
0702     unsigned int iwave = 0;
0703     for (int pid = m_packet_low_hcalout; pid <= m_packet_high_hcalout; pid++)
0704     {
0705       CaloPacket *packet = m_hcal_packets->getPacketbyId(pid);
0706       if (packet)
0707       {
0708         int nchannels = packet->iValue(0, "CHANNELS");
0709 
0710         for (int channel = 0; channel < nchannels; channel++)
0711         {
0712           std::vector<unsigned int> v_peak_sub_ped;
0713           if (packet->iValue(channel, "SUPPRESSED"))
0714           {
0715             for (int i = sample_start; i < sample_end; i++)
0716             {
0717               v_peak_sub_ped.push_back(0);
0718             }
0719           }
0720           else
0721           {
0722             for (int i = sample_start; i < sample_end; i++)
0723             {
0724               int16_t maxim = (packet->iValue(i, channel) > packet->iValue(i + 1, channel) ? packet->iValue(i, channel) : packet->iValue(i + 1, channel));
0725               maxim = (maxim > packet->iValue(i + 2, channel) ? maxim : packet->iValue(i + 2, channel));
0726               uint16_t sam = 0;
0727               if (i >= m_trig_sub_delay)
0728               {
0729                 sam = i - m_trig_sub_delay;
0730               }
0731               else
0732               {
0733                 sam = 0;
0734               }
0735               unsigned int sub = 0;
0736               if (maxim > packet->iValue(sam, channel))
0737               {
0738                 sub = (((uint16_t) (maxim - packet->iValue(sam, channel))) & 0x3fffU);
0739               }
0740 
0741               v_peak_sub_ped.push_back(sub);
0742             }
0743           }
0744           unsigned int key = TowerInfoDefs::encode_hcal(iwave);
0745           m_peak_sub_ped_hcalout[key] = v_peak_sub_ped;
0746           iwave++;
0747         }
0748       }
0749     }
0750   }
0751   if (m_do_hcalin)
0752   {
0753     if (Verbosity())
0754     {
0755       std::cout << __FILE__ << "::" << __FUNCTION__ << ":: ohcal" << std::endl;
0756     }
0757     if (!m_hcal_packets)
0758     {
0759       return Fun4AllReturnCodes::ABORTRUN;
0760     }
0761     unsigned int iwave = 0;
0762     for (int pid = m_packet_low_hcalin; pid <= m_packet_high_hcalin; pid++)
0763     {
0764       CaloPacket *packet = m_hcal_packets->getPacketbyId(pid);
0765       if (packet)
0766       {
0767         int nchannels = packet->iValue(0, "CHANNELS");
0768 
0769         for (int channel = 0; channel < nchannels; channel++)
0770         {
0771           std::vector<unsigned int> v_peak_sub_ped;
0772           if (packet->iValue(channel, "SUPPRESSED"))
0773           {
0774             for (int i = sample_start; i < sample_end; i++)
0775             {
0776               v_peak_sub_ped.push_back(0);
0777             }
0778           }
0779           else
0780           {
0781             for (int i = sample_start; i < sample_end; i++)
0782             {
0783               int16_t maxim = (packet->iValue(i, channel) > packet->iValue(i + 1, channel) ? packet->iValue(i, channel) : packet->iValue(i + 1, channel));
0784               maxim = (maxim > packet->iValue(i + 2, channel) ? maxim : packet->iValue(i + 2, channel));
0785               uint16_t sam = 0;
0786               if (i >= m_trig_sub_delay)
0787               {
0788                 sam = i - m_trig_sub_delay;
0789               }
0790               else
0791               {
0792                 sam = 0;
0793               }
0794               unsigned int sub = 0;
0795               if (maxim > packet->iValue(sam, channel))
0796               {
0797                 sub = (((uint16_t) (maxim - packet->iValue(sam, channel))) & 0x3fffU);
0798               }
0799 
0800               v_peak_sub_ped.push_back(sub);
0801             }
0802           }
0803           unsigned int key = TowerInfoDefs::encode_hcal(iwave);
0804           m_peak_sub_ped_hcalin[key] = v_peak_sub_ped;
0805           iwave++;
0806         }
0807       }
0808     }
0809   }
0810   return Fun4AllReturnCodes::EVENT_OK;
0811 }
0812 int CaloTriggerEmulator::process_waveforms()
0813 {
0814   if (!m_isdata)
0815   {
0816     return process_sim();
0817   }
0818 
0819   if (m_useoffline)
0820   {
0821     return process_offline();
0822   }
0823 
0824   if (m_event == nullptr)
0825   {
0826     std::cout << PHWHERE << " Event not found" << std::endl;
0827     return Fun4AllReturnCodes::ABORTEVENT;
0828   }
0829 
0830   if (m_event->getEvtType() != DATAEVENT)
0831   {
0832     return Fun4AllReturnCodes::ABORTEVENT;
0833   }
0834 
0835   // Get range of waveforms
0836   if (Verbosity())
0837   {
0838     std::cout << __FILE__ << "::" << __FUNCTION__ << ":: Processing waveforms" << std::endl;
0839   }
0840 
0841   int sample_start = 1;
0842   int sample_end = m_nsamples;
0843   if (m_trig_sample > 0)
0844   {
0845     sample_start = m_trig_sample;
0846     sample_end = m_trig_sample + 1;
0847   }
0848 
0849   if (m_do_emcal)
0850   {
0851     if (Verbosity())
0852     {
0853       std::cout << __FILE__ << "::" << __FUNCTION__ << ":: emcal" << std::endl;
0854     }
0855     unsigned int iwave = 0;
0856     for (int pid = m_packet_low_emcal; pid <= m_packet_high_emcal; pid++)
0857     {
0858       Packet *packet = m_event->getPacket(pid);
0859       if (packet)
0860       {
0861         int nchannels = packet->iValue(0, "CHANNELS");
0862         unsigned int adc_skip_mask = 0;
0863 
0864         adc_skip_mask = cdbttree_adcmask->GetIntValue(pid, m_fieldname);
0865 
0866         for (int channel = 0; channel < nchannels; channel++)
0867         {
0868           if (channel % 64 == 0)
0869           {
0870             unsigned int adcboard = (unsigned int) channel / 64;
0871             if ((adc_skip_mask >> adcboard) & 0x1U)
0872             {
0873               for (int iskip = 0; iskip < 64; iskip++)
0874               {
0875                 std::vector<unsigned int> v_peak_sub_ped;
0876                 for (int i = sample_start; i < sample_end; i++)
0877                 {
0878                   v_peak_sub_ped.push_back(0);
0879                 }
0880                 unsigned int key = TowerInfoDefs::encode_emcal(iwave);
0881                 m_peak_sub_ped_emcal[key] = v_peak_sub_ped;
0882                 iwave++;
0883               }
0884               continue;
0885             }
0886           }
0887           std::vector<unsigned int> v_peak_sub_ped;
0888           if (packet->iValue(channel, "SUPPRESSED"))
0889           {
0890             for (int i = sample_start; i < sample_end; i++)
0891             {
0892               v_peak_sub_ped.push_back(0);
0893             }
0894           }
0895           else
0896           {
0897             for (int i = sample_start; i < sample_end; i++)
0898             {
0899               int16_t maxim = (packet->iValue(i, channel) > packet->iValue(i + 1, channel) ? packet->iValue(i, channel) : packet->iValue(i + 1, channel));
0900               maxim = (maxim > packet->iValue(i + 2, channel) ? maxim : packet->iValue(i + 2, channel));
0901               uint16_t sam = 0;
0902               if (i >= m_trig_sub_delay)
0903               {
0904                 sam = i - m_trig_sub_delay;
0905               }
0906               else
0907               {
0908                 sam = 0;
0909               }
0910               unsigned int sub = 0;
0911               if (maxim > packet->iValue(sam, channel))
0912               {
0913                 sub = (((uint16_t) (maxim - packet->iValue(sam, channel))) & 0x3fffU);
0914               }
0915 
0916               v_peak_sub_ped.push_back(sub);
0917             }
0918           }
0919           unsigned int key = TowerInfoDefs::encode_emcal(iwave);
0920           m_peak_sub_ped_emcal[key] = v_peak_sub_ped;
0921           iwave++;
0922         }
0923       }
0924       delete packet;
0925     }
0926   }
0927   if (m_do_hcalout)
0928   {
0929     if (Verbosity())
0930     {
0931       std::cout << __FILE__ << "::" << __FUNCTION__ << ":: ohcal" << std::endl;
0932     }
0933 
0934     unsigned int iwave = 0;
0935     for (int pid = m_packet_low_hcalout; pid <= m_packet_high_hcalout; pid++)
0936     {
0937       Packet *packet = m_event->getPacket(pid);
0938       if (packet)
0939       {
0940         int nchannels = packet->iValue(0, "CHANNELS");
0941 
0942         for (int channel = 0; channel < nchannels; channel++)
0943         {
0944           std::vector<unsigned int> v_peak_sub_ped;
0945           if (packet->iValue(channel, "SUPPRESSED"))
0946           {
0947             for (int i = sample_start; i < sample_end; i++)
0948             {
0949               v_peak_sub_ped.push_back(0);
0950             }
0951           }
0952           else
0953           {
0954             for (int i = sample_start; i < sample_end; i++)
0955             {
0956               int16_t maxim = (packet->iValue(i, channel) > packet->iValue(i + 1, channel) ? packet->iValue(i, channel) : packet->iValue(i + 1, channel));
0957               maxim = (maxim > packet->iValue(i + 2, channel) ? maxim : packet->iValue(i + 2, channel));
0958               uint16_t sam = 0;
0959               if (i >= m_trig_sub_delay)
0960               {
0961                 sam = i - m_trig_sub_delay;
0962               }
0963               else
0964               {
0965                 sam = 0;
0966               }
0967               unsigned int sub = 0;
0968               if (maxim > packet->iValue(sam, channel))
0969               {
0970                 sub = (((uint16_t) (maxim - packet->iValue(sam, channel))) & 0x3fffU);
0971               }
0972 
0973               v_peak_sub_ped.push_back(sub);
0974             }
0975           }
0976           unsigned int key = TowerInfoDefs::encode_hcal(iwave);
0977           m_peak_sub_ped_hcalout[key] = v_peak_sub_ped;
0978           iwave++;
0979         }
0980       }
0981       delete packet;
0982     }
0983   }
0984   if (m_do_hcalin)
0985   {
0986     if (Verbosity())
0987     {
0988       std::cout << __FILE__ << "::" << __FUNCTION__ << ":: ohcal" << std::endl;
0989     }
0990 
0991     unsigned int iwave = 0;
0992     for (int pid = m_packet_low_hcalin; pid <= m_packet_high_hcalin; pid++)
0993     {
0994       Packet *packet = m_event->getPacket(pid);
0995       if (packet)
0996       {
0997         int nchannels = packet->iValue(0, "CHANNELS");
0998 
0999         for (int channel = 0; channel < nchannels; channel++)
1000         {
1001           std::vector<unsigned int> v_peak_sub_ped;
1002           if (packet->iValue(channel, "SUPPRESSED"))
1003           {
1004             for (int i = sample_start; i < sample_end; i++)
1005             {
1006               v_peak_sub_ped.push_back(0);
1007             }
1008           }
1009           else
1010           {
1011             for (int i = sample_start; i < sample_end; i++)
1012             {
1013               int16_t maxim = (packet->iValue(i, channel) > packet->iValue(i + 1, channel) ? packet->iValue(i, channel) : packet->iValue(i + 1, channel));
1014               maxim = (maxim > packet->iValue(i + 2, channel) ? maxim : packet->iValue(i + 2, channel));
1015               uint16_t sam = 0;
1016               if (i >= m_trig_sub_delay)
1017               {
1018                 sam = i - m_trig_sub_delay;
1019               }
1020               else
1021               {
1022                 sam = 0;
1023               }
1024               unsigned int sub = 0;
1025               if (maxim > packet->iValue(sam, channel))
1026               {
1027                 sub = (((uint16_t) (maxim - packet->iValue(sam, channel))) & 0x3fffU);
1028               }
1029 
1030               v_peak_sub_ped.push_back(sub);
1031             }
1032           }
1033           unsigned int key = TowerInfoDefs::encode_hcal(iwave);
1034           m_peak_sub_ped_hcalin[key] = v_peak_sub_ped;
1035           iwave++;
1036         }
1037       }
1038       delete packet;
1039     }
1040   }
1041 
1042   return Fun4AllReturnCodes::EVENT_OK;
1043 }
1044 
1045 int CaloTriggerEmulator::process_sim()
1046 {
1047   // Get range of waveforms
1048   if (Verbosity())
1049   {
1050     std::cout << __FILE__ << "::" << __FUNCTION__ << ":: Processing waveforms" << std::endl;
1051   }
1052 
1053   int sample_start = 1;
1054   int sample_end = m_nsamples;
1055   if (m_trig_sample > 0)
1056   {
1057     sample_start = m_trig_sample;
1058     sample_end = m_trig_sample + 1;
1059   }
1060 
1061   if (m_do_emcal)
1062   {
1063     if (Verbosity())
1064     {
1065       std::cout << __FILE__ << "::" << __FUNCTION__ << ":: emcal" << std::endl;
1066     }
1067     if (!m_waveforms_emcal->size())
1068     {
1069       return Fun4AllReturnCodes::EVENT_OK;
1070     }
1071     // for each waveform, clauclate the peak - pedestal given the sub-delay setting
1072     for (unsigned int iwave = 0; iwave < (unsigned int) m_waveforms_emcal->size(); iwave++)
1073     {
1074       std::vector<unsigned int> v_peak_sub_ped;
1075       TowerInfo *tower = m_waveforms_emcal->get_tower_at_channel(iwave);
1076       unsigned int key = TowerInfoDefs::encode_emcal(iwave);
1077       if (tower->get_isZS())
1078       {
1079         for (int i = sample_start; i < sample_end; i++)
1080         {
1081           v_peak_sub_ped.push_back(0);
1082         }
1083       }
1084       else
1085       {
1086         for (int i = sample_start; i < sample_end; i++)
1087         {
1088           int16_t maxim = (tower->get_waveform_value(i) > tower->get_waveform_value(i + 1) ? tower->get_waveform_value(i) : tower->get_waveform_value(i + 1));
1089           maxim = (maxim > tower->get_waveform_value(i + 2) ? maxim : tower->get_waveform_value(i + 2));
1090           uint16_t sam = 0;
1091           if (i >= m_trig_sub_delay)
1092           {
1093             sam = i - m_trig_sub_delay;
1094           }
1095           else
1096           {
1097             sam = 0;
1098           }
1099           unsigned int sub = 0;
1100           if (maxim > tower->get_waveform_value(sam))
1101           {
1102             sub = (((uint16_t) (maxim - tower->get_waveform_value(sam))) & 0x3fffU);
1103           }
1104 
1105           v_peak_sub_ped.push_back(sub);
1106         }
1107       }
1108       // save in global.
1109       m_peak_sub_ped_emcal[key] = v_peak_sub_ped;
1110     }
1111   }
1112   if (m_do_hcalout)
1113   {
1114     if (Verbosity())
1115     {
1116       std::cout << __FILE__ << "::" << __FUNCTION__ << ":: ohcal" << std::endl;
1117     }
1118 
1119     std::vector<int> wave;
1120     // for each waveform, clauclate the peak - pedestal given the sub-delay setting
1121     if (!m_waveforms_hcalout->size())
1122     {
1123       return Fun4AllReturnCodes::EVENT_OK;
1124     }
1125 
1126     for (unsigned int iwave = 0; iwave < (unsigned int) m_waveforms_hcalout->size(); iwave++)
1127     {
1128       std::vector<unsigned int> v_peak_sub_ped;
1129       TowerInfo *tower = m_waveforms_hcalout->get_tower_at_channel(iwave);
1130       unsigned int key = TowerInfoDefs::encode_hcal(iwave);
1131       if (tower->get_isZS())
1132       {
1133         for (int i = sample_start; i < sample_end; i++)
1134         {
1135           v_peak_sub_ped.push_back(0);
1136         }
1137       }
1138       else
1139       {
1140         for (int i = sample_start; i < sample_end; i++)
1141         {
1142           int16_t maxim = (tower->get_waveform_value(i) > tower->get_waveform_value(i + 1) ? tower->get_waveform_value(i) : tower->get_waveform_value(i + 1));
1143           maxim = (maxim > tower->get_waveform_value(i + 2) ? maxim : tower->get_waveform_value(i + 2));
1144           uint16_t sam = 0;
1145           if (i >= m_trig_sub_delay)
1146           {
1147             sam = i - m_trig_sub_delay;
1148           }
1149           else
1150           {
1151             sam = 0;
1152           }
1153           unsigned int sub = 0;
1154           if (maxim > tower->get_waveform_value(sam))
1155           {
1156             sub = (((uint16_t) (maxim - tower->get_waveform_value(sam))) & 0x3fffU);
1157           }
1158 
1159           v_peak_sub_ped.push_back(sub);
1160         }
1161       }
1162       // save in global.
1163       m_peak_sub_ped_hcalout[key] = v_peak_sub_ped;
1164     }
1165   }
1166   if (m_do_hcalin)
1167   {
1168     if (Verbosity())
1169     {
1170       std::cout << __FILE__ << "::" << __FUNCTION__ << ":: ihcal" << std::endl;
1171     }
1172     if (!m_waveforms_hcalin->size())
1173     {
1174       return Fun4AllReturnCodes::EVENT_OK;
1175     }
1176     if (Verbosity())
1177     {
1178       std::cout << __FILE__ << "::" << __FUNCTION__ << ":: ihcal" << std::endl;
1179     }
1180 
1181     std::vector<unsigned int> wave;
1182 
1183     // for each waveform, clauclate the peak - pedestal given the sub-delay setting
1184     for (unsigned int iwave = 0; iwave < (unsigned int) m_waveforms_hcalin->size(); iwave++)
1185     {
1186       std::vector<unsigned int> v_peak_sub_ped;
1187       TowerInfo *tower = m_waveforms_hcalin->get_tower_at_channel(iwave);
1188       unsigned int key = TowerInfoDefs::encode_hcal(iwave);
1189       if (tower->get_isZS())
1190       {
1191         for (int i = sample_start; i < sample_end; i++)
1192         {
1193           v_peak_sub_ped.push_back(0);
1194         }
1195       }
1196       else
1197       {
1198         for (int i = sample_start; i < sample_end; i++)
1199         {
1200           int16_t maxim = (tower->get_waveform_value(i) > tower->get_waveform_value(i + 1) ? tower->get_waveform_value(i) : tower->get_waveform_value(i + 1));
1201           maxim = (maxim > tower->get_waveform_value(i + 2) ? maxim : tower->get_waveform_value(i + 2));
1202           uint16_t sam = 0;
1203           if (i >= m_trig_sub_delay)
1204           {
1205             sam = i - m_trig_sub_delay;
1206           }
1207           else
1208           {
1209             sam = 0;
1210           }
1211           unsigned int sub = 0;
1212           if (maxim > tower->get_waveform_value(sam))
1213           {
1214             sub = (((uint16_t) (maxim - tower->get_waveform_value(sam))) & 0x3fffU);
1215           }
1216 
1217           v_peak_sub_ped.push_back(sub);
1218         }
1219       }
1220       // save in global.
1221       m_peak_sub_ped_hcalin[key] = v_peak_sub_ped;
1222     }
1223   }
1224 
1225   return Fun4AllReturnCodes::EVENT_OK;
1226 }
1227 
1228 // procedure to process the peak - pedestal into primitives.
1229 int CaloTriggerEmulator::process_primitives()
1230 {
1231   int ip;
1232   int i;
1233   bool mask;
1234   int nsample = m_nsamples - 1;
1235   if (m_trig_sample > 0)
1236   {
1237     nsample = 1;
1238   }
1239 
1240   if (Verbosity())
1241   {
1242     std::cout << __FILE__ << "::" << __FUNCTION__ << ":: Processing primitives" << std::endl;
1243   }
1244 
1245   if (m_do_emcal)
1246   {
1247     if (Verbosity())
1248     {
1249       std::cout << __FILE__ << "::" << __FUNCTION__ << ":: Processing primitives:: emcal" << std::endl;
1250     }
1251 
1252     ip = 0;
1253 
1254     // get the number of primitives needed to process
1255     m_n_primitives = m_prim_map[TriggerDefs::DetectorId::emcalDId];
1256     for (i = 0; i < m_n_primitives; i++, ip++)
1257     {
1258       if (Verbosity())
1259       {
1260         std::cout << __FILE__ << "::" << __FUNCTION__ << ":: Processing primitives:: adding " << i << std::endl;
1261       }
1262       unsigned int tmp = 0;
1263       // get the primitive key of what we are making, in order of the packet ID and channel number
1264       TriggerDefs::TriggerPrimKey primkey = TriggerDefs::getTriggerPrimKey(TriggerDefs::GetTriggerId("NONE"), TriggerDefs::GetDetectorId("EMCAL"), TriggerDefs::GetPrimitiveId("EMCAL"), ip);
1265 
1266       TriggerPrimitive *primitive = m_primitives_emcal->get_primitive_at_key(primkey);
1267       unsigned int sum = 0;
1268       // check if masked Fiber;
1269       mask = CheckFiberMasks(primkey);
1270 
1271       // calculate 16 sums
1272       for (int isum = 0; isum < m_n_sums; isum++)
1273       {
1274         // get sum key
1275         TriggerDefs::TriggerSumKey sumkey = TriggerDefs::getTriggerSumKey(TriggerDefs::GetTriggerId("NONE"), TriggerDefs::GetDetectorId("EMCAL"), TriggerDefs::GetPrimitiveId("EMCAL"), ip, isum);
1276 
1277         // calculate sums for all samples, hense the vector.
1278         std::vector<unsigned int> *t_sum = primitive->get_sum_at_key(sumkey);
1279         t_sum->clear();
1280 
1281         // check to mask channel (if fiber masked, automatically mask the channel)
1282         bool mask_channel = mask || CheckChannelMasks(sumkey);
1283         for (int is = 0; is < nsample; is++)
1284         {
1285           sum = 0;
1286           unsigned int temp_sum = 0;
1287 
1288           // if masked, just fill with 0s
1289           if (!mask_channel)
1290           {
1291             for (int j = 0; j < 4; j++)
1292             {
1293               // unsigned int iwave = 64*ip + isum*4 + j;
1294               unsigned int key = TriggerDefs::GetTowerInfoKey(TriggerDefs::GetDetectorId("EMCAL"), ip, isum, j);
1295               unsigned int lut_input = (m_peak_sub_ped_emcal[key].at(is) >> 4U) & 0x3ffU;
1296               if (m_default_lut_emcal)
1297               {
1298                 tmp = (m_l1_adc_table[lut_input] >> 2U);
1299               }
1300               else
1301               {
1302                 unsigned int lut_output = ((unsigned int) h_emcal_lut[key]->GetBinContent(lut_input + 1)) & 0x3ffU;
1303                 tmp = (lut_output >> 2U);
1304               }
1305               temp_sum += (tmp & 0xffU);
1306             }
1307             sum = ((temp_sum & 0x3ffU) >> 2U) & 0xffU;
1308             if (Verbosity() >= 10 && sum >= 1)
1309             {
1310               std::cout << __FILE__ << "::" << __FUNCTION__ << ":: emcal sum " << sumkey << " = " << sum << std::endl;
1311             }
1312           }
1313           if (Verbosity())
1314           {
1315             std::cout << __FILE__ << "::" << __FUNCTION__ << ":: Processing primitives:: adding" << std::endl;
1316           }
1317 
1318           t_sum->push_back(sum);
1319         }
1320       }
1321     }
1322   }
1323   if (m_do_hcalout)
1324   {
1325     if (Verbosity())
1326     {
1327       std::cout << __FILE__ << "::" << __FUNCTION__ << ":: Processing primitives:: ohcal" << std::endl;
1328     }
1329 
1330     ip = 0;
1331 
1332     m_n_primitives = m_prim_map[TriggerDefs::DetectorId::hcaloutDId];
1333 
1334     for (i = 0; i < m_n_primitives; i++, ip++)
1335     {
1336       TriggerDefs::TriggerPrimKey primkey = TriggerDefs::getTriggerPrimKey(TriggerDefs::GetTriggerId("NONE"), TriggerDefs::GetDetectorId("HCALOUT"), TriggerDefs::GetPrimitiveId("HCALOUT"), ip);
1337       TriggerPrimitive *primitive = m_primitives_hcalout->get_primitive_at_key(primkey);
1338       unsigned int sum;
1339       mask = CheckFiberMasks(primkey);
1340       for (int isum = 0; isum < m_n_sums; isum++)
1341       {
1342         TriggerDefs::TriggerSumKey sumkey = TriggerDefs::getTriggerSumKey(TriggerDefs::GetTriggerId("NONE"), TriggerDefs::GetDetectorId("HCALOUT"), TriggerDefs::GetPrimitiveId("HCALOUT"), ip, isum);
1343         std::vector<unsigned int> *t_sum = primitive->get_sum_at_key(sumkey);
1344         mask |= CheckChannelMasks(sumkey);
1345         for (int is = 0; is < nsample; is++)
1346         {
1347           sum = 0;
1348           unsigned int temp_sum = 0;
1349           if (!mask)
1350           {
1351             for (int j = 0; j < 4; j++)
1352             {
1353               unsigned int key = TriggerDefs::GetTowerInfoKey(TriggerDefs::GetDetectorId("HCAL"), ip, isum, j);
1354               unsigned int lut_input = (m_peak_sub_ped_hcalout[key].at(is) >> 4U) & 0x3ffU;
1355               unsigned int tmp = 0;
1356               if (m_default_lut_hcalout)
1357               {
1358                 tmp = (m_l1_adc_table[lut_input] >> 2U);
1359               }
1360               else
1361               {
1362                 unsigned int lut_output = ((unsigned int) h_hcalout_lut[key]->GetBinContent(lut_input + 1)) & 0x3ffU;
1363                 tmp = (lut_output >> 2U);
1364               }
1365               temp_sum += (tmp & 0xffU);
1366             }
1367             sum = ((temp_sum & 0x3ffU) >> 2U) & 0xffU;
1368             if (Verbosity() >= 10 && sum >= 1)
1369             {
1370               std::cout << __FILE__ << "::" << __FUNCTION__ << ":: hcalout sum " << sumkey << " = " << sum << std::endl;
1371             }
1372           }
1373           t_sum->push_back(sum);
1374         }
1375       }
1376     }
1377   }
1378   if (m_do_hcalin)
1379   {
1380     ip = 0;
1381 
1382     if (Verbosity())
1383     {
1384       std::cout << __FILE__ << "::" << __FUNCTION__ << ":: Processing primitives:: ihcal" << std::endl;
1385     }
1386 
1387     m_n_primitives = m_prim_map[TriggerDefs::DetectorId::hcalinDId];
1388 
1389     for (i = 0; i < m_n_primitives; i++, ip++)
1390     {
1391       TriggerDefs::TriggerPrimKey primkey = TriggerDefs::getTriggerPrimKey(TriggerDefs::GetTriggerId("NONE"), TriggerDefs::GetDetectorId("HCALIN"), TriggerDefs::GetPrimitiveId("HCALIN"), ip);
1392       TriggerPrimitive *primitive = m_primitives_hcalin->get_primitive_at_key(primkey);
1393       unsigned int sum;
1394       mask = CheckFiberMasks(primkey);
1395       for (int isum = 0; isum < m_n_sums; isum++)
1396       {
1397         TriggerDefs::TriggerSumKey sumkey = TriggerDefs::getTriggerSumKey(TriggerDefs::GetTriggerId("NONE"), TriggerDefs::GetDetectorId("HCALIN"), TriggerDefs::GetPrimitiveId("HCALIN"), ip, isum);
1398         std::vector<unsigned int> *t_sum = primitive->get_sum_at_key(sumkey);
1399         mask |= CheckChannelMasks(sumkey);
1400         for (int is = 0; is < nsample; is++)
1401         {
1402           sum = 0;
1403           unsigned int temp_sum = 0;
1404           if (!mask)
1405           {
1406             for (int j = 0; j < 4; j++)
1407             {
1408               unsigned int key = TriggerDefs::GetTowerInfoKey(TriggerDefs::GetDetectorId("HCAL"), ip, isum, j);
1409               unsigned int lut_input = (m_peak_sub_ped_hcalin[key].at(is) >> 4U) & 0x3ffU;
1410               unsigned int tmp = 0;
1411               if (m_default_lut_hcalin)
1412               {
1413                 tmp = (m_l1_adc_table[lut_input] >> 2U);
1414               }
1415               else
1416               {
1417                 unsigned int lut_output = ((unsigned int) h_hcalin_lut[key]->GetBinContent(lut_input + 1)) & 0x3ffU;
1418                 tmp = (lut_output >> 2U);
1419               }
1420               temp_sum += (tmp & 0x3ffU);
1421             }
1422             sum = ((temp_sum & 0xfffU) >> 2U) & 0xffU;
1423             if (Verbosity() >= 10 && sum >= 1)
1424             {
1425               std::cout << __FILE__ << "::" << __FUNCTION__ << ":: hcalin sum " << sumkey << " = " << sum << std::endl;
1426             }
1427           }
1428           t_sum->push_back(sum);
1429         }
1430       }
1431     }
1432   }
1433 
1434   return Fun4AllReturnCodes::EVENT_OK;
1435 }
1436 
1437 // Unless this is the MBD or HCAL Cosmics trigger, EMCAL and HCAL will go through here.
1438 // This creates the 8x8 non-overlapping sum and the 4x4 overlapping sum.
1439 
1440 int CaloTriggerEmulator::process_organizer()
1441 {
1442   TriggerPrimitiveContainer::Range range;
1443   if (Verbosity())
1444   {
1445     std::cout << __FILE__ << "::" << __FUNCTION__ << ":: Processing organizer" << std::endl;
1446   }
1447 
1448   int nsample = m_nsamples - 1;
1449   // bits are to say whether the trigger has fired. this is what is sent to the GL1
1450   if (m_trig_sample > 0)
1451   {
1452     nsample = 1;
1453   }
1454   // 8x8 non-overlapping sums in the EMCAL
1455   // create the 8x8 non-overlapping sum
1456   {
1457     if (Verbosity() >= 2)
1458     {
1459       std::cout << __FUNCTION__ << " " << __LINE__ << " processing 8x8 non-overlapping sums" << std::endl;
1460     }
1461 
1462     m_triggerid = TriggerDefs::TriggerId::jetTId;
1463 
1464     if (!m_primitives_emcal)
1465     {
1466       std::cout << "There is no primitive container" << std::endl;
1467       return Fun4AllReturnCodes::EVENT_OK;
1468     }
1469 
1470     range = m_primitives_emcal_ll1->getTriggerPrimitives();
1471     for (TriggerPrimitiveContainerv1::Iter iter = range.first; iter != range.second; ++iter)
1472     {
1473       TriggerPrimitivev1::Range sumrange = iter->second->getSums();
1474       for (TriggerPrimitivev1::Iter siter = sumrange.first; siter != sumrange.second; ++siter)
1475       {
1476         for (int is = 0; is < nsample; is++)
1477         {
1478           siter->second->push_back(0);
1479         }
1480       }
1481     }
1482 
1483     if (Verbosity())
1484     {
1485       std::cout << __FILE__ << "::" << __FUNCTION__ << ":: Processing getting emcal prims" << std::endl;
1486     }
1487 
1488     // iterate through emcal primitives and organize into the 16 jet primitives each with the 8x8 nonoverlapping sum
1489     range = m_primitives_emcal->getTriggerPrimitives();
1490 
1491     for (TriggerPrimitiveContainerv1::Iter iter = range.first; iter != range.second; ++iter)
1492     {
1493       // get key and see if masked
1494       TriggerDefs::TriggerPrimKey key = (*iter).first;
1495       if (CheckFiberMasks(key))
1496       {
1497         continue;
1498       }
1499 
1500       uint16_t sumphi = TriggerDefs::getPrimitivePhiId_from_TriggerPrimKey(key);
1501       uint16_t sumeta = TriggerDefs::getPrimitiveEtaId_from_TriggerPrimKey(key);
1502 
1503       // based on where the primitive is in the detector, the location of the jet primitive is determined, 0 through 15 in phi.
1504       uint16_t iprim = sumphi / 2;
1505       // eta determines the location of the sum within the jet primitive.
1506       uint16_t isum = (sumeta + ((sumphi % 2) * 12));
1507 
1508       TriggerDefs::TriggerPrimKey jet_prim_key = TriggerDefs::getTriggerPrimKey(m_triggerid, TriggerDefs::GetDetectorId("EMCAL"), TriggerDefs::GetPrimitiveId("JET"), iprim);
1509 
1510       TriggerDefs::TriggerPrimKey jet_sum_key = TriggerDefs::getTriggerSumKey(m_triggerid, TriggerDefs::GetDetectorId("EMCAL"), TriggerDefs::GetPrimitiveId("JET"), iprim, isum);
1511 
1512       // add to primitive previously made the sum of the 8x8 non-overlapping sum.
1513       std::vector<unsigned int> *t_sum = m_primitives_emcal_ll1->get_primitive_at_key(jet_prim_key)->get_sum_at_key(jet_sum_key);
1514 
1515       // get the primitive (16 2x2 sums)
1516       TriggerPrimitive *primitive = (*iter).second;
1517       TriggerPrimitivev1::Range sumrange = primitive->getSums();
1518 
1519       // iterate through all 16 sums and add together
1520       for (TriggerPrimitivev1::Iter iter_sum = sumrange.first; iter_sum != sumrange.second; ++iter_sum)
1521       {
1522         if (CheckChannelMasks(iter_sum->first))
1523         {
1524           continue;
1525         }
1526         int i = 0;
1527         for (unsigned int &it_s : *(*iter_sum).second)
1528         {
1529           t_sum->at(i) += (it_s & 0xffU);
1530           i++;
1531         }
1532       }
1533 
1534       // bit shift by 16 (divide by the 16 towers) to get an 8 bit energy sum.
1535       for (unsigned int &it_s : *t_sum)
1536       {
1537         // unsigned int sumshift = ((it_s >> 0x2U) & 0x3ffU);
1538         // unsigned int sum_lower = ( sumshift & 0x7fU );
1539         // unsigned int sum_higher = ( ( sumshift >> 0x7U ) > 0 ? 0x1U : 0x0U );
1540         // it_s = m_l1_8x8_table[sumshift];
1541         it_s = std::min(it_s, 0xffU);
1542       }
1543     }
1544   }
1545   {
1546     if (Verbosity())
1547     {
1548       std::cout << __FILE__ << "::" << __FUNCTION__ << ":: Processing HCAL" << std::endl;
1549     }
1550 
1551     range = m_primitives_hcal_ll1->getTriggerPrimitives();
1552     for (TriggerPrimitiveContainerv1::Iter iter = range.first; iter != range.second; ++iter)
1553     {
1554       TriggerPrimitivev1::Range sumrange = iter->second->getSums();
1555       for (TriggerPrimitivev1::Iter siter = sumrange.first; siter != sumrange.second; ++siter)
1556       {
1557         for (int is = 0; is < nsample; is++)
1558         {
1559           siter->second->push_back(0);
1560         }
1561       }
1562     }
1563 
1564     if (m_primitives_hcalin)
1565     {
1566       if (Verbosity())
1567       {
1568         std::cout << __FILE__ << "::" << __FUNCTION__ << ":: Processing organizer" << std::endl;
1569       }
1570 
1571       // iterate through emcal primitives and organize into the 16 jet primitives each with the 8x8 nonoverlapping sum
1572       range = m_primitives_hcalin->getTriggerPrimitives();
1573 
1574       // for hcalin: there are
1575       for (TriggerPrimitiveContainerv1::Iter iter = range.first; iter != range.second; ++iter)
1576       {
1577         // get key and see if masked
1578         TriggerDefs::TriggerPrimKey key = (*iter).first;
1579         if (CheckFiberMasks(key))
1580         {
1581           continue;
1582         }
1583 
1584         // get the primitive (16 2x2 sums)
1585         TriggerPrimitive *primitive = (*iter).second;
1586         TriggerPrimitivev1::Range sumrange = primitive->getSums();
1587 
1588         for (TriggerPrimitivev1::Iter iter_sum = sumrange.first; iter_sum != sumrange.second; ++iter_sum)
1589         {
1590           TriggerDefs::TriggerSumKey sumkey = (*iter_sum).first;
1591           uint16_t sumphi = (TriggerDefs::getPrimitivePhiId_from_TriggerSumKey(sumkey) * 4) + TriggerDefs::getSumPhiId(sumkey);
1592           uint16_t sumeta = (TriggerDefs::getPrimitiveEtaId_from_TriggerSumKey(sumkey) * 4) + TriggerDefs::getSumEtaId(sumkey);
1593 
1594           int i = 0;
1595           if (CheckChannelMasks(sumkey))
1596           {
1597             continue;
1598           }
1599           // based on where the primitive is in the detector, the location of the jet primitive is determined, 0 through 15 in phi.
1600           uint16_t iprim = sumphi / 2;
1601           // eta determines the location of the sum within the jet primitive.
1602           uint16_t isum = (sumeta + ((sumphi % 2) * 12));
1603 
1604           TriggerDefs::TriggerPrimKey jet_prim_key = TriggerDefs::getTriggerPrimKey(m_triggerid, TriggerDefs::GetDetectorId("HCAL"), TriggerDefs::GetPrimitiveId("JET"), iprim);
1605 
1606           TriggerDefs::TriggerPrimKey jet_sum_key = TriggerDefs::getTriggerSumKey(m_triggerid, TriggerDefs::GetDetectorId("HCAL"), TriggerDefs::GetPrimitiveId("JET"), iprim, isum);
1607 
1608           // add to primitive previously made the sum of the 8x8 non-overlapping sum.
1609           std::vector<unsigned int> *t_sum = m_primitives_hcal_ll1->get_primitive_at_key(jet_prim_key)->get_sum_at_key(jet_sum_key);
1610 
1611           for (unsigned int &it_s : *(*iter_sum).second)
1612           {
1613             t_sum->at(i) += (it_s & 0xffU);
1614             i++;
1615           }
1616         }
1617       }
1618     }
1619 
1620     if (m_primitives_hcalout)
1621     {
1622       if (Verbosity())
1623       {
1624         std::cout << __FILE__ << "::" << __FUNCTION__ << ":: Processing organizer" << std::endl;
1625       }
1626 
1627       // iterate through emcal primitives and organize into the 16 jet primitives each with the 8x8 nonoverlapping sum
1628       range = m_primitives_hcalout->getTriggerPrimitives();
1629 
1630       // for hcalin: there are
1631       for (TriggerPrimitiveContainerv1::Iter iter = range.first; iter != range.second; ++iter)
1632       {
1633         // get key and see if masked
1634         TriggerDefs::TriggerPrimKey key = (*iter).first;
1635         if (CheckFiberMasks(key))
1636         {
1637           continue;
1638         }
1639 
1640         // get the primitive (16 2x2 sums)
1641         TriggerPrimitive *primitive = (*iter).second;
1642         TriggerPrimitivev1::Range sumrange = primitive->getSums();
1643 
1644         for (TriggerPrimitivev1::Iter iter_sum = sumrange.first; iter_sum != sumrange.second; ++iter_sum)
1645         {
1646           TriggerDefs::TriggerSumKey sumkey = (*iter_sum).first;
1647           uint16_t sumphi = (TriggerDefs::getPrimitivePhiId_from_TriggerSumKey(sumkey) * 4) + TriggerDefs::getSumPhiId(sumkey);
1648           //              uint16_t sumeta = TriggerDefs::getPrimitiveEtaId_from_TriggerSumKey(sumkey)*4 + TriggerDefs::getSumEtaId(sumkey);
1649           uint16_t sumeta = (TriggerDefs::getPrimitiveEtaId_from_TriggerSumKey(sumkey) * 4) + TriggerDefs::getSumEtaId(sumkey);
1650 
1651           int i = 0;
1652 
1653           if (CheckChannelMasks(sumkey))
1654           {
1655             continue;
1656           }
1657           // based on where the primitive is in the detector, the location of the jet primitive is determined, 0 through 15 in phi.
1658           uint16_t iprim = sumphi / 2;
1659           // eta determines the location of the sum within the jet primitive.
1660           uint16_t isum = sumeta + ((sumphi % 2) * 12);
1661           TriggerDefs::TriggerPrimKey jet_prim_key = TriggerDefs::getTriggerPrimKey(TriggerDefs::TriggerId::jetTId, TriggerDefs::GetDetectorId("HCAL"), TriggerDefs::GetPrimitiveId("JET"), iprim);
1662 
1663           TriggerDefs::TriggerPrimKey jet_sum_key = TriggerDefs::getTriggerSumKey(TriggerDefs::TriggerId::jetTId, TriggerDefs::GetDetectorId("HCAL"), TriggerDefs::GetPrimitiveId("JET"), iprim, isum);
1664           if (Verbosity())
1665           {
1666             std::cout << __FILE__ << "::" << __FUNCTION__ << "::" << __LINE__ << ":: Processing organizer" << std::endl;
1667           }
1668 
1669           std::vector<unsigned int> *t_sum = m_primitives_hcal_ll1->get_primitive_at_key(jet_prim_key)->get_sum_at_key(jet_sum_key);
1670           for (unsigned int &it_s : *(*iter_sum).second)
1671           {
1672             t_sum->at(i) += ((it_s) & 0xffU);
1673             i++;
1674           }
1675         }
1676       }
1677     }
1678 
1679     // iterate through emcal primitives and organize into the 16 jet primitives each with the 8x8 nonoverlapping sum
1680     if (Verbosity())
1681     {
1682       std::cout << __FILE__ << "::" << __FUNCTION__ << "::" << __LINE__ << ":: Processing organizer" << std::endl;
1683     }
1684     range = m_primitives_hcal_ll1->getTriggerPrimitives();
1685     for (TriggerPrimitiveContainerv1::Iter iter = range.first; iter != range.second; ++iter)
1686     {
1687       TriggerPrimitive *primitive = (*iter).second;
1688       TriggerPrimitivev1::Range sumrange = primitive->getSums();
1689 
1690       for (TriggerPrimitivev1::Iter iter_sum = sumrange.first; iter_sum != sumrange.second; ++iter_sum)
1691       {
1692         for (unsigned int &it_s : *(*iter_sum).second)
1693         {
1694           it_s = (it_s >> 1U) & 0xffU;
1695         }
1696       }
1697     }
1698 
1699     {
1700       range = m_primitives_jet->getTriggerPrimitives();
1701       for (TriggerPrimitiveContainerv1::Iter iter = range.first; iter != range.second; ++iter)
1702       {
1703         TriggerPrimitivev1::Range sumrange = iter->second->getSums();
1704         for (TriggerPrimitivev1::Iter siter = sumrange.first; siter != sumrange.second; ++siter)
1705         {
1706           for (int is = 0; is < nsample; is++)
1707           {
1708             siter->second->push_back(0);
1709           }
1710         }
1711       }
1712     }
1713     if (Verbosity())
1714     {
1715       std::cout << __FILE__ << "::" << __FUNCTION__ << "::" << __LINE__ << ":: Processing organizer" << std::endl;
1716     }
1717     // get jet primitives (after EMCAL and HCAL sum)
1718     range = m_primitives_jet->getTriggerPrimitives();
1719     for (TriggerPrimitiveContainerv1::Iter iter = range.first; iter != range.second; ++iter)
1720     {
1721       if (Verbosity())
1722       {
1723         std::cout << __FILE__ << "::" << __FUNCTION__ << "::" << __LINE__ << ":: Processing organizer" << std::endl;
1724       }
1725 
1726       TriggerDefs::TriggerPrimKey jet_pkey = (*iter).first;
1727       TriggerDefs::TriggerPrimKey hcal_pkey = TriggerDefs::getTriggerPrimKey(TriggerDefs::TriggerId::jetTId, TriggerDefs::GetDetectorId("HCAL"), TriggerDefs::GetPrimitiveId("JET"), TriggerDefs::getPrimitiveLocId_from_TriggerPrimKey(jet_pkey));
1728       TriggerDefs::TriggerPrimKey emcal_pkey = TriggerDefs::getTriggerPrimKey(TriggerDefs::TriggerId::jetTId, TriggerDefs::GetDetectorId("EMCAL"), TriggerDefs::GetPrimitiveId("JET"), TriggerDefs::getPrimitiveLocId_from_TriggerPrimKey(jet_pkey));
1729       TriggerPrimitive *primitive = (*iter).second;
1730       TriggerPrimitivev1::Range sumrange = primitive->getSums();
1731       for (TriggerPrimitivev1::Iter iter_sum = sumrange.first; iter_sum != sumrange.second; ++iter_sum)
1732       {
1733         if (Verbosity())
1734         {
1735           std::cout << __FILE__ << "::" << __FUNCTION__ << "::" << __LINE__ << ":: Processing organizer" << std::endl;
1736         }
1737 
1738         TriggerDefs::TriggerSumKey jet_skey = (*iter_sum).first;
1739 
1740         TriggerDefs::TriggerSumKey hcal_skey = TriggerDefs::getTriggerSumKey(TriggerDefs::TriggerId::jetTId, TriggerDefs::GetDetectorId("HCAL"), TriggerDefs::GetPrimitiveId("JET"), TriggerDefs::getPrimitiveLocId_from_TriggerPrimKey(jet_pkey), TriggerDefs::getSumLocId(jet_skey));
1741         TriggerDefs::TriggerSumKey emcal_skey = TriggerDefs::getTriggerSumKey(TriggerDefs::TriggerId::jetTId, TriggerDefs::GetDetectorId("EMCAL"), TriggerDefs::GetPrimitiveId("JET"), TriggerDefs::getPrimitiveLocId_from_TriggerPrimKey(jet_pkey), TriggerDefs::getSumLocId(jet_skey));
1742 
1743         int i = 0;
1744         if (Verbosity())
1745         {
1746           std::cout << __FILE__ << "::" << __FUNCTION__ << "::" << __LINE__ << ":: Processing organizer" << std::endl;
1747         }
1748 
1749         for (unsigned int &it_s : *(iter_sum->second))
1750         {
1751           unsigned int sum_hcal = m_primitives_hcal_ll1->get_primitive_at_key(hcal_pkey)->get_sum_at_key(hcal_skey)->at(i);
1752           unsigned int sum_emcal = m_primitives_emcal_ll1->get_primitive_at_key(emcal_pkey)->get_sum_at_key(emcal_skey)->at(i);
1753           it_s = ((sum_hcal >> 1U) + (sum_emcal >> 1U)) & 0xffU;
1754           i++;
1755         }
1756       }
1757     }
1758   }
1759 
1760   return 0;
1761 }
1762 
1763 // This is where the LL1 Jet/Pair/Cosmic algorithm is
1764 
1765 int CaloTriggerEmulator::process_trigger()
1766 {
1767   std::vector<unsigned int> bits;
1768 
1769   int nsample = m_nsamples - 1;
1770   // bits are to say whether the trigger has fired. this is what is sent to the GL1
1771   if (m_trig_sample > 0)
1772   {
1773     nsample = 1;
1774   }
1775   bits.reserve(nsample);
1776   for (int is = 0; is < nsample; is++)
1777   {
1778     bits.push_back(0);
1779   }
1780 
1781   // photon
1782   // 8x8 non-overlapping sums in the EMCAL
1783   // create the 8x8 non-overlapping sum
1784 
1785   {
1786     m_triggerid = TriggerDefs::TriggerId::photonTId;
1787     std::vector<unsigned int> *trig_bits = m_ll1out_photon->GetTriggerBits();
1788     if (Verbosity() >= 2)
1789     {
1790       std::cout << __FUNCTION__ << " " << __LINE__ << " processing PHOTON trigger , bits before: " << trig_bits->size() << std::endl;
1791     }
1792 
1793     TriggerPrimitiveContainer::Range range = m_primitives_emcal_ll1->getTriggerPrimitives();
1794 
1795     for (TriggerPrimitiveContainerv1::Iter iter = range.first; iter != range.second; ++iter)
1796     {
1797       // get key and see if masked
1798       TriggerDefs::TriggerPrimKey key = (*iter).first;
1799       if (CheckFiberMasks(key))
1800       {
1801         continue;
1802       }
1803 
1804       // get the primitive (16 2x2 sums)
1805       TriggerPrimitive *primitive = (*iter).second;
1806 
1807       TriggerPrimitivev1::Range sumrange = primitive->getSums();
1808 
1809       // iterate through all 24 sums and add together
1810       for (TriggerPrimitivev1::Iter iter_sum = sumrange.first; iter_sum != sumrange.second; ++iter_sum)
1811       {
1812         // check if sum is greater than threshold.
1813         if (Verbosity() >= 2)
1814         {
1815           std::cout << __FUNCTION__ << " " << __LINE__ << " processing PHOTON trigger" << std::endl;
1816         }
1817 
1818         std::vector<unsigned int> *t_sum = (*iter_sum).second;
1819         TriggerDefs::TriggerSumKey sumk = (*iter_sum).first;
1820         for (int is = 0; is < nsample; is++)
1821         {
1822           unsigned short bit = getBits(t_sum->at(is), TriggerDefs::TriggerId::photonTId);
1823           if (bit)
1824           {
1825             m_ll1out_photon->addTriggeredSum(sumk, t_sum->at(is));
1826             m_ll1out_photon->addTriggeredPrimitive(key);
1827           }
1828           bits.at(is) |= bit;
1829         }
1830       }
1831     }
1832 
1833     uint16_t pass = 0;
1834     for (int is = 0; is < nsample; is++)
1835     {
1836       pass |= bits.at(is);
1837       trig_bits->push_back(bits.at(is));
1838     }
1839 
1840     if (pass)
1841     {
1842       m_photon_npassed++;
1843     }
1844   }
1845   {
1846     if (Verbosity() >= 2)
1847     {
1848       std::cout << __FUNCTION__ << " " << __LINE__ << " processing JET trigger" << std::endl;
1849     }
1850 
1851     // Make the jet primitives
1852     m_triggerid = TriggerDefs::TriggerId::jetTId;
1853     std::vector<unsigned int> *trig_bits = m_ll1out_jet->GetTriggerBits();
1854     std::vector<unsigned int> jet_map[32][9]{};
1855     for (auto &ie : jet_map)
1856     {
1857       for (auto &ip : ie)
1858       {
1859         for (int is = 0; is < nsample; is++)
1860         {
1861           ip.push_back(0);
1862         }
1863       }
1864     }
1865 
1866     if (!m_primitives_jet)
1867     {
1868       std::cout << "There is no primitive container" << std::endl;
1869       return Fun4AllReturnCodes::EVENT_OK;
1870     }
1871 
1872     // iterate through emcal primitives and organize into the 16 jet primitives each with the 8x8 nonoverlapping sum
1873     TriggerPrimitiveContainer::Range range = m_primitives_jet->getTriggerPrimitives();
1874 
1875     for (TriggerPrimitiveContainerv1::Iter iter = range.first; iter != range.second; ++iter)
1876     {
1877       // get the primitive (16 2x2 sums)
1878       TriggerPrimitive *primitive = (*iter).second;
1879       TriggerPrimitivev1::Range sumrange = primitive->getSums();
1880 
1881       // iterate through all 16 sums and add together
1882       for (TriggerPrimitivev1::Iter iter_sum = sumrange.first; iter_sum != sumrange.second; ++iter_sum)
1883       {
1884         TriggerDefs::TriggerSumKey sumkey = (*iter_sum).first;
1885 
1886         int i = 0;
1887         int sum_phi = static_cast<int>((TriggerDefs::getPrimitivePhiId_from_TriggerSumKey(sumkey) * 2) + TriggerDefs::getSumPhiId(sumkey));
1888         int sum_eta = static_cast<int>(TriggerDefs::getSumEtaId(sumkey));
1889         if (Verbosity() >= 2)
1890         {
1891           std::cout << __FUNCTION__ << " " << __LINE__ << " processing JET trigger " << sum_phi << " " << sum_eta << std::endl;
1892         }
1893 
1894         for (unsigned int &it_s : *(iter_sum->second))
1895         {
1896           for (int ijeta = (sum_eta <= 3 ? 0 : sum_eta - 3); ijeta <= (sum_eta > 8 ? 8 : sum_eta); ijeta++)
1897           {
1898             for (int ijphi = sum_phi - 3; ijphi <= sum_phi; ijphi++)
1899             {
1900               int iphi = (ijphi < 0 ? 32 + ijphi : ijphi);
1901               jet_map[iphi][ijeta].at(i) += it_s;
1902             }
1903           }
1904           i++;
1905         }
1906       }
1907     }
1908     if (Verbosity() >= 2)
1909     {
1910       std::cout << __FUNCTION__ << " " << __LINE__ << " processing JET trigger" << std::endl;
1911     }
1912 
1913     int pass = 0;
1914     for (int ijphi = 0; ijphi < 32; ijphi++)
1915     {
1916       for (int ijeta = 0; ijeta < 9; ijeta++)
1917       {
1918         if (Verbosity() >= 2)
1919         {
1920           std::cout << __FUNCTION__ << " " << __LINE__ << " processing JET trigger " << ijphi << " " << ijeta << std::endl;
1921         }
1922 
1923         unsigned int sk = ((unsigned int) ijphi & 0xffffU) + (((unsigned int) ijeta & 0xffffU) << 16U);
1924         std::vector<unsigned int> *sum = m_ll1out_jet->get_word(sk);
1925         if (Verbosity() >= 2)
1926         {
1927           std::cout << __FUNCTION__ << " " << __LINE__ << " processing JET trigger " << ijphi << " " << ijeta << std::endl;
1928         }
1929 
1930         for (int is = 0; is < nsample; is++)
1931         {
1932           if (Verbosity() >= 2)
1933           {
1934             std::cout << __FUNCTION__ << " " << __LINE__ << " processing JET trigger " << ijphi << " " << ijeta << std::endl;
1935           }
1936 
1937           sum->push_back(jet_map[ijphi][ijeta].at(is));
1938           unsigned short bit = getBits(jet_map[ijphi][ijeta].at(is), TriggerDefs::TriggerId::jetTId);
1939 
1940           if (bit)
1941           {
1942             m_ll1out_jet->addTriggeredSum(sk, jet_map[ijphi][ijeta].at(is));
1943             m_ll1out_jet->addTriggeredPrimitive(sk);
1944             pass = 1;
1945           }
1946           bits.at(is) |= bit;
1947         }
1948       }
1949     }
1950     if (Verbosity() >= 2)
1951     {
1952       std::cout << __FUNCTION__ << " " << __LINE__ << " processing JET trigger" << std::endl;
1953     }
1954 
1955     for (int is = 0; is < nsample; is++)
1956     {
1957       trig_bits->push_back(bits.at(is));
1958     }
1959 
1960     if (pass)
1961     {
1962       m_jet_npassed++;
1963     }
1964   }
1965   // //pair trigger here
1966   // {
1967   //   if (Verbosity())
1968   //     {
1969   //    std::cout << __FILE__ << "::" << __FUNCTION__ << "::" << std::dec <<__LINE__ << ":: Processing organizer" << std::endl;
1970   //     }
1971 
1972   //   m_triggerid = TriggerDefs::TriggerId::pairTId;
1973   //   if (m_primitives_emcal)
1974   //     {
1975   //    // iterate through emcal primitives and organize into the 16 jet primitives each with the 8x8 nonoverlapping sum
1976   //    TriggerPrimitiveContainerv1::Range range = m_primitives_emcal->getTriggerPrimitives();
1977   //    std::vector<unsigned int> pair_map[128][47]{};
1978   //    for (auto & ie : pair_map)
1979   //      {
1980   //        for (auto & ip : ie)
1981   //          {
1982   //        for (int is = 0; is < nsample; is++)
1983   //          {
1984   //            ip.push_back(0);
1985   //          }
1986   //          }
1987   //      }
1988 
1989   //    for (TriggerPrimitiveContainerv1::Iter iter = range.first; iter != range.second; ++iter)
1990   //      {
1991   //        if (Verbosity())
1992   //          {
1993   //        std::cout << __FILE__ << "::" << __FUNCTION__ << "::" << std::dec <<__LINE__ << ":: Processing organizer" << std::endl;
1994   //          }
1995 
1996   //        // get key and see if masked
1997   //        TriggerDefs::TriggerPrimKey key = (*iter).first;
1998   //        if (CheckFiberMasks(key))
1999   //          {
2000   //        if (Verbosity() >= 2)
2001   //          {
2002   //            std::cout << "masked: " << key << std::endl;
2003   //          }
2004   //        continue;
2005   //          }
2006 
2007   //        //get eta and phi from emcal
2008   //        uint16_t primphi = TriggerDefs::getPrimitivePhiId_from_TriggerPrimKey(key);
2009   //        uint16_t primeta = TriggerDefs::getPrimitiveEtaId_from_TriggerPrimKey(key);
2010 
2011   //        TriggerPrimitivev1::Range sumrange = (iter->second)->getSums();
2012   //        for (TriggerPrimitivev1::Iter iter_sum = sumrange.first; iter_sum != sumrange.second; ++iter_sum)
2013   //          {
2014 
2015   //        if (CheckChannelMasks(iter_sum->first))
2016   //          {
2017   //            continue;
2018   //          }
2019   //        uint16_t sumphi = TriggerDefs::getSumPhiId(iter_sum->first);
2020   //        uint16_t sumeta = TriggerDefs::getSumEtaId(iter_sum->first);
2021 
2022   //        uint16_t globalphi = primphi*4 + sumphi;
2023   //        uint16_t globaleta = primeta*4 + sumeta;
2024 
2025   //        uint16_t lowerphi = 127;
2026   //        if (globalphi > 0)
2027   //          {
2028   //            lowerphi = globalphi - 1;
2029   //          }
2030 
2031   //        int i = 0;
2032   //        for (unsigned int &it_s : *(iter_sum->second))
2033   //          {
2034 
2035   //            if (globaleta > 0)
2036   //              {
2037   //            pair_map[globalphi][globaleta-1].at(i) += it_s;
2038   //            pair_map[lowerphi][globaleta-1].at(i) += it_s;
2039   //              }
2040   //            if (globaleta < 47)
2041   //              {
2042   //            pair_map[globalphi][globaleta].at(i) += it_s;
2043   //            pair_map[lowerphi][globaleta].at(i) += it_s;
2044   //              }
2045   //            i++;
2046   //          }
2047   //          }
2048   //      }
2049 
2050   //    // no go through and find the peaks.
2051   //    //std::vector<unsigned int> pair_map[128][47]{};
2052   //    unsigned int maximum = 0;
2053   //    int philoc = -1;
2054   //    int etaloc = -1;
2055   //    for (int ieta = 1; ieta < 47; ieta++)
2056   //      {
2057   //        for (int iphi = 0; iphi < 128; iphi++)
2058   //          {
2059   //        if (Verbosity())
2060   //          {
2061   //            std::cout << __FILE__ << "::" << __FUNCTION__ << "::" << std::dec <<__LINE__ << ":: Processing pair "<< iphi << " " << ieta << std::endl;
2062   //          }
2063 
2064   //        int lowerphi = iphi - 1;
2065   //        int higherphi = iphi + 1;
2066   //        if (Verbosity())
2067   //          {
2068   //            std::cout << __FILE__ << "::" << __FUNCTION__ << "::" << std::dec <<__LINE__ << ":: Processing pair look"<< lowerphi << " " << higherphi << std::endl;
2069   //          }
2070 
2071   //        if (higherphi > 127) higherphi = 0;
2072   //        if (lowerphi < 0) lowerphi = 127;
2073 
2074   //        if (Verbosity())
2075   //          {
2076   //            std::cout << __FILE__ << "::" << __FUNCTION__ << "::" << std::dec <<__LINE__ << ":: Processing pair look"<< lowerphi << " " << higherphi << std::endl;
2077   //          }
2078 
2079   //        if (pair_map[iphi][ieta].at(0) <= maximum) {continue;}
2080 
2081   //        if (pair_map[iphi][ieta].at(0) < pair_map[iphi][ieta + 1].at(0)) {continue;}
2082   //        if (pair_map[iphi][ieta].at(0) < pair_map[iphi + 1][ieta + 1].at(0)) {continue;}
2083   //        if (pair_map[iphi][ieta].at(0) < pair_map[iphi - 1][ieta + 1].at(0)) {continue;}
2084   //        if (pair_map[iphi][ieta].at(0) < pair_map[iphi + 1][ieta].at(0)) {continue;}
2085   //        if (pair_map[iphi][ieta].at(0) <= pair_map[iphi - 1][ieta].at(0)) {continue;}
2086   //        if (pair_map[iphi][ieta].at(0) <= pair_map[iphi + 1][ieta - 1].at(0)) {continue;}
2087   //        if (pair_map[iphi][ieta].at(0) <= pair_map[iphi][ieta - 1].at(0)) {continue;}
2088   //        if (pair_map[iphi][ieta].at(0) <= pair_map[iphi - 1][ieta - 1].at(0)) {continue;}
2089   //        maximum = pair_map[iphi][ieta].at(0);
2090   //        philoc = iphi;
2091   //        etaloc = ieta;
2092   //        if (Verbosity())
2093   //          {
2094   //            std::cout << __FILE__ << "::" << __FUNCTION__ << "::" << std::dec <<__LINE__ << ":: Processing pair look"<< maximum << std::endl;
2095   //          }
2096 
2097   //          }
2098   //      }
2099 
2100   //    std::vector<unsigned int> *trig_bits = m_ll1out_pair->GetTriggerBits();
2101   //    unsigned int bit = getBits(maximum, TriggerDefs::TriggerId::pairTId);
2102 
2103   //    if (bit)
2104   //      {
2105   //        unsigned int sumloc = ((unsigned int) philoc) & 0xffffU;
2106   //        sumloc += (((unsigned int) etaloc)  & 0xffffU ) << 16U;
2107   //        unsigned int primloc = philoc/4;
2108   //        m_ll1out_pair->addTriggeredSum(sumloc);
2109   //        m_ll1out_pair->addTriggeredPrimitive(primloc);
2110   //        m_pair_npassed++;
2111   //      }
2112   //    trig_bits->push_back(bit);
2113   //     }
2114 
2115   return Fun4AllReturnCodes::EVENT_OK;
2116 }
2117 
2118 void CaloTriggerEmulator::GetNodes(PHCompositeNode *topNode)
2119 {
2120   if (Verbosity() >= 2)
2121   {
2122     std::cout << __FUNCTION__ << std::endl;
2123   }
2124 
2125   m_event = findNode::getClass<Event>(topNode, "PRDF");
2126 
2127   m_ll1_nodename = "LL1OUT_PAIR";
2128 
2129   m_ll1out_pair = findNode::getClass<LL1Out>(topNode, m_ll1_nodename);
2130 
2131   if (!m_ll1out_pair)
2132   {
2133     std::cout << "No LL1Out found... " << std::endl;
2134     exit(1);
2135   }
2136 
2137   m_prim_nodename = "TRIGGERPRIMITIVES_PAIR";
2138   m_primitives_pair = findNode::getClass<TriggerPrimitiveContainer>(topNode, m_prim_nodename);
2139 
2140   if (!m_primitives_pair)
2141   {
2142     std::cout << "No TriggerPrimitives found... " << std::endl;
2143     exit(1);
2144   }
2145 
2146   m_ll1_nodename = "LL1OUT_JET";
2147   m_ll1out_jet = findNode::getClass<LL1Out>(topNode, m_ll1_nodename);
2148 
2149   if (!m_ll1out_jet)
2150   {
2151     std::cout << "No LL1Out found... " << std::endl;
2152     exit(1);
2153   }
2154 
2155   m_prim_nodename = "TRIGGERPRIMITIVES_JET";
2156   m_primitives_jet = findNode::getClass<TriggerPrimitiveContainer>(topNode, m_prim_nodename);
2157 
2158   if (!m_primitives_jet)
2159   {
2160     std::cout << "No TriggerPrimitives found... " << std::endl;
2161     exit(1);
2162   }
2163 
2164   m_ll1_nodename = "LL1OUT_PHOTON";
2165   m_ll1out_photon = findNode::getClass<LL1Out>(topNode, m_ll1_nodename);
2166 
2167   if (!m_ll1out_photon)
2168   {
2169     std::cout << "No LL1Out found... " << std::endl;
2170     exit(1);
2171   }
2172 
2173   m_prim_nodename = "TRIGGERPRIMITIVES_PHOTON";
2174   m_primitives_photon = findNode::getClass<TriggerPrimitiveContainer>(topNode, m_prim_nodename);
2175 
2176   if (!m_primitives_photon)
2177   {
2178     std::cout << "No TriggerPrimitives found... " << std::endl;
2179     exit(1);
2180   }
2181 
2182   bool hcalset = false;
2183   if (m_do_hcalout)
2184   {
2185     if (!m_isdata)
2186     {
2187       m_waveforms_hcalout = findNode::getClass<TowerInfoContainer>(topNode, "WAVEFORM_HCALOUT");
2188 
2189       if (!m_waveforms_hcalout)
2190       {
2191         std::cout << "No HCALOUT Waveforms found... " << std::endl;
2192         exit(1);
2193       }
2194     }
2195     m_hcal_packets = findNode::getClass<CaloPacketContainer>(topNode, "HCALPackets");
2196 
2197     if (m_hcal_packets)
2198     {
2199       m_useoffline = true;
2200     }
2201 
2202     m_primitives_hcalout = findNode::getClass<TriggerPrimitiveContainer>(topNode, "TRIGGERPRIMITIVES_HCALOUT");
2203 
2204     if (!m_primitives_hcalout)
2205     {
2206       std::cout << "No HCALOUT Primitives found... " << std::endl;
2207       exit(1);
2208     }
2209 
2210     m_primitives_hcal_ll1 = findNode::getClass<TriggerPrimitiveContainer>(topNode, "TRIGGERPRIMITIVES_HCAL_LL1");
2211 
2212     if (!m_primitives_hcal_ll1)
2213     {
2214       std::cout << "No HCAL Primitives found... " << std::endl;
2215       exit(1);
2216     }
2217     hcalset = true;
2218   }
2219 
2220   if (m_do_hcalin)
2221   {
2222     if (!m_isdata)
2223     {
2224       m_waveforms_hcalin = findNode::getClass<TowerInfoContainer>(topNode, "WAVEFORM_HCALIN");
2225 
2226       if (!m_waveforms_hcalin)
2227       {
2228         std::cout << "No HCAL Waveforms found... " << std::endl;
2229         exit(1);
2230       }
2231     }
2232 
2233     m_primitives_hcalin = findNode::getClass<TriggerPrimitiveContainer>(topNode, "TRIGGERPRIMITIVES_HCALIN");
2234 
2235     if (!m_primitives_hcalin)
2236     {
2237       std::cout << "No HCALIN Primitives found... " << std::endl;
2238       exit(1);
2239     }
2240 
2241     if (!hcalset)
2242     {
2243       m_hcal_packets = findNode::getClass<CaloPacketContainer>(topNode, "HCALPackets");
2244 
2245       m_primitives_hcal_ll1 = findNode::getClass<TriggerPrimitiveContainer>(topNode, "TRIGGERPRIMITIVES_HCAL_LL1");
2246 
2247       if (!m_primitives_hcal_ll1)
2248       {
2249         std::cout << "No HCAL Primitives found... " << std::endl;
2250         exit(1);
2251       }
2252     }
2253   }
2254   if (m_do_emcal)
2255   {
2256     m_emcal_packets = findNode::getClass<CaloPacketContainer>(topNode, "CEMCPackets");
2257 
2258     if (!m_isdata)
2259     {
2260       m_waveforms_emcal = findNode::getClass<TowerInfoContainer>(topNode, "WAVEFORM_CEMC");
2261 
2262       if (!m_waveforms_emcal)
2263       {
2264         std::cout << "No EMCAL Waveforms found... " << std::endl;
2265         exit(1);
2266       }
2267     }
2268     m_primitives_emcal = findNode::getClass<TriggerPrimitiveContainer>(topNode, "TRIGGERPRIMITIVES_EMCAL");
2269 
2270     if (!m_primitives_emcal)
2271     {
2272       std::cout << "No EMCAL Primitives found... " << std::endl;
2273       exit(1);
2274     }
2275 
2276     m_primitives_emcal_ll1 = findNode::getClass<TriggerPrimitiveContainer>(topNode, "TRIGGERPRIMITIVES_EMCAL_LL1");
2277 
2278     if (!m_primitives_emcal_ll1)
2279     {
2280       std::cout << "No EMCAL 8x8 Primitives found... " << std::endl;
2281       exit(1);
2282     }
2283 
2284     m_primitives_emcal_2x2_ll1 = findNode::getClass<TriggerPrimitiveContainer>(topNode, "TRIGGERPRIMITIVES_EMCAL_2x2_LL1");
2285 
2286     if (!m_primitives_emcal_2x2_ll1)
2287     {
2288       std::cout << "No EMCAL 2x2 Primitives found... " << std::endl;
2289       exit(1);
2290     }
2291   }
2292 
2293   return;
2294 }
2295 void CaloTriggerEmulator::CreateNodes(PHCompositeNode *topNode)
2296 {
2297   PHNodeIterator iter(topNode);
2298   PHCompositeNode *dstNode = dynamic_cast<PHCompositeNode *>(iter.findFirst("PHCompositeNode", "DST"));
2299   if (!dstNode)
2300   {
2301     std::cout << PHWHERE << "DST Node missing doing nothing" << std::endl;
2302   }
2303 
2304   PHCompositeNode *ll1Node = dynamic_cast<PHCompositeNode *>(iter.findFirst("PHCompositeNode", "LL1"));
2305   if (!ll1Node)
2306   {
2307     ll1Node = new PHCompositeNode("LL1");
2308     dstNode->addNode(ll1Node);
2309   }
2310   {
2311     m_ll1_nodename = "LL1OUT_PHOTON";
2312     LL1Out *ll1out = findNode::getClass<LL1Out>(ll1Node, m_ll1_nodename);
2313     if (!ll1out)
2314     {
2315       ll1out = new LL1Outv1("PHOTON", "NONE");
2316       PHIODataNode<PHObject> *LL1OutNode = new PHIODataNode<PHObject>(ll1out, m_ll1_nodename, "PHObject");
2317       ll1Node->addNode(LL1OutNode);
2318     }
2319     m_prim_nodename = "TRIGGERPRIMITIVES_PHOTON";
2320     TriggerPrimitiveContainer *ll1out_prim = findNode::getClass<TriggerPrimitiveContainer>(ll1Node, m_prim_nodename);
2321     if (!ll1out_prim)
2322     {
2323       ll1out_prim = new TriggerPrimitiveContainerv1(TriggerDefs::TriggerId::photonTId, TriggerDefs::DetectorId::noneDId);
2324       PHIODataNode<PHObject> *LL1OutNode = new PHIODataNode<PHObject>(ll1out_prim, m_prim_nodename, "PHObject");
2325       ll1Node->addNode(LL1OutNode);
2326     }
2327   }
2328   {
2329     m_ll1_nodename = "LL1OUT_JET";
2330     LL1Out *ll1out = findNode::getClass<LL1Out>(ll1Node, m_ll1_nodename);
2331     if (!ll1out)
2332     {
2333       ll1out = new LL1Outv1("JET", "NONE");
2334       PHIODataNode<PHObject> *LL1OutNode = new PHIODataNode<PHObject>(ll1out, m_ll1_nodename, "PHObject");
2335       ll1Node->addNode(LL1OutNode);
2336     }
2337     m_prim_nodename = "TRIGGERPRIMITIVES_JET";
2338     TriggerPrimitiveContainer *ll1out_prim = findNode::getClass<TriggerPrimitiveContainer>(ll1Node, m_prim_nodename);
2339     if (!ll1out_prim)
2340     {
2341       ll1out_prim = new TriggerPrimitiveContainerv1(TriggerDefs::TriggerId::jetTId, TriggerDefs::DetectorId::noneDId);
2342       PHIODataNode<PHObject> *LL1OutNode = new PHIODataNode<PHObject>(ll1out_prim, m_prim_nodename, "PHObject");
2343       ll1Node->addNode(LL1OutNode);
2344     }
2345   }
2346   {
2347     m_ll1_nodename = "LL1OUT_PAIR";
2348     LL1Out *ll1out = findNode::getClass<LL1Out>(ll1Node, m_ll1_nodename);
2349     if (!ll1out)
2350     {
2351       ll1out = new LL1Outv1("PAIR", "NONE");
2352       PHIODataNode<PHObject> *LL1OutNode = new PHIODataNode<PHObject>(ll1out, m_ll1_nodename, "PHObject");
2353       ll1Node->addNode(LL1OutNode);
2354     }
2355     m_prim_nodename = "TRIGGERPRIMITIVES_PAIR";
2356     TriggerPrimitiveContainer *ll1out_prim = findNode::getClass<TriggerPrimitiveContainer>(ll1Node, m_prim_nodename);
2357     if (!ll1out_prim)
2358     {
2359       ll1out_prim = new TriggerPrimitiveContainerv1(TriggerDefs::TriggerId::pairTId, TriggerDefs::DetectorId::noneDId);
2360       PHIODataNode<PHObject> *LL1OutNode = new PHIODataNode<PHObject>(ll1out_prim, m_prim_nodename, "PHObject");
2361       ll1Node->addNode(LL1OutNode);
2362     }
2363   }
2364   if (m_do_emcal)
2365   {
2366     std::string ll1_nodename = "TRIGGERPRIMITIVES_EMCAL";
2367     TriggerPrimitiveContainer *ll1out_d1 = findNode::getClass<TriggerPrimitiveContainer>(ll1Node, ll1_nodename);
2368     if (!ll1out_d1)
2369     {
2370       ll1out_d1 = new TriggerPrimitiveContainerv1(TriggerDefs::TriggerId::noneTId, TriggerDefs::DetectorId::emcalDId);
2371       PHIODataNode<PHObject> *LL1OutNode = new PHIODataNode<PHObject>(ll1out_d1, ll1_nodename, "PHObject");
2372       ll1Node->addNode(LL1OutNode);
2373     }
2374 
2375     ll1_nodename = "TRIGGERPRIMITIVES_EMCAL_LL1";
2376     ll1out_d1 = findNode::getClass<TriggerPrimitiveContainer>(ll1Node, ll1_nodename);
2377     if (!ll1out_d1)
2378     {
2379       ll1out_d1 = new TriggerPrimitiveContainerv1(TriggerDefs::TriggerId::jetTId, TriggerDefs::DetectorId::emcalDId);
2380       PHIODataNode<PHObject> *LL1OutNode = new PHIODataNode<PHObject>(ll1out_d1, ll1_nodename, "PHObject");
2381       ll1Node->addNode(LL1OutNode);
2382     }
2383 
2384     ll1_nodename = "TRIGGERPRIMITIVES_EMCAL_2x2_LL1";
2385     ll1out_d1 = findNode::getClass<TriggerPrimitiveContainer>(ll1Node, ll1_nodename);
2386     if (!ll1out_d1)
2387     {
2388       ll1out_d1 = new TriggerPrimitiveContainerv1(TriggerDefs::TriggerId::pairTId, TriggerDefs::DetectorId::emcalDId);
2389       PHIODataNode<PHObject> *LL1OutNode = new PHIODataNode<PHObject>(ll1out_d1, ll1_nodename, "PHObject");
2390       ll1Node->addNode(LL1OutNode);
2391     }
2392   }
2393   if (m_do_hcalout)
2394   {
2395     std::string ll1_nodename = "TRIGGERPRIMITIVES_HCALOUT";
2396     TriggerPrimitiveContainer *ll1out_d = findNode::getClass<TriggerPrimitiveContainer>(ll1Node, ll1_nodename);
2397     if (!ll1out_d)
2398     {
2399       ll1out_d = new TriggerPrimitiveContainerv1(TriggerDefs::TriggerId::noneTId, TriggerDefs::DetectorId::hcaloutDId);
2400       PHIODataNode<PHObject> *LL1OutNode = new PHIODataNode<PHObject>(ll1out_d, ll1_nodename, "PHObject");
2401       ll1Node->addNode(LL1OutNode);
2402     }
2403 
2404     ll1_nodename = "TRIGGERPRIMITIVES_HCAL_LL1";
2405     TriggerPrimitiveContainer *ll1out_d1 = findNode::getClass<TriggerPrimitiveContainer>(ll1Node, ll1_nodename);
2406     if (!ll1out_d1)
2407     {
2408       ll1out_d1 = new TriggerPrimitiveContainerv1(TriggerDefs::TriggerId::jetTId, TriggerDefs::DetectorId::hcalDId);
2409       PHIODataNode<PHObject> *LL1OutNode = new PHIODataNode<PHObject>(ll1out_d1, ll1_nodename, "PHObject");
2410       ll1Node->addNode(LL1OutNode);
2411     }
2412   }
2413   if (m_do_hcalin)
2414   {
2415     std::string ll1_nodename = "TRIGGERPRIMITIVES_HCALIN";
2416     TriggerPrimitiveContainer *ll1out_d = findNode::getClass<TriggerPrimitiveContainer>(ll1Node, ll1_nodename);
2417     if (!ll1out_d)
2418     {
2419       ll1out_d = new TriggerPrimitiveContainerv1(TriggerDefs::TriggerId::noneTId, TriggerDefs::DetectorId::hcalinDId);
2420       PHIODataNode<PHObject> *LL1OutNode = new PHIODataNode<PHObject>(ll1out_d, ll1_nodename, "PHObject");
2421       ll1Node->addNode(LL1OutNode);
2422     }
2423     ll1_nodename = "TRIGGERPRIMITIVES_HCAL_LL1";
2424     TriggerPrimitiveContainer *ll1out_d1 = findNode::getClass<TriggerPrimitiveContainer>(ll1Node, ll1_nodename);
2425     if (!ll1out_d1)
2426     {
2427       ll1out_d1 = new TriggerPrimitiveContainerv1(TriggerDefs::TriggerId::jetTId, TriggerDefs::DetectorId::hcalDId);
2428       PHIODataNode<PHObject> *LL1OutNode = new PHIODataNode<PHObject>(ll1out_d1, ll1_nodename, "PHObject");
2429       ll1Node->addNode(LL1OutNode);
2430     }
2431   }
2432 }
2433 
2434 int CaloTriggerEmulator::End(PHCompositeNode * /*topNode*/)
2435 {
2436   delete cdbttree_emcal;
2437   delete cdbttree_hcalout;
2438   delete cdbttree_hcalin;
2439 
2440   std::cout << "------------------------" << std::endl;
2441   std::cout << "Total Jet passed: " << m_jet_npassed << "/" << m_nevent << std::endl;
2442   std::cout << "Total Photon passed: " << m_photon_npassed << "/" << m_nevent << std::endl;
2443   std::cout << "Total Pair passed: " << m_pair_npassed << "/" << m_nevent << std::endl;
2444   std::cout << "------------------------" << std::endl;
2445 
2446   return 0;
2447 }
2448 
2449 void CaloTriggerEmulator::identify()
2450 {
2451   std::cout << " CaloTriggerEmulator: " << m_trigger << std::endl;
2452   std::cout << " LL1Out: " << std::endl;
2453 
2454   if (m_ll1out_photon)
2455   {
2456     m_ll1out_photon->identify();
2457   }
2458   if (m_ll1out_jet)
2459   {
2460     m_ll1out_jet->identify();
2461   }
2462   if (m_ll1out_pair)
2463   {
2464     m_ll1out_pair->identify();
2465   }
2466 
2467   if (m_primitives_photon)
2468   {
2469     m_primitives_photon->identify();
2470   }
2471 
2472   if (m_primitives_jet)
2473   {
2474     m_primitives_jet->identify();
2475   }
2476 
2477   if (m_primitives_pair)
2478   {
2479     m_primitives_pair->identify();
2480   }
2481 
2482   if (m_primitives_emcal_ll1)
2483   {
2484     m_primitives_emcal_ll1->identify();
2485   }
2486 
2487   if (m_primitives_hcal_ll1)
2488   {
2489     m_primitives_hcal_ll1->identify();
2490   }
2491 
2492   if (m_primitives_emcal_2x2_ll1)
2493   {
2494     m_primitives_emcal_2x2_ll1->identify();
2495   }
2496 
2497   if (m_primitives_emcal)
2498   {
2499     m_primitives_emcal->identify();
2500   }
2501 
2502   if (m_primitives_hcalin)
2503   {
2504     m_primitives_hcalin->identify();
2505   }
2506 
2507   if (m_primitives_hcalout)
2508   {
2509     m_primitives_hcalout->identify();
2510   }
2511 
2512   std::cout << " Processed " << m_nevent << std::endl;
2513 }
2514 
2515 void CaloTriggerEmulator::useHCALIN(bool use)
2516 {
2517   m_do_hcalin = use;
2518   m_force_hcalin = true;
2519 }
2520 void CaloTriggerEmulator::useHCALOUT(bool use)
2521 {
2522   m_do_hcalout = use;
2523   m_force_hcalout = true;
2524 }
2525 
2526 void CaloTriggerEmulator::useEMCAL(bool use)
2527 {
2528   m_do_emcal = use;
2529   m_force_emcal = true;
2530 }
2531 
2532 unsigned int CaloTriggerEmulator::getBits(unsigned int sum, TriggerDefs::TriggerId tid)
2533 {
2534   if (tid == TriggerDefs::TriggerId::jetTId)
2535   {
2536     unsigned int bit = 0;
2537     for (unsigned int i = 0; i < 4; i++)
2538     {
2539       bit |= (sum >= m_threshold_jet[i] ? 0x1U << (i) : 0);
2540     }
2541 
2542     return bit;
2543   }
2544   if (tid == TriggerDefs::TriggerId::pairTId)
2545   {
2546     unsigned int bit = 0;
2547     for (unsigned int i = 0; i < 4; i++)
2548     {
2549       bit |= (sum >= m_threshold_pair[i] ? 0x1U << (i) : 0);
2550     }
2551 
2552     return bit;
2553   }
2554   if (tid == TriggerDefs::TriggerId::photonTId)
2555   {
2556     unsigned int bit = 0;
2557     for (unsigned int i = 0; i < 4; i++)
2558     {
2559       bit |= (sum >= m_threshold_photon[i] ? 0x1U << (i) : 0);
2560     }
2561 
2562     return bit;
2563   }
2564   return 0;
2565 }