CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/src/CalibFormats/HcalObjects/src/HcalText2DetIdConverter.cc

Go to the documentation of this file.
00001 
00005 #include <stdlib.h>
00006 #include <iostream>
00007 #include <iomanip>
00008 #include <cstdio>
00009 
00010 #include "FWCore/Utilities/interface/Exception.h"
00011 
00012 #include "DataFormats/HcalDetId/interface/HcalGenericDetId.h"
00013 #include "DataFormats/HcalDetId/interface/HcalDetId.h"
00014 #include "DataFormats/HcalDetId/interface/HcalCalibDetId.h"
00015 #include "DataFormats/HcalDetId/interface/HcalTrigTowerDetId.h"
00016 #include "DataFormats/HcalDetId/interface/HcalZDCDetId.h"
00017 
00018 #include "CalibFormats/HcalObjects/interface/HcalText2DetIdConverter.h"
00019 
00020 namespace {
00021   std::string strip (const std::string& fString) {
00022     if (fString.empty ()) return fString;
00023     int startIndex = fString.find_first_not_of(" \t\n");
00024     int endIndex = fString.find_last_not_of(" \t\n");
00025     return fString.substr(startIndex, (endIndex-startIndex)+1);
00026   }
00027 
00028   int calibChannel (const std::string& fName) {
00029     return 
00030       fName == "Mixer-High" ? 1 :
00031       fName == "Mixer-Low" ? 2 :
00032       fName == "Megatile" ? 3 :
00033       fName == "Mixer-Scintillator" ? 4 :
00034       fName == "RadDam1" ? 5 :
00035       fName == "RadDam2" ? 6 :
00036       fName == "RadDam3" ? 7 :
00037       atoi(fName.c_str());
00038     //      0;
00039   }
00040 }
00041 
00042 HcalText2DetIdConverter::HcalText2DetIdConverter (const std::string& fFlavor, const std::string& fField1,
00043                                                   const std::string& fField2, const std::string& fField3) {
00044   if (!init (fFlavor, fField1, fField2, fField3)) {
00045     std::cerr << "HcalText2DetIdConverter::HcalText2DetIdConverter-> Can not initiate detId from items: "
00046               << fFlavor << '/' << fField1 << '/' << fField2 << '/' << fField3 << std::endl;
00047     throw cms::Exception("HcalGenDetId initialization error") 
00048       << " Can not initiate detId from items: "
00049       << fFlavor << '/' << fField1 << '/' << fField2 << '/' << fField3 << std::endl;
00050   }
00051 }
00052 
00053 HcalText2DetIdConverter::HcalText2DetIdConverter (DetId fId) {
00054   init (fId);
00055 }
00056 
00057 bool HcalText2DetIdConverter::isHcalDetId () const {
00058   return HcalGenericDetId (mId).isHcalDetId ();
00059 }
00060 
00061 bool HcalText2DetIdConverter::isHcalCalibDetId () const {
00062   return HcalGenericDetId (mId).isHcalCalibDetId ();
00063 }
00064 
00065 bool HcalText2DetIdConverter::isHcalTrigTowerDetId () const {
00066   return HcalGenericDetId (mId).isHcalTrigTowerDetId ();
00067 }
00068 
00069 bool HcalText2DetIdConverter::isHcalZDCDetId () const {
00070   return HcalGenericDetId (mId).isHcalZDCDetId ();
00071 }
00072 
00073 bool HcalText2DetIdConverter::init (DetId fId) {
00074   bool result = true;
00075   flavorName = "UNKNOWN";
00076   mId = fId;
00077   HcalGenericDetId genId (mId);
00078   if (fId == HcalDetId::Undefined) {
00079     flavorName = "NA";
00080   }
00081   else if (genId.isHcalDetId ()) {
00082     HcalDetId hcalId (mId);
00083     flavorName = genId.genericSubdet () == HcalGenericDetId::HcalGenBarrel ? "HB" :
00084       genId.genericSubdet () == HcalGenericDetId::HcalGenEndcap ? "HE" :
00085       genId.genericSubdet () == HcalGenericDetId::HcalGenOuter ? "HO" :
00086       genId.genericSubdet () == HcalGenericDetId::HcalGenForward ? "HF" : "H_UNKNOWN";
00087     setField (1, hcalId.ieta());
00088     setField (2, hcalId.iphi());
00089     setField (3, hcalId.depth());
00090   }
00091   else if (genId.isHcalTrigTowerDetId ()) {
00092     HcalTrigTowerDetId triggerId (mId);
00093     if (triggerId == HcalTrigTowerDetId::Undefined) {
00094       flavorName = "NT";
00095     }
00096     else {
00097       flavorName = "HT";
00098       setField (1, triggerId.ieta());
00099       setField (2, triggerId.iphi());
00100       setField (3, 1);
00101     }
00102   }
00103   else if (genId.isHcalZDCDetId ()) {
00104     HcalZDCDetId zdcId (mId);
00105     switch (zdcId.section()) {
00106     case HcalZDCDetId::EM: flavorName = "ZDC_EM"; break;
00107     case HcalZDCDetId::HAD: flavorName = "ZDC_HAD"; break;
00108     case HcalZDCDetId::LUM: flavorName = "ZDC_LUM"; break;
00109     default: result = false;
00110     }
00111     setField (1, zdcId.zside());
00112     setField (2, zdcId.channel());
00113     setField (3, -99);
00114   }
00115   else if (genId.isHcalCalibDetId ()) {
00116     HcalCalibDetId calibId (mId);
00117     if (calibId.calibFlavor()==HcalCalibDetId::CalibrationBox) {
00118       switch (calibId.hcalSubdet()) {
00119       case HcalBarrel:  flavorName = "CALIB_HB"; break;
00120       case HcalEndcap:  flavorName = "CALIB_HE"; break;
00121       case HcalOuter:  flavorName = "CALIB_HO"; break;
00122       case HcalForward:  flavorName = "CALIB_HF"; break;
00123       default: result = false;
00124       }
00125       setField (1, calibId.ieta());
00126       setField (2, calibId.iphi());
00127       setField (3, calibId.cboxChannel() );
00128     } else if (calibId.calibFlavor()==HcalCalibDetId::HOCrosstalk) {
00129       flavorName="HOX";
00130       setField (1, calibId.ieta());
00131       setField (2, calibId.iphi());
00132       setField (3, -999);
00133     }
00134   }
00135   else {
00136     flavorName = "UNKNOWN_FLAVOR";
00137     std::cerr << "HcalText2DetIdConverter::init-> Unknown detId: " << std::hex << std::showbase << mId.rawId() << std::endl;
00138     result = false;
00139   }
00140   return result;
00141 }
00142 
00143 
00144 bool HcalText2DetIdConverter::init (const std::string& fFlavor, const std::string& fField1,
00145                                    const std::string& fField2, const std::string& fField3) {
00146   bool result = true;
00147   flavorName = strip (fFlavor);
00148   field1 = strip (fField1);
00149   field2 = strip (fField2);
00150   field3 = strip (fField3);
00151   if (flavorName == "HB" ||
00152       flavorName == "HE" ||
00153       flavorName == "HF" ||
00154       flavorName == "HO") {
00155     HcalSubdetector sub = flavorName == "HB" ? HcalBarrel :
00156       flavorName == "HE" ? HcalEndcap :
00157       flavorName == "HO" ? HcalOuter :
00158       HcalForward;
00159     mId = HcalDetId (sub, getField (1), getField (2), getField (3));
00160   }
00161   else if (flavorName == "HT") {
00162     mId = HcalTrigTowerDetId (getField (1), getField (2));
00163   }
00164   else if (flavorName.find ("ZDC_") == 0) {
00165     HcalZDCDetId::Section section = flavorName == "ZDC_EM" ? HcalZDCDetId::EM :
00166       flavorName == "ZDC_HAD" ? HcalZDCDetId::HAD : 
00167       flavorName == "ZDC_LUM" ? HcalZDCDetId::LUM : HcalZDCDetId::Unknown;
00168     mId = HcalZDCDetId (section, getField (1)>0, getField (2));
00169   }
00170   else if (flavorName.find ("CALIB_") == 0) {
00171     HcalSubdetector sd = HcalOther;
00172     if (flavorName.find("HB")!=std::string::npos) sd=HcalBarrel;
00173     else if (flavorName.find("HE")!=std::string::npos) sd=HcalEndcap;
00174     else if (flavorName.find("HO")!=std::string::npos) sd=HcalOuter;
00175     else if (flavorName.find("HF")!=std::string::npos) sd=HcalForward;
00176     
00177     int ieta=getField(1);
00178     int iphi=getField(2);
00179     int channel = calibChannel (field3);
00180     mId = HcalCalibDetId (sd, ieta,iphi,channel);
00181   }
00182   else if (flavorName=="HOX") {
00183     int ieta=getField(1);
00184     int iphi=getField(2);
00185     mId = HcalCalibDetId (ieta,iphi);
00186   }
00187   else if (flavorName == "NA") {
00188     mId = HcalDetId::Undefined;
00189   } 
00190   else {
00191     std::cerr << "HcalText2DetIdConverter::init-> Unknown HcalDetId flavor: " << flavorName << std::endl;
00192     result = false;
00193   }
00194   return result;
00195 }
00196 
00197 
00198 int HcalText2DetIdConverter::getField (int i) const{
00199   char* endptr;
00200   const char* nptr = i == 1 ? field1.c_str() :
00201     i == 2 ? field2.c_str() : field3.c_str();
00202   long result = strtol (nptr, &endptr, 0);
00203     if (*nptr != '\0' && *endptr == '\0') {
00204       return result;
00205     }
00206     if (i == 2 && isHcalCalibDetId ()) {
00207       int result = calibChannel (field2);
00208       if (i) return result;
00209     }
00210     if (*nptr != '\0') {
00211       std::cerr << "HcalText2DetIdConverter::getField-> Can not convert string "<< nptr << " to int. Bad symbol: " << *endptr << std::endl;
00212     }
00213     return 0;
00214   }
00215 
00216 void HcalText2DetIdConverter::setField (int i, int fValue) {
00217   char buffer [128];
00218   sprintf (buffer, "%d", fValue);
00219   if (i == 1) field1 = buffer;
00220   else if (i == 2) field2 = buffer;
00221   else  field3 = buffer;
00222 }
00223 
00224 std::string HcalText2DetIdConverter::toString () const {
00225   return flavorName + " " + field1 + " " + field2 + " " + field3;
00226 }