CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/src/L1Trigger/GlobalTriggerAnalyzer/src/L1GtPatternGenerator.cc

Go to the documentation of this file.
00001 
00017 // this class header
00018 #include "L1Trigger/GlobalTriggerAnalyzer/interface/L1GtPatternGenerator.h"
00019 
00020 // system include files
00021 #include <memory>
00022 #include <iomanip>
00023 #include <fstream>
00024 
00025 // user include files
00026 #include "DataFormats/L1GlobalCaloTrigger/interface/L1GctCollections.h"
00027 #include "DataFormats/L1GlobalCaloTrigger/interface/L1GctHtMiss.h"
00028 #include "DataFormats/L1GlobalMuonTrigger/interface/L1MuGMTCand.h"
00029 #include "DataFormats/L1GlobalMuonTrigger/interface/L1MuRegionalCand.h"
00030 #include "DataFormats/L1GlobalTrigger/interface/L1GlobalTriggerReadoutRecord.h"
00031 #include "DataFormats/L1GlobalTrigger/interface/L1GtFdlWord.h"
00032 #include "FWCore/Framework/interface/ESHandle.h"
00033 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00034 #include "L1Trigger/GlobalTriggerAnalyzer/interface/L1GtUtils.h"
00035 #include "L1Trigger/GlobalTriggerAnalyzer/interface/L1GtPatternMap.h"
00036 #include "L1Trigger/GlobalTriggerAnalyzer/interface/L1GtPatternWriter.h"
00037 
00038 // constructor
00039 L1GtPatternGenerator::L1GtPatternGenerator(const edm::ParameterSet& parSet)
00040 {
00041     // input tags for trigger records
00042     m_gctTag = parSet.getParameter<edm::InputTag>("GctInputTag");
00043     m_gmtTag = parSet.getParameter<edm::InputTag>("GmtInputTag");
00044     m_gtTag = parSet.getParameter<edm::InputTag>("GtInputTag");
00045     m_dtTag = parSet.getParameter<edm::InputTag>("DtInputTag");
00046     m_cscTag = parSet.getParameter<edm::InputTag>("CscInputTag");
00047     m_rpcbTag = parSet.getParameter<edm::InputTag>("RpcbInputTag");
00048     m_rpcfTag = parSet.getParameter<edm::InputTag>("RpcfInputTag");
00049 
00050     // output formatting stuff
00051     m_header = parSet.getParameter<std::string>("PatternFileHeader");
00052     m_footer = parSet.getParameter<std::string>("PatternFileFooter");
00053     m_columnNames = parSet.getParameter<std::vector<std::string> >("PatternFileColumns");
00054     m_columnLengths = parSet.getParameter<std::vector<uint32_t> >("PatternFileLengths");
00055     m_columnDefaults = parSet.getParameter<std::vector<uint32_t> >("PatternFileDefaultValues");
00056     m_fileName = parSet.getParameter<std::string>("PatternFileName");
00057     m_bx = parSet.getParameter<std::vector<int> >("bx");
00058     m_debug = parSet.getParameter<bool>("DebugOutput");
00059 
00060 
00061     if(m_columnLengths.size() != m_columnNames.size()) { 
00062       edm::LogWarning("L1GtPatternGenerator") 
00063         << "Length of PatternFileColumns does not match length of PatternFileLenghts, " <<
00064         m_columnNames.size() << " vs " << m_columnLengths.size() << std::endl;
00065     }
00066 
00067     LogDebug("L1GtPatternGenerator")
00068       << "\nL1 GCT  record:            "
00069       << m_gctTag 
00070       << "\nL1 GMT record:             "
00071       << m_gmtTag
00072       << "\nL1 GT record:              "
00073       << m_gtTag << std::endl;
00074 }
00075 
00076 // destructor
00077 L1GtPatternGenerator::~L1GtPatternGenerator()
00078 {}
00079 
00080 // local helper functions
00081 
00098 template <class TRecord, typename TResult> static void extractRecordData(const edm::Event& iEvent,
00099                                                                          L1GtPatternMap& allPatterns,
00100                                                                          const std::string& label, 
00101                                                                          const std::string& instance, 
00102                                                                          TResult (TRecord::*rawFunctionPtr)() const,
00103                                                                          const std::string& prefix,
00104                                                                          uint32_t (*packingFunction)(uint32_t) = NULL)
00105 {
00106   uint32_t valueCount;
00107 
00108   // Extract record from event.
00109   edm::Handle<std::vector<TRecord> > handle;
00110   iEvent.getByLabel(label, instance, handle);
00111 
00112   if(!handle.isValid()) {
00113     throw cms::Exception(__func__) << "Failed to extract record of type " << typeid(TRecord).name() <<
00114       " labeled " << label << ", instance " << instance;
00115   }
00116 
00117   edm::EventNumber_t eventNr = iEvent.id().event();
00118 
00119   // Then loop over collection and add each event to the map.
00120   for(typename std::vector<TRecord>::const_iterator it = handle->begin(); it != handle->end(); ++it) {
00121     int bx = it->bx();
00122     L1GtPatternLine& line = allPatterns.getLine(eventNr, bx);
00123     uint32_t value = ((*it).*rawFunctionPtr)();
00124     if(packingFunction != NULL) { 
00125         value = packingFunction(value);
00126     } 
00127 
00128     line.push(prefix, value);
00129     ++valueCount;
00130   }
00131 }
00132 
00133 /*** Convert a vector of bools into a vector of uint32_ts. Probably
00134      optimizable, but let's just trust that it doesn't matter... */
00135 static std::vector<uint32_t> chopWords(const std::vector<bool>& aWord) {
00136   std::vector<uint32_t> result;
00137 
00138   result.resize((aWord.size()+31)/32, 0);
00139 
00140   for(unsigned i = 0 ; i < aWord.size(); ++i) {
00141     result[i/32] |= aWord[i] << (i%32);
00142   }
00143 
00144   return result;
00145 }
00146 
00150 static void extractGlobalTriggerWord(const std::vector<bool> input, L1GtPatternLine& line, const std::string& prefix)
00151 {  
00152   std::vector<uint32_t> resultWords = chopWords(input);
00153 
00154   // add in reverse order, so that higher-order words have lower indices 
00155   // (like in "natural" number representation) (10 -> digit1=1 digit2=0)
00156   for(unsigned i = resultWords.size() ; i > 0; --i) {
00157     line.push(prefix, resultWords[i-1]);
00158   }
00159 }
00160 
00164 uint32_t L1GtPatternGenerator::packRegionalMuons(uint32_t rawData) {
00165     uint32_t invertMask = 0x0000FF00;
00166     uint32_t toKeep =   rawData & (~invertMask);
00167     return toKeep | (~rawData & invertMask);
00168 }
00169 
00170 
00171 // member functions
00172 void L1GtPatternGenerator::extractGlobalTriggerData(const edm::Event& iEvent, L1GtPatternMap& patterns) {
00173 
00174   // extract global trigger readout record
00175   edm::Handle<L1GlobalTriggerReadoutRecord> handle;
00176   iEvent.getByLabel(m_gtTag, handle);
00177 
00178   // continue if it's present
00179   if(!handle.isValid()) { 
00180     throw cms::Exception(__func__) << "Failed to extract GT readout record labeled " 
00181                                    << m_gtTag.label() << ", instance " << m_gtTag.instance();
00182   }
00183 
00184   edm::EventNumber_t eventNr = iEvent.id().event();
00185 
00186   // for each FDL word...
00187   const std::vector<L1GtFdlWord>& fdlWords = handle->gtFdlVector();
00188   for(std::vector<L1GtFdlWord>::const_iterator it = fdlWords.begin();
00189       it != fdlWords.end() ; ++it) { 
00190     // extract relevant data
00191     int bx = it->bxInEvent();
00192 
00193     // find matching pattern file line
00194     L1GtPatternLine& line = patterns.getLine(eventNr, bx);
00195 
00196     extractGlobalTriggerWord(it->gtDecisionWord(), line, "gtDecision");
00197     extractGlobalTriggerWord(it->gtDecisionWordExtended(), line, "gtDecisionExt");
00198     extractGlobalTriggerWord(it->gtTechnicalTriggerWord(), line, "gtTechTrigger");
00199 
00200     line.push("gtFinalOr", it->finalOR());
00201   }
00202 }
00203 
00207 void L1GtPatternGenerator::packHfRecords(const std::string& resultName, L1GtPatternMap& allPatterns)
00208 {
00209   // iterate over each pattern line
00210   for(L1GtPatternMap::LineMap::iterator it = allPatterns.begin(); 
00211       it != allPatterns.end(); ++it) { 
00212     // Get the HF bit counts and ring sums 
00213     uint32_t counts = it->second.get("hfBitCounts1");
00214     uint32_t sums = it->second.get("hfRingEtSums1");
00215     
00216     
00217     // Bits 0..11 -> 4 bit counts
00218     uint32_t hfPsbValue = (counts & 0xFFF) |
00219         // Bit 12..14 ring 1 pos. rap. HF Et sum
00220         (sums & 0x7) << 12 | 
00221         // Bits 16.. rest of the ET sums
00222         (sums >> 3) << 16;
00223     // TODO: Spec states non-data values for Bits 15, 31, 47 and 63.
00224 
00225     // Export computed value to pattern writer. */
00226     it->second.push(resultName, hfPsbValue);
00227   }
00228 }
00229 
00235 void L1GtPatternGenerator::analyze(const edm::Event& iEvent, const edm::EventSetup& evSetup)
00236 {
00237    L1GtPatternMap allPatterns;
00238 
00239   // GMT muon candidates
00240   extractRecordData(iEvent, allPatterns, m_gmtTag.label(), m_gmtTag.instance(), &L1MuGMTCand::getDataWord, "gmtMuon");
00241 
00242   // regional muon candidates
00243   extractRecordData(iEvent, allPatterns, m_cscTag.label(), m_cscTag.instance(), &L1MuRegionalCand::getDataWord, "cscMuon", packRegionalMuons);  
00244   extractRecordData(iEvent, allPatterns, m_dtTag.label(),  m_dtTag.instance(), &L1MuRegionalCand::getDataWord, "dtMuon", packRegionalMuons);
00245   extractRecordData(iEvent, allPatterns, m_rpcfTag.label(), m_rpcfTag.instance(), &L1MuRegionalCand::getDataWord, "fwdMuon", packRegionalMuons);
00246   extractRecordData(iEvent, allPatterns, m_rpcbTag.label(), m_rpcbTag.instance(), &L1MuRegionalCand::getDataWord, "brlMuon", packRegionalMuons);
00247 
00248   // GCT objects
00249   extractRecordData(iEvent, allPatterns, m_gctTag.label(), "nonIsoEm", &L1GctEmCand::raw, "gctEm");
00250   extractRecordData(iEvent, allPatterns, m_gctTag.label(), "isoEm", &L1GctEmCand::raw, "gctIsoEm");
00251   extractRecordData(iEvent, allPatterns, m_gctTag.label(), "", &L1GctEtMiss::et, "etMiss");
00252   extractRecordData(iEvent, allPatterns, m_gctTag.label(), "", &L1GctEtMiss::phi, "etMissPhi");
00253   extractRecordData(iEvent, allPatterns, m_gctTag.label(), "", &L1GctHtMiss::raw, "htMiss");
00254   extractRecordData(iEvent, allPatterns, m_gctTag.label(), "", &L1GctEtHad::raw, "etHad");
00255   extractRecordData(iEvent, allPatterns, m_gctTag.label(), "", &L1GctEtTotal::raw, "etTotal");
00256   extractRecordData(iEvent, allPatterns, m_gctTag.label(), "cenJets", &L1GctJetCand::raw, "cenJet");
00257   extractRecordData(iEvent, allPatterns, m_gctTag.label(), "forJets", &L1GctJetCand::raw, "forJet");
00258   extractRecordData(iEvent, allPatterns, m_gctTag.label(), "tauJets", &L1GctJetCand::raw, "tauJet");
00259   extractRecordData(iEvent, allPatterns, m_gctTag.label(), "", &L1GctHFBitCounts::raw, "hfBitCounts");
00260   extractRecordData(iEvent, allPatterns, m_gctTag.label(), "", &L1GctHFRingEtSums::raw, "hfRingEtSums");
00261 
00262   // Post processing: 
00263   // HFBitCounts/HFRingEtSums need to be mangled to PSB values
00264   packHfRecords("hfPsbValue", allPatterns);
00265 
00266   // GT objects
00267   extractGlobalTriggerData(iEvent, allPatterns);
00268 
00269   // Output
00270   m_writer->writePatterns(allPatterns);
00271 }
00272 
00276 void L1GtPatternGenerator::beginJob()
00277 {
00278   m_fileStream.open(m_fileName.c_str());
00279 
00280   if(!m_fileStream) { 
00281     edm::LogError("L1GtPatternGenerator") <<  "Failed to open output file " << m_fileName;
00282   }
00283 
00284   m_writer = std::auto_ptr<L1GtPatternWriter>(new L1GtPatternWriter(m_fileStream, m_header, m_footer, m_columnNames, m_columnLengths, m_columnDefaults, m_bx, m_debug));
00285 }
00286 
00290 void L1GtPatternGenerator::endJob()
00291 {
00292   m_writer->close();
00293   m_fileStream.close();
00294 }