Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2026-04-06 08:08:20

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