CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/L1Trigger/RPCTrigger/src/MuonsGrabber.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:     RPCTrigger
00004 // Class  :     MuonsGrabber
00005 // 
00006 // Implementation:
00007 //     <Notes on implementation>
00008 //
00009 // Original Author:  
00010 //         Created:  Thu Sep 17 14:21:01 CEST 2009
00011 // $Id: MuonsGrabber.cc,v 1.2 2009/11/04 13:31:59 fruboes Exp $
00012 //
00013 
00014 // system include files
00015 
00016 // user include files
00017 #include "L1Trigger/RPCTrigger/interface/MuonsGrabber.h"
00018 #include <xercesc/util/PlatformUtils.hpp>
00019 #include <xercesc/util/XMLString.hpp>
00020 #include <xercesc/dom/DOM.hpp>
00021 #include <xercesc/framework/LocalFileFormatTarget.hpp>
00022 #include <sstream>
00023 #include <algorithm>
00024 
00025 XERCES_CPP_NAMESPACE_USE
00026 
00027 class XStr
00028 {
00029 public :
00030     XStr(const char* const toTranscode)
00031     {
00032         fUnicodeForm = XMLString::transcode(toTranscode);
00033     }
00034 
00035     ~XStr()
00036     {
00037         XMLString::release(&fUnicodeForm);
00038     }
00039 
00040     const XMLCh* unicodeForm() const
00041     {
00042         return fUnicodeForm;
00043     }
00044 
00045 private :
00046     XMLCh*   fUnicodeForm;
00047 };
00048 
00049 #define X(str) XStr(str).unicodeForm()
00050 
00051 //
00052 // constants, enums and typedefs
00053 //
00054 
00055 //
00056 // static data member definitions
00057 //
00058 
00059 MuonsGrabber & MuonsGrabber::Instance(){
00060 
00061         static MuonsGrabber grabber;
00062         return grabber;
00063 
00064         
00065 }
00066 
00067 
00068 //
00069 // constructors and destructor
00070 //
00071 MuonsGrabber::MuonsGrabber()
00072 {
00073 
00074     try {
00075        XMLPlatformUtils::Initialize();
00076     }
00077     catch(const XMLException &toCatch)  {
00078       throw std::string("Error during Xerces-c Initialization: "
00079                  + std::string(XMLString::transcode(toCatch.getMessage())));
00080     }
00081  
00082    m_dom = DOMImplementationRegistry::getDOMImplementation(X("Core"));
00083    if (m_dom == 0) throw cms::Exception("RPCMuonsGrabber") << "Cannot get DOM" << std::endl;
00084 
00085    m_doc = m_dom->createDocument(
00086                           0,                    // root element namespace URI.
00087                           X("rpctDataStream"),         // root element name
00088                           0);                   // document type object (DTD).
00089 
00090    m_rootElem = m_doc->getDocumentElement();
00091          
00092    m_currEvent = 0;
00093 }
00094 
00095 
00096 // MuonsGrabber::MuonsGrabber(const MuonsGrabber& rhs)
00097 // {
00098 //    // do actual copying here;
00099 // }
00100 
00101 MuonsGrabber::~MuonsGrabber()
00102 {
00103 
00104   // save xmlfile
00105   XMLCh tempStr[100];
00106   XMLString::transcode("LS", tempStr, 99);
00107   DOMImplementation *impl          = DOMImplementationRegistry::getDOMImplementation(tempStr);
00108   DOMWriter         *theSerializer = ((DOMImplementationLS*)impl)->createDOMWriter();
00109   
00110   theSerializer->setEncoding(X("UTF-8"));
00111 
00112   if (theSerializer->canSetFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true))
00113               theSerializer->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true);
00114 
00115   XMLFormatTarget *myFormTarget = new LocalFileFormatTarget(X("testpulses.xml"));
00116   DOMNode* xmlstylesheet  = m_doc->createProcessingInstruction(X("xml-stylesheet"),
00117                                      X("type=\"text/xsl\"href=\"default.xsl\""));
00118 
00119   m_doc->insertBefore(xmlstylesheet, m_rootElem);
00120   theSerializer->writeNode(myFormTarget, *m_doc);
00121         
00122   delete theSerializer;
00123   delete myFormTarget;
00124   m_doc->release();
00125       
00126 
00127        
00128 
00129 }
00130 
00131 void MuonsGrabber::startNewEvent(int event, int bx) {
00132         
00133      //<event bx="0" num="0">
00134       m_currEvent = m_doc->createElement(X("event"));
00135       m_currEvent->setAttribute(X("num"), X( IntToString(event).c_str())); 
00136       m_currEvent->setAttribute(X("bx"),  X( IntToString(bx).c_str())); 
00137       m_rootElem->appendChild(m_currEvent);  
00138 
00139       
00140       m_currentEvent = event;
00141       m_currentBX = bx;
00142 }
00143 
00144 
00145 void MuonsGrabber::addMuon(RPCTBMuon & mu, int lvl, int region, int hs, int index){
00146         
00147         if (mu.getPtCode()>0) m_muons.push_back( RPCMuonExtraStruct(lvl, region, hs, index, mu) );
00148 }
00149                
00150 void MuonsGrabber::writeDataForRelativeBX(int bx){
00151 
00152   if (m_muons.empty()) return; 
00153 
00154   //<bxData num="11">
00155   DOMElement* currRelBx = m_doc->createElement(X("bxData"));
00156   currRelBx->setAttribute(X("num"), X( IntToString(bx).c_str()));
00157   m_currEvent->appendChild(currRelBx);
00158   
00159   //std::cout << "Writing out for relBx "       << bx << std::endl;
00160  
00161   //   <hs num="1" be="0">
00162   // levels
00163   // 0 - PAC
00164   // 1 - tbgs 
00165   // 2 - tcgs
00166   // 3 - hs
00167   // 4 fs
00168 
00169   std::sort(m_muons.begin(), m_muons.end(), RPCMuonExtraStruct::lvlCompare) ;
00170   for (int tcNum = 0; tcNum <= 11; ++tcNum  ) {
00171     DOMElement* tc = 0;
00172     DOMElement* tcgs = 0;
00173     for (int tbNum = 0; tbNum <= 10; ++tbNum  ) { // check actual range, probably till 9 total 
00174       DOMElement* tb = 0;
00175       DOMElement* tbgs = 0;
00176       for (int PAC = 0; PAC <= 4; ++PAC  ) { // same here
00177         
00178          DOMElement* pac = 0;
00179         // for (int segment = 0; segment <= 11; ++segment ) {      
00180            std::vector< RPCMuonExtraStruct >::iterator it = m_muons.begin();
00181            while ( it != m_muons.end()) {
00182              int muSegment =  it->_mu.getLogSegment();
00183              //if 
00184              
00185              int muTBno = m_trigConfig->getTBNum( it->_mu.getConeCrdnts() );
00186              int muPACChipNo = m_trigConfig->getTowerNumOnTb(it->_mu.getConeCrdnts() );  
00187              int muTC =   m_trigConfig->getTCNum(it->_mu.getConeCrdnts() );
00188              
00189              if ( 
00190                  !( 
00191                         ( int(it->_level) == 0  && tbNum == muTBno &&  muTC == tcNum &&   PAC == muPACChipNo)  
00192                      || ( int(it->_level) == 1  && tbNum == muTBno &&  muTC == tcNum )
00193                      || ( int(it->_level) == 2  &&  muTC == tcNum )  
00194                   )
00195                 )
00196   
00197              {
00198                ++it;
00199                continue;
00200              }
00201             // std::cout << int(it->_level) << int(it->_region) << int(it->_hsHalf)
00202               //         << " " << int(it->_index)
00203                 //       << " " << it->_mu.printDebugInfo(2) << std::endl;
00204              
00205 
00206              if (tc==0) {
00207                tc = m_doc->createElement(X("tc"));
00208                currRelBx->appendChild(tc);
00209                tc->setAttribute(X("num"), X( IntToString(tcNum).c_str()));
00210 
00211                tcgs = m_doc->createElement(X("tcgs"));
00212                tc->appendChild(tcgs);
00213                
00214              }
00215              if (tb==0 && int(it->_level) <= 1) { 
00216                tb = m_doc->createElement(X("tb"));
00217                tc->appendChild(tb);
00218                tb->setAttribute(X("num"), X( IntToString(tbNum).c_str()));
00219                
00220                tbgs = m_doc->createElement(X("tbgs"));
00221                tb->appendChild(tbgs);
00222              }
00223 
00224              if (pac == 0 && int(it->_level) == 0) {
00225                pac =m_doc->createElement(X("pac"));
00226                tb->appendChild(pac);
00227                pac->setAttribute(X("num"), X( IntToString(muPACChipNo).c_str()));
00228              }
00229              
00230              DOMElement* mu = m_doc->createElement(X("mu"));
00231              mu->setAttribute(X("pt"), X( IntToString( int(it->_mu.getPtCode() ) ).c_str()));
00232              mu->setAttribute(X("qual"), X( IntToString( int(it->_mu.getQuality() ) ).c_str()));
00233              mu->setAttribute(X("sign"), X( IntToString( int(it->_mu.getSign() ) ).c_str()));
00234 
00235              if (int(it->_level) == 0 ) {
00236                mu->setAttribute(X("num"), X( IntToString( muSegment ).c_str()));
00237                pac->appendChild(mu); 
00238              } else {
00239                 mu->setAttribute(X("num"), X( IntToString( int(it->_index) ).c_str()));
00240                 mu->setAttribute(X("phi"), X( IntToString( int(it->_mu.getPhiAddr() ) ).c_str()));
00241                 mu->setAttribute(X("eta"), X( IntToString( int(it->_mu.getEtaAddr() ) ).c_str()));
00242                 mu->setAttribute(X("gbD"), X( IntToString( int(it->_mu.getGBData()  ) ).c_str()));
00243                 if (int(it->_level) == 1 ) { 
00244                   tbgs->appendChild(mu); 
00245                 } else if (int(it->_level) == 2 ) {
00246                    tcgs->appendChild(mu);
00247                 } else {
00248                   throw cms::Exception("RPCMuonsGrabber") << "xx Unexpected level" << std::endl;
00249                 }
00250              }
00251 
00252              
00253              it = m_muons.erase(it);             
00254              
00255            } // muons iter
00256         // } // segment
00257       } // PAC 
00258     } // TB
00259   } // TC
00260 
00261   
00262 
00263   
00264   for (int level=3; level<=4;++level)  {
00265     for (int half =0; half <= 1; ++half){
00266       for (int be =0; be <= 1; ++be){ // brl/endcap
00267     
00268       std::vector< RPCMuonExtraStruct >::iterator it = m_muons.begin();
00269       DOMElement* hs = 0;
00270       while ( it != m_muons.end()) {
00271           if ( (int(it->_level) != level) || int(it->_hsHalf)!=half  || int(it->_region)!=be )  {
00272             ++it;
00273             continue;
00274           }
00275           
00276           if (hs == 0) {
00277             if (level == 3) { 
00278               hs = m_doc->createElement(X("hs"));
00279               hs->setAttribute(X("num"), X( IntToString(half).c_str()));
00280             } else  if (level ==4 ) {
00281               hs = m_doc->createElement(X("fs"));
00282             } else { // shoudlnt get here
00283               throw cms::Exception("RPCMuonsGrabber") << "Problem writing out muons - lvl " << level << std::endl;
00284             }
00285             hs->setAttribute(X("be"), X( IntToString(be).c_str()));
00286             currRelBx->appendChild(hs);
00287           }
00288           
00289           DOMElement* mu = m_doc->createElement(X("mu"));
00290           hs->appendChild(mu);
00291           mu->setAttribute(X("num"), X( IntToString( int(it->_index) ).c_str()));
00292           mu->setAttribute(X("pt"), X( IntToString( int(it->_mu.getPtCode() ) ).c_str()));
00293           mu->setAttribute(X("qual"), X( IntToString( int(it->_mu.getQuality() ) ).c_str()));
00294           mu->setAttribute(X("sign"), X( IntToString( int(it->_mu.getSign() ) ).c_str()));
00295           mu->setAttribute(X("phi"), X( IntToString( int(it->_mu.getPhiAddr() ) ).c_str()));
00296           mu->setAttribute(X("eta"), X( IntToString( int(it->_mu.getEtaAddr() ) ).c_str()));
00297           mu->setAttribute(X("gbD"), X( IntToString( int(it->_mu.getGBData()  ) ).c_str()));
00298           
00299             //std::cout << int(it->_level) << int(it->_region) << int(it->_hsHalf) 
00300             //   << " " << int(it->_index) 
00301             //   << " " << it->_mu.printDebugInfo(2) << std::endl;
00302 
00303           it = m_muons.erase(it);
00304 
00305         } // muons iter  
00306       } // be iter
00307     } //half iteration
00308   } // lvl iteration
00309 
00310   if (m_muons.size()!=0) {
00311      throw cms::Exception("RPCMuonsGrabber") << " There are still some muons in muons vec" << std::endl;
00312   
00313   }
00314 }
00315       
00316 
00317 
00318 std::string MuonsGrabber::IntToString(int i){
00319 
00320    std::stringstream ss;
00321     ss << i;
00322 
00323      return ss.str();
00324 
00325 }
00326