CMS 3D CMS Logo

CastorText2DetIdConverter.cc

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

Generated on Tue Jun 9 17:25:22 2009 for CMSSW by  doxygen 1.5.4