CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_14/src/CalibFormats/CastorObjects/src/CastorText2DetIdConverter.cc

Go to the documentation of this file.
00001 
00003 #include <stdlib.h>
00004 #include <iostream>
00005 #include <iomanip>
00006 #include <cstdio>
00007 
00008 #include "FWCore/Utilities/interface/Exception.h"
00009 
00010 #include "DataFormats/HcalDetId/interface/HcalGenericDetId.h"
00011 #include "DataFormats/HcalDetId/interface/HcalDetId.h"
00012 #include "DataFormats/HcalDetId/interface/HcalCastorDetId.h"
00013 
00014 #include "CalibFormats/CastorObjects/interface/CastorText2DetIdConverter.h"
00015 
00016 namespace {
00017   std::string strip (const std::string& fString) {
00018     if (fString.empty ()) return fString;
00019     int startIndex = fString.find_first_not_of(" \t\n");
00020     int endIndex = fString.find_last_not_of(" \t\n");
00021     return fString.substr(startIndex, (endIndex-startIndex)+1);
00022   }
00023 }
00024 
00025 CastorText2DetIdConverter::CastorText2DetIdConverter (const std::string& fFlavor, const std::string& fField1,
00026                                                   const std::string& fField2, const std::string& fField3) {
00027   if (!init (fFlavor, fField1, fField2, fField3)) {
00028     std::cerr << "CastorText2DetIdConverter::CastorText2DetIdConverter-> Can not initiate detId from items: "
00029               << fFlavor << '/' << fField1 << '/' << fField2 << '/' << fField3 << std::endl;
00030     throw cms::Exception("HcalGenDetId initialization error") 
00031       << " Can not initiate detId from items: "
00032       << fFlavor << '/' << fField1 << '/' << fField2 << '/' << fField3 << std::endl;
00033   }
00034 }
00035 
00036 CastorText2DetIdConverter::CastorText2DetIdConverter (DetId fId) {
00037   init (fId);
00038 }
00039 
00040 bool CastorText2DetIdConverter::isHcalCastorDetId () const {
00041   return HcalGenericDetId (mId).isHcalCastorDetId ();
00042 }
00043 
00044 bool CastorText2DetIdConverter::init (DetId fId) {
00045   bool result = true;
00046   flavorName = "UNKNOWN";
00047   mId = fId;
00048   HcalGenericDetId genId (mId);
00049   if (fId == HcalDetId::Undefined) {
00050     flavorName = "NA";
00051   }
00052 
00053   else if (genId.isHcalCastorDetId ()) {
00054     HcalCastorDetId castorId (mId);
00055     switch (castorId.section()) {
00056     case HcalCastorDetId::EM: flavorName = "CASTOR_EM"; break;
00057     case HcalCastorDetId::HAD: flavorName = "CASTOR_HAD"; break;
00058     default: result = false;
00059     }
00060     setField (1, castorId.zside());
00061     setField (2, castorId.sector());
00062     setField (3, castorId.module());
00063   }
00064 
00065   else {
00066     flavorName = "UNKNOWN_FLAVOR";
00067     std::cerr << "CastorText2DetIdConverter::init-> Unknown detId: " << std::hex << std::showbase << mId.rawId() << std::endl;
00068     result = false;
00069   }
00070   return result;
00071 }
00072 
00073 
00074 bool CastorText2DetIdConverter::init (const std::string& fFlavor, const std::string& fField1,
00075                                    const std::string& fField2, const std::string& fField3) {
00076   bool result = true;
00077   flavorName = strip (fFlavor);
00078   field1 = strip (fField1);
00079   field2 = strip (fField2);
00080   field3 = strip (fField3);
00081   if (flavorName.find ("CASTOR_") == 0) {
00082     HcalCastorDetId::Section section = flavorName == "CASTOR_EM" ? HcalCastorDetId::EM :
00083       flavorName == "CASTOR_HAD" ? HcalCastorDetId::HAD : HcalCastorDetId::Unknown;
00084     mId = HcalCastorDetId (section, getField (1)>0, getField (2), getField(3));
00085   }
00086 
00087   else {
00088     std::cerr << "CastorText2DetIdConverter::init-> Unknown DetId flavor: " << flavorName << std::endl;
00089     result = false;
00090   }
00091   return result;
00092 }
00093 
00094 
00095 int CastorText2DetIdConverter::getField (int i) const{
00096   char* endptr;
00097   const char* nptr = i == 1 ? field1.c_str() :
00098     i == 2 ? field2.c_str() : field3.c_str();
00099   long result = strtol (nptr, &endptr, 0);
00100     if (*nptr != '\0' && *endptr == '\0') {
00101       return result;
00102     }
00103     if (*nptr != '\0') {
00104       std::cerr << "CastorText2DetIdConverter::getField-> Can not convert string "<< nptr << " to int. Bad symbol: " << *endptr << std::endl;
00105     }
00106     return 0;
00107 }
00108 
00109 void CastorText2DetIdConverter::setField (int i, int fValue) {
00110   char buffer [128];
00111   sprintf (buffer, "%d", fValue);
00112   if (i == 1) field1 = buffer;
00113   else if (i == 2) field2 = buffer;
00114   else  field3 = buffer;
00115 }
00116 
00117 std::string CastorText2DetIdConverter::toString () const {
00118   return flavorName + " " + field1 + " " + field2 + " " + field3;
00119 }