CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_10_patch1/src/CalibCalorimetry/CastorCalib/src/CastorDbASCIIIO.cc

Go to the documentation of this file.
00001 //
00002 // F.Ratnikov (UMd), Oct 28, 2005
00003 // $Id: CastorDbASCIIIO.cc,v 1.8 2012/11/14 13:28:53 mundim Exp $
00004 //
00005 #include <vector>
00006 #include <string>
00007 #include <cstdio>
00008 
00009 #include "DataFormats/HcalDetId/interface/HcalGenericDetId.h"
00010 #include "DataFormats/HcalDetId/interface/CastorElectronicsId.h"
00011 #include "CalibFormats/CastorObjects/interface/CastorText2DetIdConverter.h"
00012 
00013 #include "CondFormats/CastorObjects/interface/AllObjects.h"
00014 #include "CalibCalorimetry/CastorCalib/interface/CastorDbASCIIIO.h"
00015 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00016 
00017 namespace CastorDbASCIIIO {
00018   class DetIdLess {
00019   public:
00020     bool operator () (DetId fFirst, DetId fSecond) const {
00021       HcalGenericDetId first (fFirst);
00022       HcalGenericDetId second (fSecond);
00023       if (first.genericSubdet () != second.genericSubdet ()) return first.genericSubdet () < second.genericSubdet ();
00024       if (first.isHcalDetId ()) {
00025         HcalDetId f1 (first);
00026         HcalDetId s1 (second);
00027         return  f1.zside () != s1.zside () ? f1.zside () < s1.zside () :
00028           f1.iphi () != s1.iphi () ? f1.iphi () < s1.iphi () :
00029           f1.ietaAbs () != s1.ietaAbs () ? f1.ietaAbs () < s1.ietaAbs () :
00030           f1.depth () < s1.depth ();
00031       }
00032       else {
00033         return first.rawId() < second.rawId();
00034       }
00035     }
00036   };
00037   class CastorElectronicsIdLess {
00038   public:
00039     bool operator () (CastorElectronicsId first, CastorElectronicsId second) const {
00040       return
00041         first.readoutVMECrateId () != second.readoutVMECrateId () ? first.readoutVMECrateId () < second.readoutVMECrateId () :
00042         first.htrSlot () != second.htrSlot () ? first.htrSlot () < second.htrSlot () :
00043         first.htrTopBottom () != second.htrTopBottom () ? first.htrTopBottom () < second.htrTopBottom () :
00044         first.fiberIndex () != second.fiberIndex () ? first.fiberIndex () < second.fiberIndex () :
00045         first.fiberChanId () < second.fiberChanId ();
00046     }
00047   };
00048 
00049 std::vector <std::string> splitString (const std::string& fLine) {
00050   std::vector <std::string> result;
00051   int start = 0;
00052   bool empty = true;
00053   for (unsigned i = 0; i <= fLine.size (); i++) {
00054     if (fLine [i] == ' ' || i == fLine.size ()) {
00055       if (!empty) {
00056         std::string item (fLine, start, i-start);
00057         result.push_back (item);
00058         empty = true;
00059       }
00060       start = i+1;
00061     }
00062     else {
00063       if (empty) empty = false;
00064     }
00065   }
00066   return result;
00067 }
00068 
00069 DetId getId (const std::vector <std::string> & items) {
00070   CastorText2DetIdConverter converter (items [3], items [0], items [1], items [2]);
00071   return converter.getId ();
00072 }
00073 
00074 void dumpId (std::ostream& fOutput, DetId id) {
00075   CastorText2DetIdConverter converter (id);
00076   char buffer [1024];
00077   sprintf (buffer, "  %15s %15s %15s %15s",
00078            converter.getField1 ().c_str (), converter.getField2 ().c_str (), converter.getField3 ().c_str (),converter.getFlavor ().c_str ());  
00079   fOutput << buffer;
00080 }
00081 
00082 template <class T,class S> 
00083 bool getCastorObject (std::istream& fInput, T* fObject, S* fCondObject) {
00084   if (!fObject) fObject = new T;
00085   char buffer [1024];
00086   while (fInput.getline(buffer, 1024)) {
00087     if (buffer [0] == '#') continue; //ignore comment
00088     std::vector <std::string> items = splitString (std::string (buffer));
00089     if (items.size()==0) continue; // blank line
00090     if (items.size () < 8) {
00091       edm::LogWarning("Format Error") << "Bad line: " << buffer << "\n line must contain 8 items: eta, phi, depth, subdet, 4x values" << std::endl;
00092       continue;
00093     }
00094     DetId id = getId (items);
00095     
00096 //    if (fObject->exists(id) )
00097 //      edm::LogWarning("Redefining Channel") << "line: " << buffer << "\n attempts to redefine data. Ignored" << std::endl;
00098 //    else
00099 //      {
00100         fCondObject = new S(id, atof (items [4].c_str()), atof (items [5].c_str()), 
00101                            atof (items [6].c_str()), atof (items [7].c_str()));
00102         fObject->addValues(*fCondObject);
00103         delete fCondObject;
00104         //      }
00105   }
00106 
00107   return true;
00108 }
00109 
00110 template <class T>
00111 bool dumpCastorObject (std::ostream& fOutput, const T& fObject) {
00112   char buffer [1024];
00113   sprintf (buffer, "# %15s %15s %15s %15s %8s %8s %8s %8s %10s\n", "eta", "phi", "dep", "det", "cap0", "cap1", "cap2", "cap3", "DetId");
00114   fOutput << buffer;
00115   std::vector<DetId> channels = fObject.getAllChannels ();
00116   //std::sort (channels.begin(), channels.end(), DetIdLess ());
00117   for (std::vector<DetId>::iterator channel = channels.begin ();
00118        channel !=  channels.end ();
00119        channel++) {
00120     const float* values = fObject.getValues (*channel)->getValues ();
00121     if (values) {
00122       dumpId (fOutput, *channel);
00123       sprintf (buffer, " %8.5f %8.5f %8.5f %8.5f %10X\n",
00124                values[0], values[1], values[2], values[3], channel->rawId ());
00125       fOutput << buffer;
00126     }
00127   }
00128   return true;
00129 }
00130 
00131 template <class T,class S> 
00132 bool getCastorSingleFloatObject (std::istream& fInput, T* fObject, S* fCondObject) {
00133   if (!fObject) fObject = new T;
00134   char buffer [1024];
00135   while (fInput.getline(buffer, 1024)) {
00136     if (buffer [0] == '#') continue; //ignore comment
00137     std::vector <std::string> items = splitString (std::string (buffer));
00138     if (items.size()==0) continue; // blank line
00139     if (items.size () < 5) {
00140       edm::LogWarning("Format Error") << "Bad line: " << buffer << "\n line must contain 5 items: eta, phi, depth, subdet, value" << std::endl;
00141       continue;
00142     }
00143     DetId id = getId (items);
00144     
00145 //    if (fObject->exists(id) )
00146 //      edm::LogWarning("Redefining Channel") << "line: " << buffer << "\n attempts to redefine data. Ignored" << std::endl;
00147 //    else
00148 //      {
00149         fCondObject = new S(id, atof (items [4].c_str()) );
00150         fObject->addValues(*fCondObject);
00151         delete fCondObject;
00152         //      }
00153   }
00154   return true;
00155 }
00156 
00157 template <class T>
00158 bool dumpCastorSingleFloatObject (std::ostream& fOutput, const T& fObject) {
00159   char buffer [1024];
00160   sprintf (buffer, "# %15s %15s %15s %15s %8s %10s\n", "eta", "phi", "dep", "det", "value", "DetId");
00161   fOutput << buffer;
00162   std::vector<DetId> channels = fObject.getAllChannels ();
00163   std::sort (channels.begin(), channels.end(), DetIdLess ());
00164   for (std::vector<DetId>::iterator channel = channels.begin ();
00165        channel !=  channels.end ();
00166        channel++) {
00167     const float value = fObject.getValues (*channel)->getValue ();
00168     dumpId (fOutput, *channel);
00169     sprintf (buffer, " %8.5f %10X\n",
00170              value, channel->rawId ());
00171     fOutput << buffer;
00172   }
00173   return true;
00174 }
00175 
00176 template <class T,class S> 
00177 bool getCastorSingleIntObject (std::istream& fInput, T* fObject, S* fCondObject) {
00178   if (!fObject) fObject = new T;
00179   char buffer [1024];
00180   while (fInput.getline(buffer, 1024)) {
00181     if (buffer [0] == '#') continue; //ignore comment
00182     std::vector <std::string> items = splitString (std::string (buffer));
00183     if (items.size()==0) continue; // blank line
00184     if (items.size () < 5) {
00185       edm::LogWarning("Format Error") << "Bad line: " << buffer << "\n line must contain 5 items: eta, phi, depth, subdet, value" << std::endl;
00186       continue;
00187     }
00188     DetId id = getId (items);
00189     
00190 //    if (fObject->exists(id) )
00191 //      edm::LogWarning("Redefining Channel") << "line: " << buffer << "\n attempts to redefine data. Ignored" << std::endl;
00192 //    else
00193 //      {
00194         fCondObject = new S(id, atoi (items [4].c_str()) );
00195         fObject->addValues(*fCondObject);
00196         delete fCondObject;
00197         //      }
00198   }
00199   return true;
00200 }
00201 
00202 template <class T>
00203 bool dumpCastorSingleIntObject (std::ostream& fOutput, const T& fObject) {
00204   char buffer [1024];
00205   sprintf (buffer, "# %15s %15s %15s %15s %8s %10s\n", "eta", "phi", "dep", "det", "value", "DetId");
00206   fOutput << buffer;
00207   std::vector<DetId> channels = fObject.getAllChannels ();
00208   std::sort (channels.begin(), channels.end(), DetIdLess ());
00209   for (std::vector<DetId>::iterator channel = channels.begin ();
00210        channel !=  channels.end ();
00211        channel++) {
00212     const int value = fObject.getValues (*channel)->getValue ();
00213     dumpId (fOutput, *channel);
00214     sprintf (buffer, " %15d %10X\n",
00215              value, channel->rawId ());
00216     fOutput << buffer;
00217   }
00218   return true;
00219 }
00220 
00221 
00222 bool getObject (std::istream& fInput, CastorGains* fObject) {return getCastorObject (fInput, fObject, new CastorGain);}
00223 bool dumpObject (std::ostream& fOutput, const CastorGains& fObject) {return dumpCastorObject (fOutput, fObject);}
00224 bool getObject (std::istream& fInput, CastorGainWidths* fObject) {return getCastorObject (fInput, fObject, new CastorGainWidth);}
00225 bool dumpObject (std::ostream& fOutput, const CastorGainWidths& fObject) {return dumpCastorObject (fOutput, fObject);}
00226 
00227 bool getObject (std::istream& fInput, CastorSaturationCorrs* fObject) {return getCastorSingleFloatObject (fInput, fObject, new CastorSaturationCorr);}
00228 bool dumpObject (std::ostream& fOutput, const CastorSaturationCorrs& fObject) {return dumpCastorSingleFloatObject (fOutput, fObject);}
00229 
00230 
00231 
00232 // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
00233 bool getObject (std::istream& fInput, CastorPedestals* fObject) {
00234   if (!fObject) fObject = new CastorPedestals(false);
00235   char buffer [1024];
00236 
00237   while (fInput.getline(buffer, 1024)) {
00238     std::vector <std::string> items = splitString (std::string (buffer));
00239     if (items.size()==0) continue; // blank line
00240     else {
00241       if (items[0] == "#U")
00242         {
00243           if (items[1] == (std::string)"ADC") fObject->setUnitADC(true);
00244             else if (items[1] == (std::string)"fC") fObject->setUnitADC(false);
00245           else 
00246             {
00247               edm::LogWarning("Pedestal Unit Error") << "Unrecognized unit for pedestals. Assuming fC." << std::endl;
00248               fObject->setUnitADC(false);
00249             }
00250           break;
00251         }
00252       else
00253         {
00254           edm::LogWarning("Pedestal Unit Missing") << "The unit for the pedestals is missing in the txt file." << std::endl;
00255           return false;
00256         }
00257     }
00258   }
00259   while (fInput.getline(buffer, 1024)) {
00260     if (buffer [0] == '#') continue;
00261     std::vector <std::string> items = splitString (std::string (buffer));
00262     if (items.size()==0) continue; // blank line
00263     if (items.size () < 8) {
00264       edm::LogWarning("Format Error") << "Bad line: " << buffer << "\n line must contain 8 items: eta, phi, depth, subdet, 4x values" 
00265                                       << " or 12 items: eta, phi, depth, subdet, 4x values for mean, 4x values for width"
00266                                       << std::endl;
00267       continue;
00268     }
00269     DetId id = getId (items);
00270     
00271 //    if (fObject->exists(id) )
00272 //      edm::LogWarning("Redefining Channel") << "line: " << buffer << "\n attempts to redefine data. Ignored" << std::endl;
00273 //    else
00274 //      {
00275 
00276     if (items.size() < 12) // old format without widths
00277       {
00278         CastorPedestal* fCondObject = new CastorPedestal(id, atof (items [4].c_str()), atof (items [5].c_str()), 
00279                                                      atof (items [6].c_str()), atof (items [7].c_str()), 
00280                                                      0., 0., 0., 0. );
00281         fObject->addValues(*fCondObject);
00282         delete fCondObject;
00283       }
00284     else // new format with widths
00285       {
00286         CastorPedestal* fCondObject = new CastorPedestal(id, atof (items [4].c_str()), atof (items [5].c_str()), 
00287                                                      atof (items [6].c_str()), atof (items [7].c_str()), 
00288                                                      atof (items [8].c_str()), atof (items [9].c_str()),
00289                                                      atof (items [10].c_str()), atof (items [11].c_str()) );
00290         fObject->addValues(*fCondObject);
00291         delete fCondObject;
00292       }
00293 
00294         //      }
00295   }
00296   return true;
00297 }
00298 
00299 
00300 bool dumpObject (std::ostream& fOutput, const CastorPedestals& fObject) {
00301   char buffer [1024];
00302   if (fObject.isADC() ) sprintf (buffer, "#U ADC  << this is the unit \n");
00303   else  sprintf (buffer, "#U fC  << this is the unit \n");
00304   fOutput << buffer;
00305 
00306   sprintf (buffer, "# %15s %15s %15s %15s %8s %8s %8s %8s %8s %8s %8s %8s %10s\n", "eta", "phi", "dep", "det", "cap0", "cap1", "cap2", "cap3", "widthcap0", "widthcap1", "widthcap2", "widthcap3", "DetId");
00307   fOutput << buffer;
00308 
00309   std::vector<DetId> channels = fObject.getAllChannels ();
00310   std::sort (channels.begin(), channels.end(), DetIdLess ());
00311   for (std::vector<DetId>::iterator channel = channels.begin ();
00312        channel !=  channels.end ();
00313        channel++) {
00314     const float* values = fObject.getValues (*channel)->getValues ();
00315     if (values) {
00316       dumpId (fOutput, *channel);
00317       sprintf (buffer, " %8.5f %8.5f %8.5f %8.5f %8.5f %8.5f %8.5f %8.5f %10X\n",
00318                values[0], values[1], values[2], values[3], values[4], values[5], values[6], values[7], channel->rawId ());
00319       fOutput << buffer;
00320     }
00321   }
00322   return true;
00323 }
00324 
00325 
00326 // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
00327 bool getObject (std::istream& fInput, CastorChannelQuality* fObject) 
00328 {
00329   if (!fObject) fObject = new CastorChannelQuality;
00330   char buffer [1024];
00331   while (fInput.getline(buffer, 1024)) {
00332     if (buffer [0] == '#') continue; //ignore comment
00333     std::vector <std::string> items = splitString (std::string (buffer));
00334     if (items.size()==0) continue; // blank line
00335     if (items.size () < 5) {
00336       edm::LogWarning("Format Error") << "Bad line: " << buffer << "\n line must contain 5 items: eta, phi, depth, subdet, GOOD/BAD/HOT/DEAD" << std::endl;
00337       continue;
00338     }
00339     DetId id = getId (items);
00340     
00341     if (fObject->exists(id) ) {
00342       edm::LogWarning("Redefining Channel") << "line: " << buffer << "\n attempts to redefine data. Ignored" << std::endl;
00343       continue;
00344     }
00345 //    else
00346 //      {
00347     uint32_t mystatus;
00348     CastorChannelStatus* fCondObject = NULL;
00349     if (items[4].substr(0,2)=="0x") {
00350        sscanf(items[4].c_str(),"%X", &mystatus);
00351        fCondObject = new CastorChannelStatus(id,mystatus);
00352     }
00353     else if (isalpha(items[4].c_str()[0])) {
00354        fCondObject = new CastorChannelStatus(id, items[4]);
00355     }
00356     else {
00357        sscanf(items[4].c_str(),"%u", &mystatus);
00358        fCondObject = new CastorChannelStatus(id,mystatus);
00359     }
00360     fObject->addValues(*fCondObject);
00361     delete fCondObject;
00362         //      }
00363   }
00364   return true;
00365 }
00366 
00367 
00368 bool dumpObject (std::ostream& fOutput, const CastorChannelQuality& fObject) {
00369   char buffer [1024];
00370   sprintf (buffer, "# %15s %15s %15s %15s %15s %10s\n", "eta", "phi", "dep", "det", "value", "DetId");
00371   fOutput << buffer;
00372   std::vector<DetId> channels = fObject.getAllChannels ();
00373   std::sort (channels.begin(), channels.end(), DetIdLess ());
00374   for (std::vector<DetId>::iterator channel = channels.begin ();
00375        channel !=  channels.end ();
00376        channel++) {
00377     const int value = fObject.getValues (*channel)->getValue ();
00378     dumpId (fOutput, *channel);
00379     sprintf (buffer, " %15X %10X\n",
00380              value, channel->rawId ());
00381     fOutput << buffer;
00382   }
00383   return true;
00384 }
00385 
00386 
00387 // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
00388 bool getObject (std::istream& fInput, CastorPedestalWidths* fObject) {
00389   if (!fObject) fObject = new CastorPedestalWidths(false);
00390   char buffer [1024];
00391   int linecounter = 0;
00392 
00393   while (fInput.getline(buffer, 1024)) {
00394     linecounter++;
00395     std::vector <std::string> items = splitString (std::string (buffer));
00396     if (items.size()==0) continue; // blank line
00397     else {
00398       if (items[0] == (std::string)"#U")
00399         {
00400           if (items[1] == (std::string)"ADC") fObject->setUnitADC(true); 
00401           else if (items[1] == (std::string)"fC") fObject->setUnitADC(false);
00402           else 
00403             {
00404               edm::LogWarning("Pedestal Width Unit Error") << "Unrecognized unit for pedestal widths. Assuming fC." << std::endl;
00405               fObject->setUnitADC(false);
00406             }
00407           break;
00408         }
00409       else
00410         {
00411           edm::LogWarning("Pedestal Width Unit Missing") << "The unit for the pedestal widths is missing in the txt file." << std::endl;
00412           return false;
00413         }
00414     }
00415   }
00416 
00417   while (fInput.getline(buffer, 1024)) {
00418     linecounter++;
00419     if (buffer [0] == '#') continue; //ignore comment
00420     std::vector <std::string> items = splitString (std::string (buffer));
00421     if (items.size()==0) continue; // blank line
00422     if (items.size () < 14) {
00423       edm::LogWarning("Format Error") << "Bad line: " << buffer << "\n line number: " << linecounter << "\n line must contain 14 items: eta, phi, depth, subdet, 10x correlations" 
00424                                       << " or 20 items: eta, phi, depth, subdet, 16x correlations" 
00425                                       << std::endl;
00426       continue;
00427     }
00428     DetId id = getId (items);
00429 
00430 //    if (fObject->exists(id) )
00431 //      edm::LogWarning("Redefining Channel") << "line: " << buffer << "\n attempts to redefine data. Ignored" << std::endl;
00432 //    else
00433 //      {
00434 
00435     if (items.size() < 20) //old format
00436       {
00437         CastorPedestalWidth values(id);
00438         values.setSigma (0, 0, atof (items [4].c_str()));
00439         values.setSigma (1, 0, atof (items [5].c_str()));
00440         values.setSigma (1, 1, atof (items [6].c_str()));
00441         values.setSigma (2, 0, atof (items [7].c_str()));
00442         values.setSigma (2, 1, atof (items [8].c_str()));
00443         values.setSigma (2, 2, atof (items [9].c_str()));
00444         values.setSigma (3, 0, atof (items [10].c_str()));
00445         values.setSigma (3, 1, atof (items [11].c_str()));
00446         values.setSigma (3, 2, atof (items [12].c_str()));
00447         values.setSigma (3, 3, atof (items [13].c_str()));
00448         values.setSigma (0, 1, 0.);
00449         values.setSigma (0, 2, 0.);
00450         values.setSigma (0, 3, 0.);
00451         values.setSigma (1, 2, 0.);
00452         values.setSigma (1, 3, 0.);
00453         values.setSigma (2, 3, 0.);
00454         fObject->addValues(values);     
00455       }
00456     else // new format
00457       {
00458         CastorPedestalWidth values(id);
00459         values.setSigma (0, 0, atof (items [4].c_str()) );
00460         values.setSigma (0, 1, atof (items [5].c_str()) );
00461         values.setSigma (0, 2, atof (items [6].c_str()) );
00462         values.setSigma (0, 3, atof (items [7].c_str()) );
00463         values.setSigma (1, 0, atof (items [8].c_str()) );
00464         values.setSigma (1, 1, atof (items [9].c_str()) );
00465         values.setSigma (1, 2, atof (items [10].c_str()) );
00466         values.setSigma (1, 3, atof (items [11].c_str()) );
00467         values.setSigma (2, 0, atof (items [12].c_str()) );
00468         values.setSigma (2, 1, atof (items [13].c_str()) );
00469         values.setSigma (2, 2, atof (items [14].c_str()) );
00470         values.setSigma (2, 3, atof (items [15].c_str()) );
00471         values.setSigma (3, 0, atof (items [16].c_str()) );
00472         values.setSigma (3, 1, atof (items [17].c_str()) );
00473         values.setSigma (3, 2, atof (items [18].c_str()) );
00474         values.setSigma (3, 3, atof (items [19].c_str()) );
00475         fObject->addValues(values);     
00476       }
00477 
00478         //      }
00479   }
00480   return true;
00481 }
00482 
00483 bool dumpObject (std::ostream& fOutput, const CastorPedestalWidths& fObject) {
00484   char buffer [1024];
00485   if (fObject.isADC() ) sprintf (buffer, "#U ADC  << this is the unit \n");
00486   else  sprintf (buffer, "#U fC  << this is the unit \n");
00487   fOutput << buffer;
00488 
00489   sprintf (buffer, "# %15s %15s %15s %15s %8s %8s %8s %8s %8s %8s %8s %8s %8s %8s %8s %8s %8s %8s %8s %8s %10s\n", 
00490            "eta", "phi", "dep", "det", 
00491            "cov_0_0", "cov_0_1", "cov_0_2", "cov_0_3", "cov_1_0", "cov_1_1", "cov_1_2", "cov_1_3", "cov_2_0", "cov_2_1", "cov_2_2", "cov_2_3", "cov_3_0", "cov_3_1", "cov_3_2", "cov_3_3", 
00492            "DetId");
00493   fOutput << buffer;
00494   std::vector<DetId> channels = fObject.getAllChannels ();
00495   std::sort (channels.begin(), channels.end(), DetIdLess ());
00496   for (std::vector<DetId>::iterator channel = channels.begin ();
00497        channel !=  channels.end ();
00498        channel++) {
00499     const CastorPedestalWidth* item = fObject.getValues (*channel);
00500     if (item) {
00501       dumpId (fOutput, *channel);
00502       sprintf (buffer, " %8.5f %8.5f %8.5f %8.5f %8.5f %8.5f %8.5f %8.5f %8.5f %8.5f %8.5f %8.5f %8.5f %8.5f %8.5f %8.5f %10X\n",
00503                item->getSigma (0,0), item->getSigma (0,1), item->getSigma (0,2), item->getSigma (0,3), 
00504                item->getSigma (1,0), item->getSigma (1,1), item->getSigma (1,2), item->getSigma (1,3),
00505                item->getSigma (2,0), item->getSigma (2,1), item->getSigma (2,2), item->getSigma (2,3),
00506                item->getSigma (3,0), item->getSigma (3,1), item->getSigma (3,2), item->getSigma (3,3), channel->rawId ());
00507       fOutput << buffer;
00508     }
00509   }
00510   return true;
00511 }
00512 
00513 
00514 // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
00515 bool getObject (std::istream& fInput, CastorQIEData* fObject) {
00516   char buffer [1024];
00517   while (fInput.getline(buffer, 1024)) {
00518     if (buffer [0] == '#') continue; //ignore comment
00519     std::vector <std::string> items = splitString (std::string (buffer));
00520     if (items.size()<1) continue;
00521     if (items [0] == "SHAPE") { // basic shape
00522       if (items.size () < 33) {
00523         edm::LogWarning("Format Error") << "Bad line: " << buffer << "\n line must contain 33 items: SHAPE  32 x low QIE edges for first 32 bins" << std::endl;
00524         continue;
00525       }
00526       //float lowEdges [32];
00527       //int i = 32;
00528       //while (--i >= 0) lowEdges [i] = atof (items [i+1].c_str ());
00529       //      fObject->setShape (lowEdges);
00530     }
00531     else { // QIE parameters
00532       if (items.size () < 36) {
00533         edm::LogWarning("Format Error") << "Bad line: " << buffer << "\n line must contain 36 items: eta, phi, depth, subdet, 4 capId x 4 Ranges x offsets, 4 capId x 4 Ranges x slopes" << std::endl;
00534         continue;
00535       }
00536       DetId id = getId (items);
00537       fObject->sort ();
00538       //      try {
00539       //      fObject->getCoder (id);
00540       //      edm::LogWarning("Redefining Channel") << "line: " << buffer << "\n attempts to redefine data. Ignored" << std::endl;
00541         //      }
00542 //      catch (cms::Exception& e) {
00543         CastorQIECoder coder (id.rawId ());
00544         int index = 4;
00545         for (unsigned capid = 0; capid < 4; capid++) {
00546           for (unsigned range = 0; range < 4; range++) {
00547             coder.setOffset (capid, range, atof (items [index++].c_str ()));
00548           }
00549         }
00550         for (unsigned capid = 0; capid < 4; capid++) {
00551           for (unsigned range = 0; range < 4; range++) {
00552             coder.setSlope (capid, range, atof (items [index++].c_str ()));
00553           }
00554         }
00555         fObject->addCoder (coder);
00556 //      }
00557     }
00558   }
00559   fObject->sort ();
00560   return true;
00561 }
00562 
00563 bool dumpObject (std::ostream& fOutput, const CastorQIEData& fObject) {
00564   char buffer [1024];
00565   fOutput << "# QIE basic shape: SHAPE 32 x low edge values for first 32 channels" << std::endl;
00566   sprintf (buffer, "SHAPE ");
00567   fOutput << buffer;
00568   for (unsigned bin = 0; bin < 32; bin++) {
00569     sprintf (buffer, " %8.5f", fObject.getShape ().lowEdge (bin));
00570     fOutput << buffer;
00571   }
00572   fOutput << std::endl;
00573 
00574   fOutput << "# QIE data" << std::endl;
00575   sprintf (buffer, "# %15s %15s %15s %15s %36s %36s %36s %36s %36s %36s %36s %36s\n", 
00576            "eta", "phi", "dep", "det", 
00577            "4 x offsets cap0", "4 x offsets cap1", "4 x offsets cap2", "4 x offsets cap3",
00578            "4 x slopes cap0", "4 x slopes cap1", "4 x slopes cap2", "4 x slopes cap3");
00579   fOutput << buffer;
00580   std::vector<DetId> channels = fObject.getAllChannels ();
00581   std::sort (channels.begin(), channels.end(), DetIdLess ());
00582   for (std::vector<DetId>::iterator channel = channels.begin ();
00583        channel !=  channels.end ();
00584        channel++) {
00585     const CastorQIECoder* coder = fObject.getCoder (*channel);
00586     dumpId (fOutput, *channel);
00587     for (unsigned capid = 0; capid < 4; capid++) {
00588       for (unsigned range = 0; range < 4; range++) {
00589         sprintf (buffer, " %8.5f", coder->offset (capid, range));
00590         fOutput << buffer;
00591       }
00592     }
00593     for (unsigned capid = 0; capid < 4; capid++) {
00594       for (unsigned range = 0; range < 4; range++) {
00595         sprintf (buffer, " %8.5f", coder->slope (capid, range));
00596         fOutput << buffer;
00597       }
00598     }
00599     fOutput << std::endl;
00600   }
00601   return true;
00602 }
00603 
00604 // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
00605 bool getObject (std::istream& fInput, CastorCalibrationQIEData* fObject) {
00606   char buffer [1024];
00607   while (fInput.getline(buffer, 1024)) {
00608     if (buffer [0] == '#') continue; //ignore comment
00609     std::vector <std::string> items = splitString (std::string (buffer));
00610     if (items.size () < 36) {
00611       edm::LogWarning("Format Error") << "Bad line: " << buffer << "\n line must contain 36 items: eta, phi, depth, subdet, 32 bin values" << std::endl;
00612       continue;
00613     }
00614     DetId id = getId (items);
00615     fObject->sort ();
00616     //    try {
00617     //    fObject->getCoder (id);
00618     //    edm::LogWarning("Redefining Channel") << "line: " << buffer << "\n attempts to redefine data. Ignored" << std::endl;
00619       //    }
00620 //    catch (cms::Exception& e) {
00621       CastorCalibrationQIECoder coder (id.rawId ());
00622       int index = 4;
00623       float values [32];
00624       for (unsigned bin = 0; bin < 32; bin++) {
00625         values[bin] = atof (items [index++].c_str ());
00626       }
00627       coder.setMinCharges (values);
00628       fObject->addCoder (coder);
00629 //    }
00630   }
00631   fObject->sort ();
00632   return true;
00633 }
00634 
00635 bool dumpObject (std::ostream& fOutput, const CastorCalibrationQIEData& fObject) {
00636   char buffer [1024];
00637   fOutput << "# QIE data in calibration mode" << std::endl;
00638   sprintf (buffer, "# %15s %15s %15s %15s %288s\n", 
00639            "eta", "phi", "dep", "det", "32 x charges");
00640   fOutput << buffer;
00641   std::vector<DetId> channels = fObject.getAllChannels ();
00642   std::sort (channels.begin(), channels.end(), DetIdLess ());
00643   for (std::vector<DetId>::iterator channel = channels.begin ();
00644        channel !=  channels.end ();
00645        channel++) {
00646     const CastorCalibrationQIECoder* coder = fObject.getCoder (*channel);
00647     if (coder) {
00648       dumpId (fOutput, *channel);
00649       const float* lowEdge = coder->minCharges ();
00650       for (unsigned bin = 0; bin < 32; bin++) {
00651         sprintf (buffer, " %8.5f", lowEdge [bin]);
00652         fOutput << buffer;
00653       }
00654       fOutput << std::endl;
00655     }
00656   }
00657   return true;
00658 }
00659 
00660 
00661 // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
00662 bool getObject (std::istream& fInput, CastorElectronicsMap* fObject) {
00663   char buffer [1024];
00664   while (fInput.getline(buffer, 1024)) {
00665     if (buffer [0] == '#') continue; //ignore comment
00666     std::vector <std::string> items = splitString (std::string (buffer));
00667     if (items.size () < 12) {
00668       if (items.size()==0) continue; // no warning here
00669       if (items.size()<9) {
00670         edm::LogError("MapFormat") << "CastorElectronicsMap-> line too short: " << buffer;
00671         continue;
00672       }
00673       if (items[8]=="NA" || items[8]=="NT") {
00674         while (items.size()<12) items.push_back(""); // don't worry here
00675       } else if (items[8]=="HT") {
00676         if (items.size()==11) items.push_back("");
00677         else {
00678           edm::LogError("MapFormat") << "CastorElectronicsMap-> Bad line: " << buffer 
00679                                      << "\n HT line must contain at least 11 items: i  cr sl tb dcc spigot fiber fiberchan subdet=HT ieta iphi";
00680           continue;
00681         }
00682       } else {
00683         edm::LogError("MapFormat") << "CastorElectronicsMap-> Bad line: " << buffer 
00684                                    << "\n line must contain 12 items: i  cr sl tb dcc spigot fiber fiberchan subdet ieta iphi depth";
00685         continue;
00686       }
00687     }
00688     //    std::cout << "CastorElectronicsMap-> processing line: " << buffer << std::endl;
00689     int crate = atoi (items [1].c_str());
00690     int slot = atoi (items [2].c_str());
00691     int top = 1;
00692     if (items [3] == "b") top = 0;
00693     int dcc = atoi (items [4].c_str());
00694     int spigot = atoi (items [5].c_str());
00695     CastorElectronicsId elId;
00696     if (items[8] == "HT" || items[8] == "NT") {
00697       int slb = atoi (items [6].c_str());
00698       int slbCh = atoi (items [7].c_str());
00699       elId=CastorElectronicsId(slbCh, slb, spigot, dcc,crate,slot,top);
00700     } else {
00701       int fiber = atoi (items [6].c_str());
00702       int fiberCh = atoi (items [7].c_str());
00703 
00704       elId=CastorElectronicsId(fiberCh, fiber, spigot, dcc);
00705       elId.setHTR (crate, slot, top);
00706     }
00707 
00708     // first, handle undefined cases
00709     if (items [8] == "NA") { // undefined channel
00710       fObject->mapEId2chId (elId, DetId (HcalDetId::Undefined));
00711     } else if (items [8] == "NT") { // undefined trigger channel
00712       fObject->mapEId2tId (elId, DetId (HcalTrigTowerDetId::Undefined));
00713     } else {
00714       CastorText2DetIdConverter converter (items [8], items [9], items [10], items [11]);
00715       if (converter.isHcalCastorDetId ()) { 
00716         fObject->mapEId2chId (elId, converter.getId ());
00717       }
00718       else {
00719         edm::LogWarning("Format Error") << "CastorElectronicsMap-> Unknown subdetector: " 
00720                   << items [8] << '/' << items [9] << '/' << items [10] << '/' << items [11] << std::endl; 
00721       }
00722     }
00723   }
00724   fObject->sort ();
00725   return true;
00726 }
00727 
00728 bool dumpObject (std::ostream& fOutput, const CastorElectronicsMap& fObject) {
00729   std::vector<CastorElectronicsId> eids = fObject.allElectronicsId ();
00730   char buf [1024];
00731   // changes by Jared, 6.03.09/(included 25.03.09)
00732   //  sprintf (buf, "#%10s %6s %6s %6s %6s %6s %6s %6s %15s %15s %15s %15s",
00733   sprintf (buf, "# %7s %3s %3s %3s %4s %7s %10s %14s %7s %5s %5s %6s",
00734            "i", "cr", "sl", "tb", "dcc", "spigot", "fiber/slb", "fibcha/slbcha", "subdet", "ieta", "iphi", "depth");
00735   fOutput << buf << std::endl;
00736 
00737   for (unsigned i = 0; i < eids.size (); i++) {
00738     CastorElectronicsId eid = eids[i];
00739     if (eid.isTriggerChainId()) {
00740       DetId trigger = fObject.lookupTrigger (eid);
00741       if (trigger.rawId ()) {
00742         CastorText2DetIdConverter converter (trigger);
00743         // changes by Jared, 6.03.09/(included 25.03.09)
00744         //      sprintf (buf, " %10X %6d %6d %6c %6d %6d %6d %6d %15s %15s %15s %15s",
00745         sprintf (buf, " %7X %3d %3d %3c %4d %7d %10d %14d %7s %5s %5s %6s",
00746                  //              i,
00747                  converter.getId().rawId(),
00748                  // changes by Jared, 6.03.09/(included 25.03.09)
00749                  //              eid.readoutVMECrateId(), eid.htrSlot(), eid.htrTopBottom()>0?'t':'b', eid.dccid(), eid.spigot(), eid.fiberIndex(), eid.fiberChanId(),
00750                  eid.readoutVMECrateId(), eid.htrSlot(), eid.htrTopBottom()>0?'t':'b', eid.dccid(), eid.spigot(), eid.slbSiteNumber(), eid.slbChannelIndex(),
00751                  converter.getFlavor ().c_str (), converter.getField1 ().c_str (), converter.getField2 ().c_str (), converter.getField3 ().c_str ()
00752                  );
00753         fOutput << buf << std::endl;
00754       }
00755     } else {
00756       DetId channel = fObject.lookup (eid);
00757       if (channel.rawId()) {
00758         CastorText2DetIdConverter converter (channel);
00759         // changes by Jared, 6.03.09/(included 25.03.09)
00760         //      sprintf (buf, " %10X %6d %6d %6c %6d %6d %6d %6d %15s %15s %15s %15s",
00761         sprintf (buf, " %7X %3d %3d %3c %4d %7d %10d %14d %7s %5s %5s %6s",
00762                  //              i,
00763                  converter.getId().rawId(),
00764                  eid.readoutVMECrateId(), eid.htrSlot(), eid.htrTopBottom()>0?'t':'b', eid.dccid(), eid.spigot(), eid.fiberIndex(), eid.fiberChanId(),
00765                  converter.getFlavor ().c_str (), converter.getField1 ().c_str (), converter.getField2 ().c_str (), converter.getField3 ().c_str ()
00766                );
00767         fOutput << buf << std::endl;
00768       }
00769     }
00770   }
00771   return true;
00772 }
00773 
00774 bool getObject (std::istream& fInput, CastorRecoParams* fObject) {
00775         if (!fObject) fObject = new CastorRecoParams();
00776         char buffer [1024];
00777         while (fInput.getline(buffer, 1024)) {
00778                 if (buffer [0] == '#') continue; //ignore comment
00779                 std::vector <std::string> items = splitString (std::string (buffer));
00780                 if (items.size()==0) continue; // blank line
00781                 if (items.size () < 6) {
00782                         edm::LogWarning("Format Error") << "Bad line: " << buffer << "\n line must contain 6 items: eta, phi, depth, subdet, firstSample, samplesToAdd" << std::endl;
00783                         continue;
00784                 }
00785                 DetId id = getId (items);
00786               
00787                 CastorRecoParam* fCondObject = new CastorRecoParam(id, atoi (items [4].c_str()), atoi (items [5].c_str()) );
00788                 fObject->addValues(*fCondObject);
00789                 delete fCondObject;
00790         }
00791         return true;
00792 }
00793 
00794 bool dumpObject (std::ostream& fOutput, const CastorRecoParams& fObject) {
00795         char buffer [1024];
00796         sprintf (buffer, "# %15s %15s %15s %15s %18s %15s %10s\n", "eta", "phi", "dep", "det", "firstSample", "samplesToAdd", "DetId");
00797         fOutput << buffer;
00798         std::vector<DetId> channels = fObject.getAllChannels ();
00799         std::sort (channels.begin(), channels.end(), DetIdLess ());
00800         for (std::vector<DetId>::iterator channel = channels.begin();channel != channels.end();channel++) {
00801                 dumpId (fOutput, *channel);
00802                 sprintf (buffer, " %15d %15d %16X\n",
00803                 fObject.getValues (*channel)->firstSample(), fObject.getValues (*channel)->samplesToAdd(), channel->rawId ());
00804                 fOutput << buffer;
00805         }
00806         return true;
00807 }
00808 
00809 }