CMS 3D CMS Logo

/data/git/CMSSW_5_3_11_patch5/src/CalibCalorimetry/CastorCalib/src/CastorDbXml.cc

Go to the documentation of this file.
00001 // F.Ratnikov (UMd), Oct 28, 2005
00002 // Modified by S. Won 6 May 2008
00003 // $Id: CastorDbXml.cc,v 1.2 2009/12/10 10:29:00 elmer Exp $
00004 //
00005 #include <vector>
00006 #include <string>
00007 
00008 
00009 #include "CalibFormats/CastorObjects/interface/CastorText2DetIdConverter.h"
00010 
00011 #include "CondFormats/CastorObjects/interface/AllObjects.h"
00012 #include "CalibCalorimetry/CastorCalib/interface/CastorDbXml.h"
00013 
00014 namespace {
00015   void dumpProlog (std::ostream& fOutput) {
00016     fOutput << "<?xml version='1.0' encoding='UTF-8'?>" << std::endl;
00017     fOutput << "<!DOCTYPE root []>" << std::endl;
00018     fOutput << "<ROOT>" << std::endl;
00019   }
00020 
00021   void dumpRun (std::ostream& fOutput, unsigned fRun) {
00022     fOutput << "<RUN>"<< std::endl;
00023     fOutput << "   <RUN_TYPE>" << "CastorDbXml" << "</RUN_TYPE>"<< std::endl;
00024     fOutput << "   <RUN_NUMBER>" << fRun << "</RUN_NUMBER>"<< std::endl;
00025     fOutput << "</RUN>" << std::endl;
00026   }
00027 
00028   void dumpHeader (std::ostream& fOutput, unsigned fRun, const std::string& fTableName, const std::string& fTypeName) {
00029     fOutput << "  <HEADER>" << std::endl;
00030     fOutput << "    <TYPE>" << std::endl;
00031     fOutput << "      <EXTENSION_TABLE_NAME>" << fTableName << "</EXTENSION_TABLE_NAME>" << std::endl;
00032     fOutput << "      <NAME>" << fTypeName << "</NAME>" << std::endl;
00033     fOutput << "    </TYPE>" << std::endl;
00034     dumpRun (fOutput, fRun);
00035     fOutput << "  </HEADER>" << std::endl;
00036   }
00037 
00038   void dumpFooter (std::ostream& fOutput) {
00039     fOutput << "</ROOT>" << std::endl;
00040   }
00041 
00042   void dumpChannelId (std::ostream& fOutput, DetId fChannel) {
00043     CastorText2DetIdConverter converter (fChannel);
00044     fOutput << "<CHANNEL> "<< std::endl;
00045     fOutput << "   <EXTENSION_TABLE_NAME>HCAL_CHANNELS</EXTENSION_TABLE_NAME> "<< std::endl;
00046     fOutput << "   <ETA>" << abs (converter.getField (1)) << "</ETA>"<< std::endl;
00047     fOutput << "   <PHI>" << converter.getField (2) << "</PHI> "<< std::endl;
00048     fOutput << "   <DEPTH>" << converter.getField (3) << "</DEPTH> "<< std::endl;
00049     fOutput << "   <Z>" << (converter.getField (1) > 0 ? "1" : "-1") << "</Z> "<< std::endl;
00050     fOutput << "   <DETECTOR_NAME>" << converter.getFlavor () << "</DETECTOR_NAME> "<< std::endl;
00051     fOutput << "   <HCAL_CHANNEL_ID>" << converter.getId().rawId () << "</HCAL_CHANNEL_ID> "<< std::endl;
00052     fOutput << "</CHANNEL>"<< std::endl;
00053     fOutput << std::endl;
00054   }
00055 
00056   void dumpData (std::ostream& fOutput, const float* fValues, const CastorPedestalWidth& fErrors) {
00057     fOutput << "<DATA> "<< std::endl;
00058     fOutput << "   <CAPACITOR_0_VALUE>" << fValues [0] << "</CAPACITOR_0_VALUE> "<< std::endl;
00059     fOutput << "   <CAPACITOR_1_VALUE>" << fValues [1] << "</CAPACITOR_1_VALUE> "<< std::endl;
00060     fOutput << "   <CAPACITOR_2_VALUE>" << fValues [2] << "</CAPACITOR_2_VALUE> "<< std::endl;  
00061     fOutput << "   <CAPACITOR_3_VALUE>" << fValues [3] << "</CAPACITOR_3_VALUE> "<< std::endl;
00062     fOutput << "   <SIGMA_0_0>" << fErrors.getSigma (0,0) << "</SIGMA_0_0> "<< std::endl;       
00063     fOutput << "   <SIGMA_1_1>" << fErrors.getSigma (1,1) << "</SIGMA_1_1> "<< std::endl;       
00064     fOutput << "   <SIGMA_2_2>" << fErrors.getSigma (2,2) << "</SIGMA_2_2> "<< std::endl;       
00065     fOutput << "   <SIGMA_3_3>" << fErrors.getSigma (3,3) << "</SIGMA_3_3> "<< std::endl;       
00066     fOutput << "   <SIGMA_0_1>" << fErrors.getSigma (1,0) << "</SIGMA_0_1> "<< std::endl;       
00067     fOutput << "   <SIGMA_0_2>" << fErrors.getSigma (2,0) << "</SIGMA_0_2> "<< std::endl;       
00068     fOutput << "   <SIGMA_0_3>" << fErrors.getSigma (3,0) << "</SIGMA_0_3> "<< std::endl;       
00069     fOutput << "   <SIGMA_1_2>" << fErrors.getSigma (2,1) << "</SIGMA_1_2> "<< std::endl;       
00070     fOutput << "   <SIGMA_1_3>" << fErrors.getSigma (3,1) << "</SIGMA_1_3> "<< std::endl;       
00071     fOutput << "   <SIGMA_2_3>" << fErrors.getSigma (3,2) << "</SIGMA_2_3> "<< std::endl;       
00072     fOutput << "</DATA> " << std::endl;
00073   }
00074 
00075   void dumpData (std::ostream& fOutput, const float* fValues, const float* fErrors) {
00076     fOutput << "<DATA> "<< std::endl;
00077     fOutput << "   <CAPACITOR_0_VALUE>" << fValues [0] << "</CAPACITOR_0_VALUE> "<< std::endl;
00078     fOutput << "   <CAPACITOR_1_VALUE>" << fValues [1] << "</CAPACITOR_1_VALUE> "<< std::endl;
00079     fOutput << "   <CAPACITOR_2_VALUE>" << fValues [2] << "</CAPACITOR_2_VALUE> "<< std::endl;
00080     fOutput << "   <CAPACITOR_3_VALUE>" << fValues [3] << "</CAPACITOR_3_VALUE> "<< std::endl;
00081     fOutput << "   <CAPACITOR_0_ERROR>" << fErrors [0] << "</CAPACITOR_0_ERROR> "<< std::endl;
00082     fOutput << "   <CAPACITOR_1_ERROR>" << fErrors [1] << "</CAPACITOR_1_ERROR> "<< std::endl;
00083     fOutput << "   <CAPACITOR_2_ERROR>" << fErrors [2] << "</CAPACITOR_2_ERROR> "<< std::endl;
00084     fOutput << "   <CAPACITOR_3_ERROR>" << fErrors [3] << "</CAPACITOR_3_ERROR> "<< std::endl;
00085     fOutput << "</DATA> " << std::endl;
00086   }
00087 
00088   void dumpDataset (std::ostream& fOutput, unsigned fVersion = 0, const std::string& fFileName = "", const std::string& fDescription = "") {
00089     fOutput << "<DATA_SET>" << std::endl;
00090     fOutput << "   <VERSION>" << fVersion << "</VERSION>" << std::endl;
00091     if (!fFileName.empty ()) 
00092       fOutput << "      <DATA_FILE_NAME>" << fFileName << "</DATA_FILE_NAME>" << std::endl;
00093     if (!fDescription.empty ())
00094       fOutput << "      <COMMENT_DESCRIPTION>" << fDescription << "</COMMENT_DESCRIPTION>" << std::endl;
00095   }
00096 
00097   void endDataset (std::ostream& fOutput) {
00098     fOutput << "</DATA_SET>" << std::endl;
00099   }
00100 
00101   void dumpMapping (std::ostream& fOutput, unsigned fRun, const std::string& fKind, 
00102                     unsigned long fGMTIOVBegin, unsigned long fGMTIOVEnd, 
00103                     const std::string& fTag, unsigned fVersion, const std::vector<DetId>& fChannels) {
00104     const std::string IOV_ID = "IOV_ID";
00105     const std::string TAG_ID = "TAG_ID";
00106     fOutput << "<ELEMENTS>" << std::endl;
00107     // set channels affected
00108     int i = fChannels.size ();
00109     while (--i >= 0) {
00110       fOutput << "<DATA_SET id=\"" << i << "\">" << std::endl;
00111       dumpRun (fOutput, fRun);
00112       fOutput << "<KIND_OF_CONDITION><NAME>" << fKind << "</NAME></KIND_OF_CONDITION>" << std::endl;
00113       dumpChannelId (fOutput, fChannels[i]);
00114       fOutput << "<VERSION>" << fVersion << "</VERSION>" << std::endl;
00115       fOutput << "</DATA_SET>" << std::endl;
00116     }
00117     // set IOV
00118     fOutput << "<IOV id=\"" << IOV_ID << "\">";
00119     fOutput << "   <INTERVAL_OF_VALIDITY_BEGIN>" << fGMTIOVBegin << "</INTERVAL_OF_VALIDITY_BEGIN>"<< std::endl;
00120     fOutput << "   <INTERVAL_OF_VALIDITY_END>" << fGMTIOVEnd << "</INTERVAL_OF_VALIDITY_END>"<< std::endl;
00121     fOutput << "</IOV>" << std::endl;
00122     // set TAG
00123     fOutput << "<TAG id=\"" << TAG_ID << "\" mode=\"create\">"<< std::endl;
00124     fOutput << "   <TAG_NAME>" << fTag << "</TAG_NAME>"<< std::endl;
00125     fOutput << "   <DETECTOR_NAME>HCAL</DETECTOR_NAME>"<< std::endl;
00126     fOutput << "   <COMMENT_DESCRIPTION>Automatically created by CastorDbXml</COMMENT_DESCRIPTION>" << std::endl;
00127     fOutput << "</TAG>" << std::endl;
00128 
00129     fOutput << "</ELEMENTS>" << std::endl;
00130 
00131     // mapping itself
00132     fOutput << "<MAPS>" << std::endl;
00133     fOutput << "<TAG idref=\"" << TAG_ID << "\">" << std::endl;
00134     fOutput << "<IOV idref=\"" << IOV_ID << "\">" << std::endl;
00135     i = fChannels.size ();
00136     while (--i >= 0) {
00137       fOutput << "<DATA_SET idref=\"" << i << "\"/>" << std::endl;
00138     }
00139     fOutput << "</IOV>" << std::endl;
00140     fOutput << "</TAG>" << std::endl;
00141     fOutput << "</MAPS>" << std::endl;
00142   }
00143 }
00144 
00145 
00146 
00147 bool CastorDbXml::dumpObject (std::ostream& fOutput, 
00148                             unsigned fRun, unsigned long fGMTIOVBegin, unsigned long fGMTIOVEnd, const std::string& fTag, unsigned fVersion, 
00149                             const CastorPedestals& fObject) {
00150   float dummyError = 0.0001;
00151   std::cout << "CastorDbXml::dumpObject-> set default errors: 0.0001, 0.0001, 0.0001, 0.0001" << std::endl;
00152   CastorPedestalWidths widths(fObject.isADC() );
00153   std::vector<DetId> channels = fObject.getAllChannels ();
00154   for (std::vector<DetId>::iterator channel = channels.begin ();
00155        channel !=  channels.end ();
00156        channel++) {
00157 
00158     CastorPedestalWidth item(*channel);
00159     for (int iCapId = 1; iCapId <= 4; iCapId++) {
00160       item.setSigma (iCapId, iCapId, dummyError*dummyError);
00161     }
00162     widths.addValues(item);
00163 
00164   }
00165   return dumpObject (fOutput, fRun, fGMTIOVBegin, fGMTIOVEnd, fTag, fVersion, fObject, widths);
00166 }
00167 
00168 bool CastorDbXml::dumpObject (std::ostream& fOutput, 
00169                             unsigned fRun, unsigned long fGMTIOVBegin, unsigned long fGMTIOVEnd, const std::string& fTag, unsigned fVersion, 
00170                             const CastorPedestals& fObject, const CastorPedestalWidths& fError) {
00171   const std::string KIND = "HCAL_PEDESTALS_V2";
00172 
00173   dumpProlog (fOutput);
00174   dumpHeader (fOutput, fRun, KIND, KIND);
00175 
00176   std::vector<DetId> channels = fObject.getAllChannels ();
00177   for (std::vector<DetId>::iterator channel = channels.begin ();
00178        channel !=  channels.end ();
00179        channel++) {
00180     DetId chId = *channel;
00181     const float* values = fObject.getValues (chId)->getValues ();
00182     const CastorPedestalWidth* errors = fError.getValues (chId);
00183     if (!values) {
00184       std::cerr << "CastorDbXml::dumpObject-> Can not get data for channel " << CastorText2DetIdConverter(chId).toString () << std::endl;
00185       continue;
00186     }
00187     if (!errors) {
00188       std::cerr << "CastorDbXml::dumpObject-> Can not get errors for channel " << CastorText2DetIdConverter(chId).toString () <<  ". Use defaults" << std::endl;
00189       continue;
00190     }
00191     dumpDataset (fOutput, fVersion, "", "");
00192     dumpChannelId (fOutput,chId); 
00193     dumpData (fOutput, values, *errors);
00194     endDataset (fOutput);
00195   }
00196   dumpMapping (fOutput, fRun, KIND, fGMTIOVBegin, fGMTIOVEnd, fTag, fVersion, channels);
00197 
00198   dumpFooter (fOutput);
00199   return true;
00200 }
00201 
00202 bool CastorDbXml::dumpObject (std::ostream& fOutput, 
00203                             unsigned fRun, unsigned long fGMTIOVBegin, unsigned long fGMTIOVEnd, const std::string& fTag, unsigned fVersion,
00204                             const CastorGains& fObject) {
00205   float dummyErrors [4] = {0., 0., 0., 0.};
00206   std::cout << "CastorDbXml::dumpObject-> set default errors: 4 x 0.0" << std::endl;
00207 
00208   CastorGainWidths widths;
00209   std::vector<DetId> channels = fObject.getAllChannels ();
00210   for (std::vector<DetId>::iterator channel = channels.begin (); channel !=  channels.end (); channel++) 
00211     {
00212       CastorGainWidth item(*channel,dummyErrors[0],dummyErrors[1],dummyErrors[2],dummyErrors[3]);
00213       widths.addValues(item);
00214     }
00215 
00216   return dumpObject (fOutput, fRun, fGMTIOVBegin, fGMTIOVEnd, fTag, fVersion, fObject, widths);
00217 }
00218 
00219 bool CastorDbXml::dumpObject (std::ostream& fOutput, 
00220                             unsigned fRun, unsigned long fGMTIOVBegin, unsigned long fGMTIOVEnd, const std::string& fTag, unsigned fVersion,
00221                             const CastorGains& fObject, const CastorGainWidths& fError) {
00222   const std::string KIND = "HCAL Gains";
00223   const std::string TABLE = "HCAL_GAIN_PEDSTL_CALIBRATIONS";
00224 
00225   dumpProlog (fOutput);
00226   dumpHeader (fOutput, fRun, TABLE, KIND);
00227 
00228   std::vector<DetId> channels = fObject.getAllChannels ();
00229   for (std::vector<DetId>::iterator channel = channels.begin ();
00230        channel !=  channels.end ();
00231        channel++) {
00232     DetId chId = *channel;
00233     const float* values = fObject.getValues (chId)->getValues ();
00234     const float* errors = fError.getValues (chId)->getValues ();
00235     if (!values) {
00236       std::cerr << "CastorDbXml::dumpObject-> Can not get data for channel " << CastorText2DetIdConverter(chId).toString () << std::endl;
00237       continue;
00238     }
00239     if (!errors) {
00240       std::cerr << "CastorDbXml::dumpObject-> Can not get errors for channel " << CastorText2DetIdConverter(chId).toString () <<  ". Use defaults" << std::endl;
00241       continue;
00242     }
00243     dumpDataset (fOutput, fVersion, "", "");
00244     dumpChannelId (fOutput,chId); 
00245     dumpData (fOutput, values, errors);
00246     endDataset (fOutput);
00247   }
00248   dumpMapping (fOutput, fRun, KIND, fGMTIOVBegin, fGMTIOVEnd, fTag, fVersion, channels);
00249 
00250   dumpFooter (fOutput);
00251   return true;
00252 }