00001
00002 #include <stdexcept>
00003 #include <memory>
00004 #include <fstream>
00005 #include <iostream>
00006 #include <iomanip>
00007 #include <string>
00008 #include <cctype>
00009 #include <vector>
00010 #include <bitset>
00011 #include <ctime>
00012
00013 #include "IORawData/RPCFileReader/interface/RPCFileReader.h"
00014 #include "IORawData/RPCFileReader/interface/RPCPacData.h"
00015
00016 #include "DataFormats/FEDRawData/interface/FEDRawData.h"
00017 #include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
00018
00019 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00020 #include "DataFormats/L1GlobalMuonTrigger/interface/L1MuRegionalCand.h"
00021
00022
00023 using namespace std;
00024 using namespace edm;
00025
00026
00027 RPCFileReader::RPCFileReader(ParameterSet const& pset,
00028 InputSourceDescription const& desc):
00029 ExternalInputSource(pset, desc)
00030 {
00031
00032
00033 produces<FEDRawDataCollection>();
00034 produces<std::vector<L1MuRegionalCand> >("MTCCData");
00035
00036
00037
00038
00039 eventPos_[0]=0; eventPos_[1]=0;
00040 eventCounter_=0; fileCounter_ = 0;
00041 run_ = 1; event_ = 1; bxn_ =1;
00042 isOpen_ = false; noMoreData_ = false;
00043
00044 debug_ = pset.getUntrackedParameter<bool>("PrintOut",false);
00045 saveOutOfTime_ = pset.getUntrackedParameter<bool>("SaveOutOfTimeDigis",false);
00046 pacTrigger_ = pset.getUntrackedParameter<bool>("IsPACTrigger",false);
00047 triggerFedId_ = pset.getUntrackedParameter<unsigned int>("TriggerFedId",790);
00048 const vector<unsigned int> tbNumdef(1,1);
00049 tbNums_ = pset.getUntrackedParameter<std::vector<unsigned int> >("TriggerBoardNums",tbNumdef);
00050 maskChannels_ = pset.getUntrackedParameter<bool>("MaskChannels",false);
00051 if(tbNums_.size()==0)tbNums_.push_back(1);
00052
00053
00054 for(int k=0;k<(int)tbNums_.size();k++){
00055 vector<LogCone> lcv(12);
00056 theLogCones_.push_back(lcv);
00057 vector<RPCPacData> rpdv(3,RPCPacData(0));
00058 vector<vector<RPCPacData> > rpdvv(18,rpdv);
00059 vector<vector<vector<RPCPacData> > > rpdvvv(LAST_BX-FIRST_BX,rpdvv);
00060 linkData_.push_back(rpdvvv);
00061 }
00062
00063 vector<bool> bv(18,0);
00064 for(unsigned int iChip=0;iChip<tbNums_.size();iChip++){
00065 maskedChannels.push_back(bv);
00066 }
00067 if(maskChannels_){
00068 ifstream mfile;
00069 mfile.open("masks.dat");
00070
00071
00072 if((mfile.rdstate()&ifstream::failbit)!= 0){
00073 std::cerr << "Error opening masks.dat" << std::endl;
00074 edm::LogError("RPCFR")<< "[RPCFileReader::RPCFileReader] "
00075 << "Error opening file with masks definition";
00076 mfile.close();
00077 }else{
00078 int chan;
00079 for(unsigned int iChip=0;iChip<tbNums_.size();iChip++){
00080 for(unsigned int iL=0;iL<18;iL++){
00081 mfile >> chan;
00082 maskedChannels[iChip][iL]=(bool)chan;
00083 }
00084 }
00085 mfile.close();
00086 }
00087 if(debug_){
00088 edm::LogInfo("RPCFR")<< "[RPCFileReader::RPCFileReader] Masked channels:";
00089 for(unsigned int iChip=0;iChip<tbNums_.size();iChip++){
00090 ostringstream ostr;
00091 ostr << iChip <<". (DCC channel = " << tbNums_[iChip]<<"): ";
00092 for(int iL=0;iL<18;iL++){
00093 ostr<<(int)maskedChannels[iChip][iL]<<" ";
00094 }
00095 edm::LogInfo("RPCFR")<<ostr.str();
00096 }
00097 }
00098 }
00099 }
00100
00101
00102 RPCFileReader::~RPCFileReader(){}
00103
00104
00105 void RPCFileReader::setRunAndEventInfo(){
00106
00107
00108 bool triggeredOrEmpty = false;
00109 tm aTime;
00110
00111 while(!triggeredOrEmpty){
00112 if(!isOpen_){
00113 if(fileCounter_<(int)fileNames().size()){
00114 eventPos_[0]=0; eventPos_[1]=0;
00115 isOpen_=true;
00116 fileCounter_++;
00117 edm::LogInfo("RPCFR")<< "[RPCFileReader::setRunAndEventInfo] "
00118 << "Open for reading file no. " << fileCounter_
00119 << " " << fileNames()[fileCounter_-1].substr(5);
00120 }
00121 else{
00122 edm::LogInfo("RPCFR")<< "[RPCFileReader::setRunAndEventInfo] "
00123 << "No more events to read. Finishing after "
00124 << eventCounter_ << " events.";
00125 noMoreData_=true;
00126 return;
00127 }
00128 }
00129
00130
00131 for(int iChip=0;iChip<(int)tbNums_.size();iChip++){
00132 for(int iBX=0;iBX<8;iBX++){
00133 for(int iL=0;iL<18;iL++){
00134 for(int iLB=0;iLB<3;iLB++){
00135 linkData_[iChip][iBX][iL][iLB] = RPCPacData();
00136 }
00137 }
00138 }
00139 }
00140
00141 readDataFromAsciiFile(fileNames()[fileCounter_-1].substr(5),eventPos_);
00142
00143 bool isPacTrigger = false;
00144 for(int iChip=0; iChip<(int)tbNums_.size(); iChip++){
00145 for(int iCone=0; iCone<12; iCone++){
00146 isPacTrigger = (isPacTrigger||theLogCones_[iChip][iCone].ptCode);
00147 }
00148 }
00149 if(!(pacTrigger_)||isPacTrigger){
00150
00151 triggeredOrEmpty = true;
00152 }
00153 else{
00154 if(debug_)
00155 edm::LogInfo("RPCFR")<< "[RPCFileReader::setRunAndEventInfo] "
00156 << " Data not triggered by PAC!";
00157 triggeredOrEmpty = false;
00158 }
00159
00160 if(!isOpen_){
00161 triggeredOrEmpty = false;
00162 }
00163
00164 }
00165 unsigned int freq = 100;
00166 if(debug_) freq = 1;
00167 if(eventCounter_%freq==0)
00168 edm::LogInfo("RPCFR") << "[RPCFileReader::setRunAndEventInfo] "
00169 << " #" << eventCounter_ << " Run: "<< run_
00170 << " Event: " << event_ << " Bx = " << bxn_
00171 << " Time Stamp: " << timeStamp_.hour << ":"
00172 << timeStamp_.min << ":" << timeStamp_.sec
00173 << " " << timeStamp_.day << " " << timeStamp_.month
00174 << " " << timeStamp_.year;
00175
00176
00177
00178 int month = 0;
00179 if(timeStamp_.month=="Jan") month = 0;
00180 if(timeStamp_.month=="Feb") month = 1;
00181 if(timeStamp_.month=="March") month = 2;
00182 if(timeStamp_.month=="May") month = 3;
00183 if(timeStamp_.month=="April") month = 4;
00184 if(timeStamp_.month=="June") month = 5;
00185 if(timeStamp_.month=="July") month = 6;
00186 if(timeStamp_.month=="Aug") month = 7;
00187 if(timeStamp_.month=="Sep") month = 8;
00188 if(timeStamp_.month=="Nov") month = 9;
00189 if(timeStamp_.month=="Oct") month = 10;
00190 if(timeStamp_.month=="Dec") month = 11;
00191 aTime.tm_sec = timeStamp_.sec;
00192 aTime.tm_min = timeStamp_.min;
00193 aTime.tm_hour = timeStamp_.hour;
00194 aTime.tm_mday = timeStamp_.day;
00195 aTime.tm_mon = month;
00196 aTime.tm_year = timeStamp_.year - 1900;
00197
00198
00199 setRunNumber(run_);
00200 setEventNumber(event_);
00201 setTime(mktime(&aTime));
00202
00203 return;
00204 }
00205
00206
00207 bool RPCFileReader::produce(edm::Event &ev) {
00208 std::auto_ptr<FEDRawDataCollection> result(new FEDRawDataCollection);
00209
00210
00211 if(noMoreData_) return false;
00212
00213 unsigned int freq = 100;
00214 if(debug_) freq = 1;
00215 if(eventCounter_%freq==0)
00216 edm::LogInfo("RPCFR") << "[RPCFileReader::produce] "
00217 << " #" << eventCounter_ << " Run: "<< run_
00218 << " Event: " << event_ << " Bx = " << bxn_
00219 << " Time Stamp: " << timeStamp_.hour << ":"
00220 << timeStamp_.min << ":" << timeStamp_.sec
00221 << " " << timeStamp_.day << " " << timeStamp_.month
00222 << " " << timeStamp_.year;
00223
00224
00225 FEDRawData *rawData = rpcDataFormatter();
00226 FEDRawData& fedRawData = result->FEDData(triggerFedId_);
00227 fedRawData = *rawData;
00228 ev.put(result);
00229
00230
00231 float phi = 0;
00232 float eta = 0;
00233 std::vector<L1MuRegionalCand> RPCCand;
00234 for(unsigned int iChip=0;iChip<tbNums_.size();iChip++){
00235 for(unsigned int i=0;i<12;i++){
00236 if(!theLogCones_[iChip][i].ptCode) continue;
00237 L1MuRegionalCand l1Cand;
00238 l1Cand.setQualityPacked(theLogCones_[iChip][i].quality);
00239 l1Cand.setPtPacked(theLogCones_[iChip][i].ptCode);
00240 l1Cand.setType(0);
00241 l1Cand.setChargePacked(1);
00242 l1Cand.setPhiValue(phi);
00243 l1Cand.setEtaValue(eta);
00244 RPCCand.push_back(l1Cand);
00245 }
00246 }
00247
00248 std::auto_ptr<std::vector<L1MuRegionalCand> > candBarell(new std::vector<L1MuRegionalCand>);
00249 candBarell->insert(candBarell->end(), RPCCand.begin(), RPCCand.end());
00250 ev.put(candBarell, "MTCCData");
00251
00252 return true;
00253 }
00254
00255
00256 void RPCFileReader::readDataFromAsciiFile(string fileName, int *pos){
00257
00258 if(!pos){
00259 edm::LogError("RPCFR")<< "[RPCFileReader::readDataFromAsciiFile] "
00260 << "Unknown event position; Abort";
00261 return;
00262 }
00263
00264 ifstream infile;
00265 infile.open(fileName.c_str(),ios::binary);
00266
00267
00268 if((infile.rdstate()&ifstream::failbit)!= 0){
00269 std::cerr << "Error opening " << fileName << std::endl;
00270 edm::LogError("RPCFR")<< "[RPCFileReader::readDataFromAsciiFile] "
00271 << "Error opening data file " << fileName;
00272 isOpen_=false;
00273 infile.close();
00274
00275 return;
00276 }
00277
00278 string dummy;
00279 int idummy;
00280 int iChip=0;
00281
00282 bool currentEvent=true;
00283
00284 infile.seekg(pos[0]);
00285
00286 while(infile >> setw(0)>> dummy){
00287 if(dummy=="Beginning"){
00288 infile >> dummy;
00289 if(dummy=="Event"){
00290 if(!currentEvent) {
00291 pos[0]=pos[1];
00292 break;
00293 }
00294 infile >> dummy >> dec >> event_;
00295 infile >> dummy >> dec >> bxn_;
00296 infile >> dummy >> dummy >> dec >> timeStamp_.month >> timeStamp_.day;
00297 infile >> setw(2) >> dec >> timeStamp_.hour;
00298 infile.ignore(1);
00299 infile >> setw(2) >> dec >> timeStamp_.min;
00300 infile.ignore(1);
00301 infile >> setw(2) >> dec >> timeStamp_.sec;
00302 infile >> dec >> timeStamp_.year;
00303 currentEvent=false;
00304 eventCounter_++;
00305 }
00306 else if(dummy=="Header"){
00307 infile >> dummy >> dec >> run_;
00308 }
00309 else{
00310 if(debug_)
00311 edm::LogInfo("RPCFR")<< "[RPCFileReader::readDataFromAsciiFile] "
00312 << "Unrecognized header: "<< dummy
00313 << " Skipping ";
00314 infile.ignore(10000,10);
00315 }
00316 }
00317 else if((dummy=="PAC")||(dummy=="RMB")){
00318 infile >> iChip;
00319 if(debug_)
00320 edm::LogInfo("RPCFR")<< "[RPCFileReader::readDataFromAsciiFile] "
00321 << "Readout from "<< dummy << " " << iChip;
00322 }
00323 else if(isHexNumber(dummy)){
00324 int bxLocal = atoi(dummy.c_str());
00325 int bxn4b, bc0, valid;
00326 const int valid_mask[18] = {0x20000,0x10000,0x8000,0x4000,
00327 0x2000, 0x1000, 0x800, 0x400,
00328 0x200, 0x100, 0x80, 0x40,
00329 0x20, 0x10, 0x8 , 0x4,
00330 0x2, 0x1};
00331 infile >> hex >> bxn4b >> bc0 >> valid;
00332 for(int iL=17; iL>=0; iL-=3){
00333 infile >> dummy;
00334 for(int iLb=0;iLb>-3;iLb--){
00335 infile >> hex >> idummy;
00336 RPCPacData dummyPartData;
00337 if(((valid&valid_mask[17-(iL+iLb)])>>(iL+iLb))==1){
00338 dummyPartData.fromRaw(idummy);
00339
00340 }
00341 else{
00342 if(debug_)
00343 edm::LogInfo("RPCFR")<< "[RPCFileReader::readDataFromAsciiFile] "
00344 << " Not valid data for link no. = " << iL+iLb
00345 << " Zeroing partition data.";
00346 }
00347 for(int iBX=FIRST_BX;iBX<LAST_BX;iBX++){
00348 if(bxLocal-(int)dummyPartData.partitionDelay()==RPC_PAC_L1ACCEPT_BX+iBX){
00349 linkData_[iChip][iBX-FIRST_BX][iL+iLb][dummyPartData.lbNum()]=dummyPartData;
00350 }
00351 }
00352 }
00353 }
00354 for(int iC=12*3-1; iC>=0; iC-=3){
00355 infile >> dummy;
00356 LogCone logCone;
00357 infile >> hex >> logCone.quality >> logCone.ptCode >> logCone.sign;
00358 if(logCone.ptCode){
00359 edm::LogInfo("Any time LogCone")<<"Log cone: Q: "<<logCone.quality
00360 <<" ptCode: "<<logCone.ptCode
00361 <<" sign: "<<logCone.sign<<std::endl;
00362 }
00363 if(bxLocal==RPC_PAC_L1ACCEPT_BX+RPC_PAC_TRIGGER_DELAY){
00364 theLogCones_[iChip][iC/3]=logCone;
00365 if(logCone.ptCode){
00366 edm::LogInfo("In time LogCone")<<"in time Log cone: Q: "<<logCone.quality
00367 <<" ptCode: "<<logCone.ptCode
00368 <<" sign: "<<logCone.sign<<std::endl;
00369 }
00370 }
00371 }
00372 infile >> dummy;
00373 pos[1]=infile.tellg();
00374 }
00375 else{
00376 if(debug_)
00377 edm::LogInfo("RPCFR")<< "[RPCFileReader::readDataFromAsciiFile] "
00378 << "Unrecognized begining of record: " << dummy
00379 << " Skipping record.";
00380 infile.ignore(10000,10);
00381 }
00382 }
00383 if(debug_)
00384 edm::LogInfo("RPCFR") << "[RPCFileReader::readDataFromAsciiFile] "
00385 << " #" << eventCounter_ << " Run: "<< run_
00386 << " Event: " << event_ << " Bx = " << bxn_
00387 << " Time Stamp: " << timeStamp_.hour << ":"
00388 << timeStamp_.min << ":" << timeStamp_.sec
00389 << " " << timeStamp_.day << " " << timeStamp_.month
00390 << " " << timeStamp_.year;
00391 if(infile.eof()){
00392 isOpen_=false;
00393 }
00394 else{
00395 isOpen_=true;
00396 }
00397
00398 infile.close();
00399
00400 return;
00401 }
00402
00403
00404 RPCFileReader::Word16 RPCFileReader::buildCDWord(RPCPacData linkData){
00405
00406 Word16 word = (Word16(linkData.lbNum())<<14)
00407 |(Word16(linkData.partitionNum())<<10)
00408 |(Word16(linkData.endOfData())<<9)
00409 |(Word16(linkData.halfPartition())<<8)
00410 |(Word16(linkData.partitionData())<<0);
00411 if(debug_){
00412 edm::LogInfo("RPCFR") << "[RPCFileReader::buildCDWord] ";
00413 }
00414 return word;
00415 }
00416
00417 RPCFileReader::Word16 RPCFileReader::buildSLDWord(unsigned int tbNum, unsigned int linkNum){
00418
00419 Word16 specIdent = 3;
00420 Word16 word = (specIdent<<14)
00421 |(1<<13)|(1<<12)|(1<<11)
00422 |(Word16(tbNum)<<5)
00423 |(Word16(linkNum)<<0);
00424 if(debug_){
00425 edm::LogInfo("RPCFR") << "[RPCFileReader::buildSLDWord] ";
00426 }
00427 return word;
00428 }
00429
00430 RPCFileReader::Word16 RPCFileReader::buildSBXDWord(unsigned int bxn){
00431 Word16 specIdent = 3;
00432
00433 Word16 word = (specIdent<<14)
00434 |(0<<13)|(1<<12)
00435 |(Word16(bxn)<<0);
00436 if(debug_){
00437 edm::LogInfo("RPCFR") << "[RPCFileReader::buildSBXDWord] ";
00438 }
00439 return word;
00440 }
00441
00442 RPCFileReader::Word16 RPCFileReader::buildEmptyWord(){
00443 Word16 specIdent = 3;
00444 Word16 word = 0;
00445 word = (specIdent<<14)
00446 |(1<<13)|(0<<12)|(1<<11);
00447 if(debug_){
00448 edm::LogInfo("RPCFR") << "[RPCFileReader::buildEmptyWord] ";
00449 }
00450 return word;
00451 }
00452
00453
00454 FEDRawData* RPCFileReader::rpcDataFormatter(){
00455
00456 std::vector<Word16> words;
00457 bool empty=true;
00458
00459 int beginBX = 0;
00460 int endBX = 1;
00461 if(saveOutOfTime_){
00462
00463
00464 beginBX = FIRST_BX;
00465 endBX = LAST_BX;
00466 }
00467 for(unsigned int iChip=0;iChip<tbNums_.size();iChip++){
00468 for(int iBX=beginBX;iBX<endBX;iBX++){
00469
00470 for(unsigned int iL=0; iL<18; iL++){
00471 for(unsigned int iLb=0; iLb<3; iLb++){
00472 if((linkData_[iChip][iBX-FIRST_BX][iL][iLb].partitionData()!=0)&&
00473 !(maskedChannels[iChip][iL]))
00474 empty=false;
00475 }
00476 }
00477 if(!empty){
00478
00479 if(bxn_+iBX>=0)
00480 words.push_back(buildSBXDWord((unsigned int)(bxn_+iBX)));
00481 else
00482 continue;
00483 for(unsigned int iL=0; iL<18; iL++){
00484
00485 empty=true;
00486 for(unsigned int iLb=0; iLb<3; iLb++){
00487 if((linkData_[iChip][iBX-FIRST_BX][iL][iLb].partitionData()!=0)&&
00488 !(maskedChannels[iChip][iL]))
00489 empty=false;
00490 }
00491 if(!empty){
00492 words.push_back(buildSLDWord(tbNums_[iChip], iL));
00493 for(unsigned int iLb=0; iLb<3; iLb++){
00494 if(linkData_[iChip][iBX-FIRST_BX][iL][iLb].partitionData()!=0){
00495 words.push_back(buildCDWord(linkData_[iChip][iBX-FIRST_BX][iL][iLb]));
00496 }
00497 }
00498 }
00499 }
00500 }
00501
00502 while(words.size()%4!=0){
00503 words.push_back(buildEmptyWord());
00504 }
00505 }
00506 }
00507
00508 if(debug_){
00509 edm::LogInfo("RPCFR") << "[RPCFileReader::rpcDataFormater] Num of words: " << words.size();
00510 }
00511
00512 int dataSize = words.size()*sizeof(Word16);
00513 FEDRawData *rawData = new FEDRawData(dataSize+2*sizeof(Word64));
00514 Word64 *word = reinterpret_cast<Word64*>(rawData->data());
00515
00516 *word = Word64(0);
00517 *word = (Word64(0x5)<<60)|(Word64(0x1)<<56)|(Word64(event_)<<32)
00518 |(Word64(bxn_)<<20)|((triggerFedId_)<<8);
00519 if(debug_){
00520 edm::LogInfo("RPCFR") << "[RPCFileReader::rpcDataFormater] Header: " << *reinterpret_cast<bitset<64>* >(word);
00521 }
00522 word++;
00523
00524 for(unsigned int i=0; i<words.size(); i+=4){
00525 *word = (Word64(words[i]) <<48)
00526 |(Word64(words[i+1])<<32)
00527 |(Word64(words[i+2])<<16)
00528 |(Word64(words[i+3]) );
00529 if(debug_){
00530 edm::LogInfo("RPCFR") << "[RPCFileReader::rpcDataFormater] Word64: " << *reinterpret_cast<bitset<64>* >(word);
00531 }
00532 word++;
00533 }
00534
00535 *word = Word64(0);
00536 *word = (Word64(0xa)<<60)|(Word64(0x0)<<56)|(Word64(2+words.size()/4)<<32)
00537 |(0xf<<8)|(0x0<<4);
00538 if(debug_){
00539 edm::LogInfo("RPCFR") << "[RPCFileReader::rpcDataFormater] Trailer: " << *reinterpret_cast<bitset<64>* >(word);
00540 }
00541 word++;
00542
00543
00544 if(word!=reinterpret_cast<Word64*>(rawData->data()+dataSize+2*sizeof(Word64))){
00545 string warn = "ERROR !!! Problem with memory in RPCFileReader::rpcDataFormater !!!";
00546 throw cms::Exception(warn);
00547 }
00548
00549 return rawData;
00550 }