Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:02:45

0001 /** \class DTROSChannelId
0002  *
0003  *  See header file for a description of this class.
0004  *
0005  *  $Date: 2009/03/26 14:11:04 $
0006  *  $Revision: 1.1 $
0007  *  \author Paolo Ronchese INFN Padova
0008  *
0009  */
0010 
0011 //----------------------
0012 // This Class' Header --
0013 //----------------------
0014 #include "CondTools/DT/interface/DTCompactMapWriter.h"
0015 
0016 //-------------------------------
0017 // Collaborating Class Headers --
0018 //-------------------------------
0019 
0020 //---------------
0021 // C++ Headers --
0022 //---------------
0023 #include <string>
0024 #include <vector>
0025 #include <map>
0026 #include <algorithm>
0027 #include <iostream>
0028 #include <fstream>
0029 
0030 class DTROSChannelId {
0031 public:
0032   DTROSChannelId(int ddu, int ros, int channel);
0033   int dduId() const;
0034   int rosId() const;
0035   int channelId() const;
0036 
0037 private:
0038   int ddu_id;
0039   int ros_id;
0040   int channel_id;
0041 };
0042 
0043 DTROSChannelId::DTROSChannelId(int ddu, int ros, int channel) : ddu_id(ddu), ros_id(ros), channel_id(channel) {}
0044 
0045 int DTROSChannelId::dduId() const { return ddu_id; }
0046 
0047 int DTROSChannelId::rosId() const { return ros_id; }
0048 
0049 int DTROSChannelId::channelId() const { return channel_id; }
0050 
0051 class DTROSChannelCompare {
0052 public:
0053   bool operator()(const DTROSChannelId& idl, const DTROSChannelId& idr) const;
0054 
0055 private:
0056 };
0057 
0058 bool DTROSChannelCompare::operator()(const DTROSChannelId& idl, const DTROSChannelId& idr) const {
0059   if (idl.dduId() < idr.dduId())
0060     return true;
0061   if (idl.dduId() > idr.dduId())
0062     return false;
0063   if (idl.rosId() < idr.rosId())
0064     return true;
0065   if (idl.rosId() > idr.rosId())
0066     return false;
0067   if (idl.channelId() < idr.channelId())
0068     return true;
0069   if (idl.channelId() > idr.channelId())
0070     return false;
0071   return false;
0072 }
0073 
0074 class DTROBCardId {
0075 public:
0076   DTROBCardId(int wheel, int sector, int station, int rob);
0077   int wheelId() const;
0078   int sectorId() const;
0079   int stationId() const;
0080   int robId() const;
0081 
0082 private:
0083   int wheel_id;
0084   int sector_id;
0085   int station_id;
0086   int rob_id;
0087 };
0088 
0089 DTROBCardId::DTROBCardId(int wheel, int sector, int station, int rob)
0090     : wheel_id(wheel), sector_id(sector), station_id(station), rob_id(rob) {}
0091 
0092 int DTROBCardId::wheelId() const { return wheel_id; }
0093 
0094 int DTROBCardId::sectorId() const { return sector_id; }
0095 
0096 int DTROBCardId::stationId() const { return station_id; }
0097 
0098 int DTROBCardId::robId() const { return rob_id; }
0099 
0100 class DTROBCardCompare {
0101 public:
0102   bool operator()(const DTROBCardId& idl, const DTROBCardId& idr) const;
0103 
0104 private:
0105 };
0106 
0107 bool DTROBCardCompare::operator()(const DTROBCardId& idl, const DTROBCardId& idr) const {
0108   if (idl.wheelId() < idr.wheelId())
0109     return true;
0110   if (idl.wheelId() > idr.wheelId())
0111     return false;
0112   if (idl.sectorId() < idr.sectorId())
0113     return true;
0114   if (idl.sectorId() > idr.sectorId())
0115     return false;
0116   if (idl.stationId() < idr.stationId())
0117     return true;
0118   if (idl.stationId() > idr.stationId())
0119     return false;
0120   if (idl.robId() < idr.robId())
0121     return true;
0122   if (idl.robId() > idr.robId())
0123     return false;
0124   return false;
0125 }
0126 
0127 class DTTDCChannelId {
0128 public:
0129   DTTDCChannelId(int tdc, int channel);
0130   int tdcId() const;
0131   int channelId() const;
0132 
0133 private:
0134   int tdc_id;
0135   int channel_id;
0136 };
0137 
0138 DTTDCChannelId::DTTDCChannelId(int tdc, int channel) : tdc_id(tdc), channel_id(channel) {}
0139 
0140 int DTTDCChannelId::tdcId() const { return tdc_id; }
0141 
0142 int DTTDCChannelId::channelId() const { return channel_id; }
0143 
0144 class DTTDCChannelCompare {
0145 public:
0146   bool operator()(const DTTDCChannelId& idl, const DTTDCChannelId& idr) const;
0147 
0148 private:
0149 };
0150 
0151 bool DTTDCChannelCompare::operator()(const DTTDCChannelId& idl, const DTTDCChannelId& idr) const {
0152   if (idl.tdcId() < idr.tdcId())
0153     return true;
0154   if (idl.tdcId() > idr.tdcId())
0155     return false;
0156   if (idl.channelId() < idr.channelId())
0157     return true;
0158   if (idl.channelId() > idr.channelId())
0159     return false;
0160   return false;
0161 }
0162 
0163 class DTPhysicalWireId {
0164 public:
0165   DTPhysicalWireId(int superlayer, int layer, int cell);
0166   int superlayerId() const;
0167   int layerId() const;
0168   int cellId() const;
0169 
0170 private:
0171   int superlayer_id;
0172   int layer_id;
0173   int cell_id;
0174 };
0175 
0176 DTPhysicalWireId::DTPhysicalWireId(int superlayer, int layer, int cell)
0177     : superlayer_id(superlayer), layer_id(layer), cell_id(cell) {}
0178 
0179 int DTPhysicalWireId::superlayerId() const { return superlayer_id; }
0180 
0181 int DTPhysicalWireId::layerId() const { return layer_id; }
0182 
0183 int DTPhysicalWireId::cellId() const { return cell_id; }
0184 
0185 class DTPhysicalWireCompare {
0186 public:
0187   bool operator()(const DTPhysicalWireId& idl, const DTPhysicalWireId& idr) const;
0188 
0189 private:
0190 };
0191 
0192 bool DTPhysicalWireCompare::operator()(const DTPhysicalWireId& idl, const DTPhysicalWireId& idr) const {
0193   if (idl.superlayerId() < idr.superlayerId())
0194     return true;
0195   if (idl.superlayerId() > idr.superlayerId())
0196     return false;
0197   if (idl.layerId() < idr.layerId())
0198     return true;
0199   if (idl.layerId() > idr.layerId())
0200     return false;
0201   if (idl.cellId() < idr.cellId())
0202     return true;
0203   if (idl.cellId() > idr.cellId())
0204     return false;
0205   return false;
0206 }
0207 
0208 class DTMapElementIdentifiers {
0209 public:
0210   static int defaultIdValue;
0211   static int ROSMapIdOffset;
0212   static int ROBMapIdOffset;
0213   static int TDCMapIdOffset;
0214 };
0215 
0216 int DTMapElementIdentifiers::defaultIdValue = 0x7fffffff;
0217 int DTMapElementIdentifiers::ROSMapIdOffset = 2000001000;
0218 int DTMapElementIdentifiers::ROBMapIdOffset = 2000002000;
0219 int DTMapElementIdentifiers::TDCMapIdOffset = 2000003000;
0220 
0221 void DTCompactMapWriter::buildSteering(std::istream& jobDesc) {
0222   // template maps and lists, name and type
0223   std::string map_type("dummy_string");
0224   std::string map_file("dummy_string");
0225   std::string map_list("dummy_string");
0226 
0227   // ros-rob and tdc-cell maps
0228   std::map<DTROBCardId, int, DTROBCardCompare> tdc_idm;
0229   std::map<DTROSChannelId, DTROBCardId, DTROSChannelCompare> ros_map;
0230   std::map<DTROSChannelId, DTROBCardId, DTROSChannelCompare> rob_map;
0231 
0232   int rob_count = 1;
0233   int ros_count = 1;
0234 
0235   // parse job description file and loop over map templates and lists
0236   bool jobEnd = false;
0237   int max_line_length = 1000;
0238   char* job_line = new char[max_line_length];
0239   while (jobDesc.getline(job_line, max_line_length)) {
0240     // parse job description line
0241     char* ptr_line = job_line;
0242     char* end_line = job_line + max_line_length;
0243     while (ptr_line < end_line) {
0244       // get description command key
0245       // remove leading blanks
0246       while (*ptr_line == ' ')
0247         ptr_line++;
0248       std::string key(ptr_line);
0249       int off_blank = key.find(' ');
0250       int off_equal = key.find('=');
0251       if (off_equal < 0)
0252         off_equal = max_line_length;
0253       if (off_blank < 0)
0254         off_blank = key.length();
0255       int ioff = (off_blank < off_equal ? off_blank : off_equal);
0256       key.erase(ioff++, key.length());
0257       // go to command value
0258       ptr_line += ++off_equal;
0259       // exit loop at end of description file
0260       if ((jobEnd = (key == "end")))
0261         break;
0262       // get description command value
0263       while (*ptr_line == ' ')
0264         ptr_line++;
0265       std::string val(ptr_line);
0266       int off_value = val.find(' ');
0267       if ((off_value > 0) && (off_value < static_cast<int>(val.length())))
0268         val.erase(off_value++, val.length());
0269       // go to next token
0270       ptr_line += off_value;
0271       // set type, template or list file name
0272       if (key == "type")
0273         map_type = val;
0274       if (key == "map")
0275         map_file = val;
0276       if (key == "list")
0277         map_list = val;
0278     }
0279     if (jobEnd)
0280       break;
0281 
0282     // open map list file
0283     std::ifstream lfile(map_list.c_str());
0284 
0285     bool write;
0286     // store TDC maps
0287     if (map_type == "TDC") {
0288       // loop over TDC map list for concerned wheels and sectors
0289       int station = 0;
0290       int wheel;
0291       int sector;
0292       write = true;
0293       while (lfile >> wheel >> sector >> station)
0294         fillTDCMap(map_file, wheel, sector, tdc_idm, station, rob_count, write);
0295       ++rob_count;
0296     }
0297 
0298     // store ROS maps
0299     if (map_type == "ROS") {
0300       // loop over ROS map list for concerned ddu, ros, wheel and sector
0301       int ddu;
0302       int ros;
0303       int wheel;
0304       int sector;
0305       write = true;
0306       while (lfile >> ddu >> ros >> wheel >> sector)
0307         fillROSMap(map_file, ddu, ros, wheel, sector, ros_map, rob_map, ros_count, write);
0308       ++ros_count;
0309     }
0310   }
0311   delete[] job_line;
0312 
0313   // merge ROS and TDC maps
0314   fillReadOutMap(ros_count, tdc_idm, ros_map, rob_map);
0315 
0316   return;
0317 }
0318 
0319 void DTCompactMapWriter::fillTDCMap(const std::string& map_file,
0320                                     int wheel,
0321                                     int sector,
0322                                     std::map<DTROBCardId, int, DTROBCardCompare>& tdc_idm,
0323                                     int stationId,
0324                                     int map_count,
0325                                     bool& write) {
0326   // template map file name
0327   std::ifstream ifile(map_file.c_str());
0328 
0329   // get station number
0330   int station;
0331   int rob;
0332   station = stationId;
0333   ifile >> rob;
0334   DTROBCardId key(wheel, sector, station, rob);
0335   tdc_idm.insert(std::pair<DTROBCardId, int>(key, map_count));
0336   if (!write)
0337     return;
0338   // loop over TDC channels and cells
0339   int superlayer;
0340   int layer;
0341   int wire;
0342   int tdc;
0343   int channel;
0344   while (ifile >> tdc >> channel >> superlayer >> layer >> wire) {
0345     // set ROB card, TDC channel and cell identifiers
0346     DTTDCChannelId tdcChannel(tdc, channel);
0347     DTPhysicalWireId physicalWire(superlayer, layer, wire);
0348     // look for rob card connection map
0349     std::cout << DTMapElementIdentifiers::TDCMapIdOffset << " " << map_count << " " << rob << " " << tdc << " "
0350               << channel << " " << DTMapElementIdentifiers::defaultIdValue << " "
0351               << DTMapElementIdentifiers::defaultIdValue << " " << DTMapElementIdentifiers::defaultIdValue << " "
0352               << superlayer << " " << layer << " " << wire << std::endl;
0353   }
0354   write = false;
0355   return;
0356 }
0357 
0358 void DTCompactMapWriter::fillROSMap(const std::string& map_file,
0359                                     int ddu,
0360                                     int ros,
0361                                     int whdef,
0362                                     int secdef,
0363                                     std::map<DTROSChannelId, DTROBCardId, DTROSChannelCompare>& ddu_map,
0364                                     std::map<DTROSChannelId, DTROBCardId, DTROSChannelCompare>& ros_map,
0365                                     int map_count,
0366                                     bool& write) {
0367   DTROSChannelId rosBoard(ddu, ros, 0);
0368   DTROBCardId sectorId(whdef, secdef, 0, map_count);
0369   ddu_map.insert(std::pair<DTROSChannelId, DTROBCardId>(rosBoard, sectorId));
0370 
0371   // template map file name
0372   std::ifstream ifile(map_file.c_str());
0373 
0374   // loop over ROS channels and ROBs
0375   int channel;
0376   int wheel;
0377   int sector;
0378   int station;
0379   int rob;
0380   DTROSChannelId rosChanMap(ddu, ros, 0);
0381   if (!write)
0382     return;
0383   while (ifile >> channel >> wheel >> sector >> station >> rob) {
0384     // set sector to default unless specified
0385     if (!sector)
0386       sector = DTMapElementIdentifiers::defaultIdValue;
0387     if (!wheel)
0388       wheel = DTMapElementIdentifiers::defaultIdValue;
0389     // set ROS channel and ROB identifiers
0390     DTROSChannelId rosChannel(ddu, ros, channel);
0391     DTROBCardId robCard(wheel, sector, station, rob);
0392     // store ROS channel to ROB connection into map
0393     ros_map.insert(std::pair<DTROSChannelId, DTROBCardId>(rosChannel, robCard));
0394   }
0395   write = false;
0396 
0397   return;
0398 }
0399 
0400 void DTCompactMapWriter::cloneROS(std::map<DTROSChannelId, DTROBCardId, DTROSChannelCompare>& ros_m,
0401                                   std::map<DTROSChannelId, DTROBCardId, DTROSChannelCompare>& ros_a,
0402                                   int ddu_o,
0403                                   int ros_o,
0404                                   int ddu_f,
0405                                   int ros_f) {
0406   std::map<DTROSChannelId, DTROBCardId, DTROSChannelCompare>::const_iterator ros_iter = ros_m.begin();
0407   std::map<DTROSChannelId, DTROBCardId, DTROSChannelCompare>::const_iterator ros_iend = ros_m.end();
0408   while (ros_iter != ros_iend) {
0409     const std::pair<DTROSChannelId, DTROBCardId>& rosLink = *ros_iter++;
0410     const DTROSChannelId& rosChannelId = rosLink.first;
0411     const DTROBCardId& robCardId = rosLink.second;
0412     if (rosChannelId.dduId() != ddu_o)
0413       continue;
0414     if (rosChannelId.rosId() != ros_o)
0415       continue;
0416     const DTROSChannelId rosChanNewId(ddu_f, ros_f, rosChannelId.channelId());
0417     ros_a.insert(std::pair<DTROSChannelId, DTROBCardId>(rosChanNewId, robCardId));
0418   }
0419   return;
0420 }
0421 
0422 void DTCompactMapWriter::appendROS(std::map<DTROSChannelId, DTROBCardId, DTROSChannelCompare>& ros_m,
0423                                    std::map<DTROSChannelId, DTROBCardId, DTROSChannelCompare>& ros_a) {
0424   std::map<DTROSChannelId, DTROBCardId, DTROSChannelCompare>::const_iterator ros_iter = ros_a.begin();
0425   std::map<DTROSChannelId, DTROBCardId, DTROSChannelCompare>::const_iterator ros_iend = ros_a.end();
0426   while (ros_iter != ros_iend)
0427     ros_m.insert(*ros_iter++);
0428   return;
0429 }
0430 
0431 void DTCompactMapWriter::fillReadOutMap(int ros_count,
0432                                         std::map<DTROBCardId, int, DTROBCardCompare>& tdc_idm,
0433                                         std::map<DTROSChannelId, DTROBCardId, DTROSChannelCompare>& ddu_map,
0434                                         std::map<DTROSChannelId, DTROBCardId, DTROSChannelCompare>& ros_map) {
0435   bool check = true;
0436   bool write = false;
0437 
0438   while (check || write) {
0439     // ROS board to sector connection map
0440     std::map<DTROSChannelId, DTROBCardId, DTROSChannelCompare>::const_iterator ddu_iter = ddu_map.begin();
0441     std::map<DTROSChannelId, DTROBCardId, DTROSChannelCompare>::const_iterator ddu_iend = ddu_map.end();
0442     // ROB card to TDC element map
0443     std::map<DTROBCardId, int, DTROBCardCompare>::const_iterator idt_iend = tdc_idm.end();
0444     // ROS channel to ROB connection map
0445     std::map<DTROSChannelId, DTROBCardId, DTROSChannelCompare>::const_iterator ros_iter = ros_map.begin();
0446     std::map<DTROSChannelId, DTROBCardId, DTROSChannelCompare>::const_iterator ros_iend = ros_map.end();
0447 
0448     check = false;
0449     std::map<DTROSChannelId, DTROBCardId, DTROSChannelCompare> ros_app;
0450 
0451     // loop over ROS channels
0452     while (ros_iter != ros_iend) {
0453       // get ROS channel and ROB identifiers
0454       const std::pair<DTROSChannelId, DTROBCardId>& rosLink = *ros_iter++;
0455       const DTROSChannelId& rosChannelId = rosLink.first;
0456       const DTROSChannelId rosChanMapId(rosChannelId.dduId(), rosChannelId.rosId(), 0);
0457       const DTROBCardId& robCMapId = rosLink.second;
0458       ddu_iter = ddu_map.find(rosChanMapId);
0459       int whdef = 0;
0460       int secdef = 0;
0461       int rosMapCk = 0;
0462       if (ddu_iter != ddu_iend) {
0463         const DTROBCardId& sector = ddu_iter->second;
0464         whdef = sector.wheelId();
0465         secdef = sector.sectorId();
0466         rosMapCk = sector.robId();
0467       } else {
0468         std::cout << "DDU map " << rosChannelId.dduId() << " " << rosChannelId.rosId() << " 0 not found" << std::endl;
0469         continue;
0470       }
0471       int wheel = robCMapId.wheelId();
0472       int sector = robCMapId.sectorId();
0473       int station = robCMapId.stationId();
0474       int rob = robCMapId.robId();
0475       if (wheel != DTMapElementIdentifiers::defaultIdValue)
0476         whdef = wheel;
0477       if (sector != DTMapElementIdentifiers::defaultIdValue)
0478         secdef = sector;
0479       const DTROBCardId robCardId(whdef, secdef, station, rob);
0480       std::map<DTROBCardId, int, DTROBCardCompare>::const_iterator idt_iter = tdc_idm.find(robCardId);
0481       if (idt_iter != idt_iend) {
0482         int tdcMapId = idt_iter->second;
0483         ddu_iter = ddu_map.begin();
0484         while (ddu_iter != ddu_iend) {
0485           const std::pair<DTROSChannelId, DTROBCardId>& dduLink = *ddu_iter++;
0486           const DTROSChannelId& ros_id = dduLink.first;
0487           const DTROBCardId& sec_id = dduLink.second;
0488           int whe_chk = sec_id.wheelId();
0489           int sec_chk = sec_id.sectorId();
0490           if (wheel != DTMapElementIdentifiers::defaultIdValue)
0491             whe_chk = wheel;
0492           if (sector != DTMapElementIdentifiers::defaultIdValue)
0493             sec_chk = sector;
0494           if (rosMapCk != sec_id.robId())
0495             continue;
0496           DTROBCardId robCardId(whe_chk, sec_chk, station, rob);
0497           idt_iter = tdc_idm.find(robCardId);
0498           int tdcMapCk = idt_iter->second;
0499           if ((tdcMapId != tdcMapCk) && (!check)) {
0500             cloneROS(ros_map, ros_app, rosChannelId.dduId(), rosChannelId.rosId(), ros_id.dduId(), ros_id.rosId());
0501             tdcMapId = tdcMapCk;
0502             check = true;
0503           }
0504           if ((tdcMapId == tdcMapCk) && check) {
0505             std::map<DTROSChannelId, DTROBCardId, DTROSChannelCompare>::iterator ddu_inew = ddu_map.find(ros_id);
0506             ddu_inew->second = DTROBCardId(sec_id.wheelId(), sec_id.sectorId(), sec_id.stationId(), ros_count);
0507           }
0508         }
0509         if (check) {
0510           ros_count++;
0511           break;
0512         }
0513         if (write)
0514           std::cout << DTMapElementIdentifiers::ROSMapIdOffset << " " << rosMapCk << " " << rosChannelId.channelId()
0515                     << " " << DTMapElementIdentifiers::defaultIdValue << " " << DTMapElementIdentifiers::defaultIdValue
0516                     << " " << wheel << " " << station << " " << sector << " " << rob << " "
0517                     << DTMapElementIdentifiers::TDCMapIdOffset << " " << tdcMapId << std::endl;
0518 
0519       } else {
0520         std::cout << "TDC map " << wheel << " " << sector << " " << station << " " << rob << " not found" << std::endl;
0521       }
0522     }
0523     appendROS(ros_map, ros_app);
0524     if (!write) {
0525       write = !check;
0526     } else {
0527       ddu_iter = ddu_map.begin();
0528       while (ddu_iter != ddu_iend) {
0529         const std::pair<DTROSChannelId, DTROBCardId>& dduLink = *ddu_iter++;
0530         const DTROSChannelId& ros_id = dduLink.first;
0531         const DTROBCardId& sec_id = dduLink.second;
0532         std::cout << ros_id.dduId() << " " << ros_id.rosId() << " " << DTMapElementIdentifiers::defaultIdValue << " "
0533                   << DTMapElementIdentifiers::defaultIdValue << " " << DTMapElementIdentifiers::defaultIdValue << " "
0534                   << sec_id.wheelId() << " " << DTMapElementIdentifiers::defaultIdValue << " " << sec_id.sectorId()
0535                   << " " << DTMapElementIdentifiers::defaultIdValue << " " << DTMapElementIdentifiers::ROSMapIdOffset
0536                   << " " << sec_id.robId() << std::endl;
0537       }
0538       write = false;
0539     }
0540   }
0541 
0542   return;
0543 }