Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:20:28

0001 #include "L1Trigger/L1TCommon/interface/TriggerSystem.h"
0002 #include "L1Trigger/L1TCommon/interface/XmlConfigParser.h"
0003 
0004 using namespace std;
0005 
0006 namespace l1t {
0007 
0008   void TriggerSystem::configureSystemFromFiles(const char *hwCfgFile, const char *topCfgFile, const char *key) {
0009     // read hw description xml
0010     // this will set the sysId
0011     {
0012       XmlConfigParser xmlRdr;
0013       xmlRdr.readDOMFromFile(hwCfgFile);
0014       xmlRdr.readRootElement(*this);
0015     }
0016     // read configuration xml files
0017     {
0018       XmlConfigParser xmlRdr;
0019       xmlRdr.readDOMFromFile(topCfgFile);
0020       xmlRdr.buildGlobalDoc(key, topCfgFile);
0021       xmlRdr.readContexts(key, sysId, *this);
0022     }
0023     isConfigured = true;
0024   }
0025 
0026   void TriggerSystem::addProcessor(const char *processor, const char *role, const char *crate, const char *slot) {
0027     // every processor must have a single defined role
0028     auto p2r = procToRole.find(processor);
0029     if (p2r != procToRole.end() && p2r->second != role)
0030       throw std::runtime_error("Processor: '" + string(processor) + "' already exists but with different role: '" +
0031                                p2r->second + "'");
0032     else {
0033       procEnabled[processor] = true;
0034       procToRole[processor] = role;
0035       procToSlot[processor] = slot;
0036       procParameters.insert(make_pair(string(processor), std::map<std::string, Parameter>()));
0037       procMasks.insert(make_pair(string(processor), std::map<std::string, Mask>()));
0038       roleForProcs[role].insert(processor);
0039       crateForProcs[crate].insert(processor);
0040     }
0041   }
0042 
0043   void TriggerSystem::addDaq(const char *daq, const char *role, const char *crate) {
0044     auto d2r = daqttcToRole.find(daq);
0045     if (d2r != daqttcToRole.end() && d2r->second != role)
0046       throw runtime_error("DAQttc: '" + string(daq) + "' already exists but with different role: " + d2r->second);
0047     else {
0048       daqttcToRole[daq] = role;
0049       daqttcToCrate[daq] = crate;
0050       roleForDaqttcs[role].insert(daq);
0051     }
0052   }
0053 
0054   void TriggerSystem::addParameter(
0055       const char *id, const char *procOrRole, const char *type, const char *value, const char *delim) {
0056     // some tables forget to specify delimeter and we get an empty string here
0057     //  force the "," default delimeter
0058     if (strlen(delim) == 0)
0059       delim = ",";
0060 
0061     // first try to locate a processor with name matching the procOrRole argument
0062     auto processor = procParameters.find(procOrRole);
0063     if (processor != procParameters.end()) {
0064       // processor found -> apply settings to this processor
0065       auto setting = processor->second.find(id);
0066       if (setting != processor->second.end()) {
0067         // setting with this id already exists -> always take the latest value
0068         // if( logs )
0069         //    *logs << "Warning: overriding already existing " << id
0070         //          << " = (" << setting->second.getType() << ") " << setting->second.getValue()
0071         //          << " with new value (" << type << ") " << value << endl;
0072         setting->second = Parameter(id, procOrRole, type, value, delim);
0073       } else
0074         processor->second.insert(make_pair(string(id), Parameter(id, procOrRole, type, value, delim)));
0075       // let's run a consistency check
0076       auto p2r = procToRole.find(procOrRole);
0077       if (p2r == procToRole.end())
0078         if (logs)
0079           *logs << "Warning: TriggerSystem object doesn't yet assign "
0080                 << " a role to the processor " << procOrRole << endl;
0081       return;
0082     }
0083 
0084     // if we've got here, the procOrRole argument must have meaning of role,
0085     //  throw exception otherwise
0086     auto role = roleForProcs.find(procOrRole);
0087     if (role != roleForProcs.end()) {
0088       // apply setting on all of the processors for this role
0089       for (auto &proc : role->second) {
0090         auto processor = procParameters.find(proc);
0091         if (processor != procParameters.end()) {
0092           // processor found -> apply settings to this processor
0093           //  unless the setting with such id already exists
0094           auto setting = processor->second.find(id);
0095           if (setting == processor->second.end())
0096             processor->second.insert(make_pair(string(id), Parameter(id, procOrRole, type, value, delim)));
0097         } else {
0098           map<string, Parameter> tmp;
0099           tmp.insert(make_pair(id, Parameter(id, procOrRole, type, value)));
0100           procParameters.insert(make_pair(proc, std::move(tmp)));
0101           // construct with brace-initialization, although better looking, cannot use move constructor:
0102           //procParameters.insert(
0103           //   make_pair(proc, map<string,Parameter>( {{id,Parameter(id,procOrRole,type,value)}} ) )
0104           //);
0105         }
0106       }
0107     } else
0108       throw runtime_error("Processor or Role '" + string(procOrRole) + "' was not found");
0109   }
0110 
0111   void TriggerSystem::addTable(const char *id,
0112                                const char *procOrRole,
0113                                const char *columns,
0114                                const char *types,
0115                                const vector<string> &rows,
0116                                const char *delim) {
0117     // some tables forget to specify delimeter and we get an empty string here
0118     //  force the "," default delimeter
0119     if (strlen(delim) == 0)
0120       delim = ",";
0121 
0122     // first try to locate a processor with name matching the procOrRole argument
0123     auto processor = procParameters.find(procOrRole);
0124     if (processor != procParameters.end()) {
0125       // processor found -> apply settings to this processor
0126       auto setting = processor->second.find(id);
0127       if (setting != processor->second.end())
0128         // setting with this id already exists -> always take latest value
0129         setting->second = Parameter(id, procOrRole, types, columns, rows, delim);
0130       else
0131         processor->second.insert(make_pair(string(id), Parameter(id, procOrRole, types, columns, rows, delim)));
0132       // let's run a consistency check
0133       auto p2r = procToRole.find(procOrRole);
0134       if (p2r == procToRole.end())
0135         if (logs)
0136           *logs << "Warning: TriggerSystem object doesn't yet assign "
0137                 << " a role to the processor " << procOrRole << endl;
0138       return;
0139     }
0140 
0141     // if we've got here, the procOrRole argument must have meaning of role,
0142     //  throw exception otherwise
0143     auto role = roleForProcs.find(procOrRole);
0144     if (role != roleForProcs.end()) {
0145       // apply setting on all of the processors for this role
0146       for (auto &proc : role->second) {
0147         auto processor = procParameters.find(proc);
0148         if (processor != procParameters.end()) {
0149           // processor found -> apply settings to this processor
0150           //  unless the setting with such id already exists
0151           auto setting = processor->second.find(id);
0152           if (setting == processor->second.end())
0153             processor->second.insert(make_pair(string(id), Parameter(id, procOrRole, types, columns, rows, delim)));
0154         } else {
0155           map<string, Parameter> tmp;
0156           tmp.insert(make_pair(id, Parameter(id, procOrRole, types, columns, rows, delim)));
0157           procParameters.insert(make_pair(proc, std::move(tmp)));
0158           // construct with brace-initialization, although better looking, cannot use move constructor:
0159           //procParameters.insert(
0160           //   make_pair(proc, map<string,Parameter>( {{id,Parameter(id,procOrRole,types,columns,rows,delim)}} ) )
0161           //);
0162         }
0163       }
0164     } else
0165       throw runtime_error("Processor or Role '" + string(procOrRole) + "' was not found");
0166   }
0167 
0168   const map<string, Parameter> &TriggerSystem::getParameters(const char *p) const {
0169     if (!isConfigured)
0170       throw runtime_error("TriggerSystem is not configured yet. First call the configureSystem method");
0171 
0172     auto processor = procParameters.find(p);
0173     if (processor == procParameters.end())
0174       throw runtime_error("Processor '" + string(p) + "' was not found in the configuration");
0175 
0176     return processor->second;
0177   }
0178 
0179   void TriggerSystem::addMask(const char *id, const char *procOrRoleOrDaq) {
0180     // first try to locate a processor with name matching the procOrRoleOrDaq argument
0181     auto processor = procMasks.find(procOrRoleOrDaq);
0182     if (processor != procMasks.end()) {
0183       // processor found -> apply settings to this processor
0184       auto mask = processor->second.find(id);
0185       if (mask != processor->second.end()) {
0186         // setting with this id already exists -> always take the latest value
0187         //if( logs)
0188         //   *logs << "Warning: overriding already existing " << id
0189         //         << " = (" << setting->second.getType() << ") " << setting->second.getValue()
0190         //         << " with new value (" << type << ") " << value << endl;
0191         mask->second = Mask(id, procOrRoleOrDaq);
0192       } else
0193         processor->second.insert(make_pair(string(id), Mask(id, procOrRoleOrDaq)));
0194       // let's run a consistency check
0195       auto p2r = procToRole.find(procOrRoleOrDaq);
0196       if (p2r == procToRole.end())
0197         if (logs)
0198           *logs << "Warning: TriggerSystem object doesn't yet assign "
0199                 << " a role to the processor " << procOrRoleOrDaq << endl;
0200       return;
0201     }
0202 
0203     // if we've got here, the procOrRoleOrDaq argument may have meaning of role
0204     auto role = roleForProcs.find(procOrRoleOrDaq);
0205     if (role != roleForProcs.end()) {
0206       // apply setting on all of the processors for this role
0207       for (auto &proc : role->second) {
0208         auto processor = procMasks.find(proc);
0209         if (processor != procMasks.end()) {
0210           // processor found -> apply settings to this processor
0211           //  unless the setting with such id already exists
0212           auto mask = processor->second.find(id);
0213           if (mask == processor->second.end())
0214             processor->second.insert(make_pair(string(id), Mask(id, procOrRoleOrDaq)));
0215         } else {
0216           // here, copy constructor creates no overhead over the move constructor, whould that be defined
0217           procMasks.insert(make_pair(proc, map<string, Mask>({{id, Mask(id, procOrRoleOrDaq)}})));
0218         }
0219       }
0220       return;
0221     }
0222 
0223     // if we've got here, the procOrRoleOrDaq argument the only choise left is daqttc configuration
0224     //  that, in turn, can again be a daqttc processor or daqttc role
0225     // in either case, for daq we do not have any independent configuration apart from the mask status
0226     //  and the slot location of the processor that is masked (sitting, of course, in the same crate)
0227     auto d2c = daqttcToCrate.find(procOrRoleOrDaq);
0228     if (d2c != daqttcToCrate.end()) {
0229       // now, as we know crate of this daqttc, look for the crate's processors that match the id name
0230       size_t idLen = strlen(id);
0231       string slot = id + (idLen > 2 ? idLen - 2 : 0);  // last two digits of the port comptise the slot #
0232       auto processors = crateForProcs.find(d2c->second);
0233       if (processors != crateForProcs.end()) {
0234         for (auto &proc : processors->second)
0235           if (procToSlot[proc] == slot)
0236             procEnabled[proc] = false;
0237       } else if (logs)
0238         *logs << "Warning: no processors in daqttc crate for " << procOrRoleOrDaq << " ... do nothing" << endl;
0239       return;
0240     }
0241 
0242     // so, finally, this is daqttc role
0243     auto r2d = roleForDaqttcs.find(procOrRoleOrDaq);
0244     if (r2d != roleForDaqttcs.end()) {
0245       for (auto &daq : r2d->second) {
0246         auto processors = crateForProcs.find(daq);
0247         if (processors != crateForProcs.end()) {
0248           for (auto &proc : processors->second)
0249             procEnabled[proc] = false;
0250         } else if (logs)
0251           *logs << "Warning: no processors in daqttc crate " << d2c->second << " for " << procOrRoleOrDaq
0252                 << " ... do nothing" << endl;
0253         return;
0254       }
0255     }
0256 
0257     // if we ever reach here, we've ran out of options
0258     throw runtime_error("Processor/DAQ or Role '" + string(procOrRoleOrDaq) + "' was not found in the map for masking");
0259   }
0260 
0261   const map<string, Mask> &TriggerSystem::getMasks(const char *p) const {
0262     if (!isConfigured)
0263       throw std::runtime_error("TriggerSystem is not configured yet. First call the configureSystem method");
0264 
0265     auto processor = procMasks.find(p);
0266     if (processor == procMasks.end())
0267       throw std::runtime_error("Processor '" + string(p) + "' was not found in the configuration");
0268 
0269     return processor->second;
0270   }
0271 
0272   bool TriggerSystem::isMasked(const char *p, const char *id) const {
0273     const std::map<std::string, Mask> &m = getMasks(p);
0274 
0275     auto mask = m.find(id);
0276     if (mask == m.end())
0277       return false;
0278 
0279     return true;
0280   }
0281 
0282   void TriggerSystem::disableProcOrRoleOrDaq(const char *procOrRoleOrDaq) {
0283     // follow the standard search steps to identify if the argument is processor or role or daqttc processor/role
0284 
0285     // the argument is simply a processor's name
0286     auto processor = procEnabled.find(procOrRoleOrDaq);
0287     if (processor != procEnabled.end()) {
0288       processor->second = false;
0289       return;
0290     }
0291 
0292     // role
0293     auto role = roleForProcs.find(procOrRoleOrDaq);
0294     if (role != roleForProcs.end()) {
0295       // apply setting on all of the processors for this role
0296       for (auto &proc : role->second)
0297         // by design procEnabled must have every single processor for every role
0298         procEnabled[proc] = false;
0299       return;
0300     }
0301 
0302     // the whole daq of a crate is disables -> disable all of the processors in the crate
0303     auto d2c = daqttcToCrate.find(procOrRoleOrDaq);
0304     if (d2c != daqttcToCrate.end()) {
0305       auto processors = crateForProcs.find(d2c->second);
0306       if (processors != crateForProcs.end()) {
0307         for (auto &proc : processors->second)
0308           // by design procEnabled must have every single processor for every crate
0309           procEnabled[proc] = false;
0310       } else if (logs)
0311         *logs << "Warning: no processors in daqttc crate for " << procOrRoleOrDaq << " ... do nothing" << endl;
0312       return;
0313     }
0314 
0315     // so, finally, this is daqttc role
0316     auto r2d = roleForDaqttcs.find(procOrRoleOrDaq);
0317     if (r2d != roleForDaqttcs.end()) {
0318       for (auto &daq : r2d->second) {
0319         auto d2c = daqttcToCrate.find(daq);
0320         if (d2c != daqttcToCrate.end()) {
0321           auto processors = crateForProcs.find(d2c->second);
0322           if (processors != crateForProcs.end()) {
0323             for (auto &proc : processors->second)
0324               procEnabled[proc] = false;
0325           } else if (logs)
0326             *logs << "Warning: no processors in daqttc crate " << d2c->second << " for " << procOrRoleOrDaq
0327                   << " ... do nothing" << endl;
0328         } else if (logs)
0329           *logs << "Warning: daqttc " << daq << " has no crate "
0330                 << " ... do nothing" << endl;
0331         return;
0332       }
0333     }
0334 
0335     // if we ever reach here, we've ran out of options
0336     throw runtime_error("Processor/DAQ or Role '" + string(procOrRoleOrDaq) + "' was not found");
0337   }
0338 
0339   bool TriggerSystem::isProcEnabled(const char *p) const {
0340     if (!isConfigured)
0341       throw std::runtime_error("TriggerSystem is not configured yet. First call the configureSystem method");
0342 
0343     auto processor = procEnabled.find(p);
0344     if (processor == procEnabled.end())
0345       throw runtime_error("Processor '" + string(p) + "' not found");
0346 
0347     return processor->second;
0348   }
0349 
0350 }  // namespace l1t