CMS 3D CMS Logo

/data/git/CMSSW_5_3_11_patch5/src/CondCore/HcalPlugins/plugins/HcalQIEDataPyWrapper.cc

Go to the documentation of this file.
00001 #include "CondFormats/HcalObjects/interface/HcalQIEData.h"
00002 #include "CondFormats/HcalObjects/interface/HcalQIECoder.h"
00003 
00004 #include "CondCore/Utilities/interface/PayLoadInspector.h"
00005 #include "CondCore/Utilities/interface/InspectorPythonWrapper.h"
00006 
00007 #include <string>
00008 #include <fstream>
00009 #include <sstream>
00010 
00011 #include "TH1F.h"
00012 #include "TH2F.h"
00013 #include "DataFormats/DetId/interface/DetId.h"
00014 #include "DataFormats/HcalDetId/interface/HcalDetId.h"
00015 
00016 #include "TROOT.h"
00017 #include "TCanvas.h"
00018 #include "TStyle.h"
00019 #include "TColor.h"
00020 #include "TLine.h"
00021 
00022 #include "math.h"
00023 //functions for correct representation of data in summary and plot:
00024 #include "CondCore/HcalPlugins/interface/HcalObjRepresent.h"
00025 using namespace HcalObjRepresent;
00026 
00027 namespace cond {
00028         template<>
00029         class ValueExtractor<HcalQIEData>: public  BaseValueExtractor<HcalQIEData> {
00030         public:
00031                 typedef HcalQIEData Class;
00032                 typedef ExtractWhat<Class> What;
00033                 static What what() { return What();}
00034 
00035                 ValueExtractor(){}
00036 
00037                 ValueExtractor(What const & what)
00038                 {
00039                         // here one can make stuff really complicated...
00040                 }
00041 
00042                 void compute(Class const & it){
00043                 }
00044         private:
00045 
00046         };
00047 
00048         class HcalQIEDataDataRepr: public ADataRepr
00049         {
00050         protected:
00051                 HcalQIEData::tAllContWithNames allContainers;
00052         public:
00053                 unsigned int fCapId;
00054                 unsigned int fRange;
00055                 bool slopeOrOffset;
00056 
00057                 HcalQIEDataDataRepr(unsigned int total, HcalQIEData::tAllContWithNames const & allCont)
00058                         :ADataRepr(total), allContainers(allCont), fCapId(0), fRange(0), slopeOrOffset(false){}
00059 
00060 
00061 
00062 
00063         protected:
00064                 void doFillIn(std::vector<TH2F> &graphData){
00065                         //ITERATORS AND VALUES:
00066                         HcalQIEData::tAllContWithNames::const_iterator iter;
00067                         std::vector<HcalQIECoder>::const_iterator contIter;
00068                         float value = 0.0;
00069 
00070                         //run trough all pair containers
00071                         for (iter = allContainers.begin(); iter != allContainers.end(); ++iter){
00072                                 //Run trough all values:
00073                                 for (contIter = (*iter).second.begin(); contIter != (*iter).second.end(); ++contIter){
00074                                         hcal_id = HcalDetId((uint32_t)(*contIter).rawId());
00075 
00076                                         depth = hcal_id.depth();
00077                                         if (depth<1 || depth>4) 
00078                                                 continue;
00079 
00080                                         ieta=hcal_id.ieta();
00081                                         iphi=hcal_id.iphi();
00082 
00083                                         if (hcal_id.subdet() == HcalForward)
00084                                                 ieta>0 ? ++ieta : --ieta;
00085 
00086                                         //GET VALUE:
00087                                         //unsigned fCapId, unsigned fRange;
00088                                         if (slopeOrOffset){
00089                                                 value = (*contIter).slope(fCapId, fRange);
00090                                         } else {
00091                                                 value = (*contIter).offset(fCapId, fRange);
00092                                         }
00093                                         //logstatus = log2(1.*channelBits)+1;
00094 
00095                                         //FILLING GOES HERE:
00096                                         graphData[depth-1].Fill(ieta,iphi, value);      
00097                                 }
00098                         }
00099                 }
00100         };
00101 
00102         std::string QIEDataCounter(const int nr, unsigned int &formated_nr, int base = 4){
00103                 int numer = nr;
00104                 int tens = 0, ones = 0;
00105                 if (numer >= 16){
00106                         numer = numer -16;
00107                 }
00108                 tens = numer / base;
00109                 ones = numer - (tens*base);
00110                 formated_nr = tens*10 + ones;
00111                 std::stringstream ss;
00112                 ss << tens << ones;
00113                 return ss.str();
00114         }
00115 
00116         template<>
00117         std::string PayLoadInspector<HcalQIEData>::summary() const {
00118                 std::stringstream ss;
00119                 // get all containers with names
00120                 HcalQIEData::tAllContWithNames allContainers = object().getAllContainers();
00121 
00122                 // initializing iterators
00123                 HcalQIEData::tAllContWithNames::const_iterator iter;
00124                 std::vector<HcalQIECoder>::const_iterator contIter;
00125 
00126                 ss << "Total HCAL containers: " << allContainers.size() << std::endl;
00127 
00128                 typedef std::pair<std::pair< float, float >, int> tPora;
00129 
00130                 //std::vector<tPora> vMaz(allContainers.size());
00131                 //std::vector<tPora>::iterator iMaz = vMaz.begin();
00132 
00133                 std::vector<tPora> vOffsets(allContainers.size());// for each container (total 8)
00134                 std::vector<tPora> vSlopes(allContainers.size());// for each container (total 8)
00135 
00136                 std::vector<tPora>::iterator iOffset = vOffsets.begin();
00137                 std::vector<tPora>::iterator iSlope = vSlopes.begin();
00138 
00139                 float offset = .0;
00140                 float slope = .0;
00141 
00142 
00143                 //Run trough all 8 detector containers:
00144                 for (iter = allContainers.begin(); iter != allContainers.end(); ++iter, ++iOffset, ++iSlope){
00145                         iOffset->second = (*iter).second.size();// total number of values in detector
00146                         iSlope->second = (*iter).second.size();// total number of values in detector
00147 
00148                         (iOffset->first).first = .0;
00149                         (iOffset->first).second = .0;
00150                         (iSlope->first).first = .0;
00151                         (iSlope->first).second = .0;
00152 
00153                         //Run trough all values in container
00154                         unsigned int i = 0;
00155                         for (contIter = (*iter).second.begin(); contIter != (*iter).second.end(); ++contIter){
00156 
00157                                 //Run trough all values in object:
00158                                 for (unsigned int fCapId = 0; fCapId < 4; ++fCapId){
00159                                         for (unsigned int fRange = 0; fRange < 4; ++fRange){
00160                                                 offset =  (*contIter).offset (fCapId, fRange);
00161                                                 (iOffset->first).first += offset;
00162                                                 (iOffset->first).second+= (offset * offset);
00163 
00164                                                 slope = (*contIter).slope (fCapId, fRange);
00165                                                 (iSlope->first).first += slope;
00166                                                 (iSlope->first).second+= (slope * slope);
00167                                         }
00168                                 }
00169                                 ++i;
00170                                 //ss << "[" << i << "] " << capValue << ", " << (iMaz->first).first[i] << ", " << (iMaz->first).second[i] << "; ";
00171                         }
00172                 }
00173                 //ss << std::endl;
00174 
00175                 //got all the values, now do the work:
00176                 iOffset = vOffsets.begin();
00177                 iSlope = vSlopes.begin();
00178                 float sumOffset = 0.0, averageOffset = 0.0, std_devOffset = 0.0, sqr_sumOffset = 0.0;
00179                 int sizeOffset = 0;
00180 
00181                 float sumSlope = 0.0, averageSlope = 0.0, std_devSlope = 0.0, sqr_sumSlope = 0.0;
00182                 int sizeSlope = 0;
00183 
00184                 sizeOffset = (*iOffset).second;
00185                 sizeSlope = (*iSlope).second;
00186 
00187 
00188                 
00189                 unsigned int i = 0;
00190                 for (iter = allContainers.begin(); iter != allContainers.end(); ++iter, ++i, ++iSlope, ++iOffset){
00191 
00192                         ss << "---------------------------------------------" << std::endl;
00193                         ss << "Detector: " << (*iter).first << ";    Total values: "<< (*iter).second.size() << std::endl;
00194                         sumOffset = ((*iOffset).first).first;
00195                         sqr_sumOffset = ((*iOffset).first).second;
00196                         averageOffset = sumOffset/sizeOffset;
00197                         //here needs to take absolute value for sqrt:
00198                         std_devOffset = sqrt( fabs((sqr_sumOffset / sizeOffset) - (averageOffset * averageOffset)) );
00199 
00200                         sumSlope = ((*iSlope).first).first;
00201                         sqr_sumSlope = ((*iSlope).first).second;
00202                         averageSlope = sumSlope/sizeSlope;
00203                         //here needs to take absolute value for sqrt:
00204                         std_devSlope = sqrt( fabs((sqr_sumSlope / sizeSlope) - (averageSlope * averageSlope)) );
00205 
00206                         ss  << "    Offset: " << std::endl;
00207                         ss      << "          Average: " << averageOffset << "; "<< std::endl;
00208                         ss      << "          Standart deviation: " << std_devOffset << "; " << std::endl;                                      
00209                         ss  << "    Slope: " << std::endl;
00210                         ss      << "          Average: " << averageSlope << "; "<< std::endl;
00211                         ss      << "          Standart deviation: " << std_devSlope << "; " << std::endl;
00212                 }               
00213                 //std::cout << ss.str();
00214                 return ss.str();
00215         }
00216 
00217         template<>
00218         std::string PayLoadInspector<HcalQIEData>::plot(std::string const & filename,
00219                 std::string const &,
00220                 std::vector<int> const&,
00221                 std::vector<float> const& ) const 
00222         {
00223 
00224                 //how much values are in container
00225                 unsigned int numOfValues = 32;
00226 
00227                 //create object helper for making plots;
00228                 HcalQIEDataDataRepr datarepr(numOfValues, object().getAllContainers());
00229 
00230                 typedef std::vector<TH2F> graphData;
00231                 std::vector< graphData > graphDataVec(numOfValues);
00232                 std::vector< graphData >::iterator imageIter;
00233                 imageIter = graphDataVec.begin();
00234 
00235 
00236 
00237                 std::string name = "_Offset_";
00238                 datarepr.nr = 0;
00239                 datarepr.id = 0;
00240                 datarepr.rootname.str("_Offsetrootvalue_");
00241                 datarepr.plotname.str("Offset ");
00242                 datarepr.filename.str("");
00243                 datarepr.filename << filename << name;
00244                 //Run trough all values in object:
00245                 datarepr.slopeOrOffset = false;
00246 
00247                 for (unsigned int fCapId = 0; fCapId < 4; ++fCapId){
00248                         for (unsigned int fRange = 0; fRange < 4; ++fRange){
00249                                 datarepr.fCapId = fCapId;
00250                                 datarepr.fRange = fRange;
00251 
00252                                 QIEDataCounter(datarepr.id, datarepr.nr);
00253 
00254                                 if (datarepr.nr == 0){
00255                                         datarepr.filename.str("");
00256                                         datarepr.filename << filename << name  <<"0";
00257                                 } else if ( datarepr.nr == 10)
00258                                 {
00259                                         datarepr.filename.str("");
00260                                         datarepr.filename << filename << name;
00261                                 }
00262 
00263 
00264 
00265                                 datarepr.fillOneGain((*imageIter));
00266 
00267 
00268                                 ++(datarepr.id);
00269                                 ++(datarepr.nr);
00270                                 ++(imageIter);
00271                         }
00272                 }
00274                 name = "_Slope_";
00275                 datarepr.rootname.str("_Sloperootname_");
00276                 datarepr.plotname.str("Slope ");
00277                 datarepr.filename.str("");
00278                 datarepr.filename << filename << name;  
00279 
00280                 datarepr.slopeOrOffset = true;
00281                 datarepr.nr = 0;
00282                 datarepr.id = 0;
00283 
00284                 for (unsigned int fCapId = 0; fCapId < 4; ++fCapId){
00285                         for (unsigned int fRange = 0; fRange < 4; ++fRange){
00286                                 datarepr.fCapId = fCapId;
00287                                 datarepr.fRange = fRange;
00288 
00289                                 QIEDataCounter(datarepr.id, datarepr.nr);
00290 
00291                                 if (datarepr.nr == 0){
00292                                         datarepr.filename.str("");
00293                                         datarepr.filename << filename << name  <<"0";
00294                                 } else if ( datarepr.nr == 10)
00295                                 {
00296                                         datarepr.filename.str("");
00297                                         datarepr.filename << filename << name;
00298                                 }
00299 
00300                                 datarepr.fillOneGain((*imageIter));
00301 
00302                                 ++(datarepr.id);
00303                                 ++(datarepr.nr);
00304                                 ++(imageIter);
00305                         }
00306                 }
00307 
00308 
00309                 return filename;
00310         }
00311 }
00312 
00313 PYTHON_WRAPPER(HcalQIEData,HcalQIEData);