CMS 3D CMS Logo

CMSSW_4_4_3_patch1/src/CalibCalorimetry/HcalStandardModules/src/HcalCholeskyDecomp.cc

Go to the documentation of this file.
00001 // Original Author:  Annabel Downs
00002 //         Created:  Wed Jul 29 10:54:11 CEST 2009
00003 #include <memory>
00004 #include "CalibCalorimetry/HcalStandardModules/interface/HcalCholeskyDecomp.h"
00005 
00006 HcalCholeskyDecomp::HcalCholeskyDecomp(const edm::ParameterSet& iConfig)
00007 {
00008   outfile = iConfig.getUntrackedParameter<std::string>("outFile","CholeskyMatrices.txt");
00009 }
00010 
00011 
00012 HcalCholeskyDecomp::~HcalCholeskyDecomp()
00013 {
00014 }
00015 
00016 void
00017 HcalCholeskyDecomp::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup)
00018 {
00019    using namespace edm;
00020    using namespace std;
00021 
00022    edm::ESHandle<HcalCovarianceMatrices> refCov;
00023    iSetup.get<HcalCovarianceMatricesRcd>().get("reference",refCov);
00024    const HcalCovarianceMatrices* myCov = refCov.product(); //Fill emap from database
00025 
00026    double sig[4][10][10];
00027    double c[4][10][10], cikik[4], cikjk[4], ct[4][10][10];
00028 
00029    HcalCholeskyMatrices * outMatrices = new HcalCholeskyMatrices();
00030 
00031    std::vector<DetId> listChan = myCov->getAllChannels();
00032    std::vector<DetId>::iterator cell;
00033 
00034    int HBcount = 0;
00035    int HEcount = 0;
00036    int HFcount = 0;
00037    int HOcount = 0;
00038 
00039    double HBmatrix[4][10][10];
00040    double HEmatrix[4][10][10];
00041    double HFmatrix[4][10][10];
00042    double HOmatrix[4][10][10];
00043 
00044    double tempmatrix[4][10][10] ;
00045 
00046 
00047    for(int m= 0; m != 4; m++){
00048       for(int i = 0; i != 10; i++){
00049          for(int j = 0; j!=10; j++){
00050           HBmatrix[m][i][j] = 0;
00051           HEmatrix[m][i][j] = 0;
00052           HFmatrix[m][i][j] = 0;
00053           HOmatrix[m][i][j] = 0;
00054          }
00055       }
00056    }
00057 
00058    for (std::vector<DetId>::iterator it = listChan.begin(); it != listChan.end(); it++)
00059    {
00060    for(int m= 0; m != 4; m++){
00061       for(int i = 0; i != 10; i++){
00062          for(int j = 0; j!=10; j++){
00063             sig[m][i][j] = 0;
00064             c[m][i][j] = 0;
00065             ct[m][i][j] = 0;
00066             cikik[m] =0;
00067             cikjk[m] = 0;
00068             tempmatrix[m][i][j] = 0;
00069          }
00070       }
00071    }
00072 
00073    const HcalCovarianceMatrix * CMatrix = myCov->getValues(*it);
00074    HcalDetId hcalid(it->rawId());
00075 
00076    for(int m = 0; m != 4; m++){
00077       for(int i = 0; i != 10; i++){
00078          for(int j = 0; j != 10; j++) {sig[m][i][j] = CMatrix->getValue(m,i,j);}
00079         }
00080    }
00081 
00082 //Method to check 4x10 storage
00083 //  for(int m = 0; m != 4; m++){
00084 //      for(int i = 0; i != 6; i++){
00085 //         for(int j=(i+4); j!=10; j++) {sig[m][i][j] = 0;}
00086 //        }
00087 //   }
00088         
00089 
00090    for(int m = 0; m != 4; m++){
00091       for(int i = 0; i != 10; i++){
00092          for(int j = i; j != 10; j++){ sig[m][j][i] = sig[m][i][j];}
00093       }
00094    }
00095    //.......................Cholesky Decomposition.............................
00096    //Step 1a
00097    for(int m = 0; m!=4; m++)
00098          {
00099         
00100                 c[m][0][0]=sqrt(sig[m][0][0]);
00101         }
00102   for(int m = 0; m!=4; m++)
00103    {
00104       for(int i = 1; i != 10; i++)
00105         {
00106          c[m][i][0] = (sig[m][0][i] / c[m][0][0]);
00107         }
00108    }
00109    
00110  //Chelesky Matrices
00111    for(int m = 0; m!=4; m++)
00112    {
00113       c[m][1][1] = sqrt(sig[m][1][1] - (c[m][1][0]*c[m][1][0])); // i) step 2a of chelesky decomp
00114       if (((sig[m][1][1]) - (c[m][1][0]*c[m][1][0]))<=0) continue;
00115       for(int i = 2; i !=10; i++)
00116         {
00117         cikik[m] = 0;
00118          for(int j=1; j!= i; j++)
00119                 {
00120                 cikjk[m] = 0;
00121                     for(int k=0; k != j; k++)
00122                         {
00123                         cikjk[m] += (c[m][i][k]*c[m][j][k]); // ii)  step 2a of chelesky decomp
00124                         }
00125                 c[m][i][j] = (sig[m][i][j] - cikjk[m])/c[m][j][j]; // step 3a of chelesky decomp
00126                 }//end of j loop
00127          
00128                 for( int k = 0; k != i; k++)
00129                 {
00130                          cikik[m] += (c[m][i][k]*c[m][i][k]);
00131                 }
00132                 double test = (sig[m][i][i] - cikik[m]);
00133                 if(test > 0 )
00134                 {
00135                         c[m][i][i] = sqrt(test);
00136                 }
00137                 else
00138                 {
00139                         c[m][i][i] = 1000;
00140                 }
00141         }//end of i loop
00142 /*
00143  //Cholesky Matrix for rechit (2-5 ts)
00144         for (int i = 0; i!=2; i++)
00145         {
00146           for (int j=0; j!=10; j++)
00147           {
00148                 c[m][i][j] = 0;
00149           }
00150         }
00151         for (int i = 6; i!=10; i++)
00152         {
00153           for (int j=0; j!=10; j++)
00154           {
00155                 c[m][i][j] = 0;
00156           }
00157         }
00158 */
00159 
00160 
00161    }//end of m loop
00162    
00163 
00164    HcalCholeskyMatrix * item = new HcalCholeskyMatrix(it->rawId());
00165    for(int m = 0; m != 4; m++){
00166       for(int i = 0; i != 10; i++){
00167          for(int j = i; j != 10; j++){ 
00168              item->setValue(m,j,i, c[m][j][i]);
00169              tempmatrix[m][j][i] = c[m][j][i];
00170          }//sig[m][j][i]
00171       }
00172    }
00173 
00174    if(hcalid.subdet()==1)
00175    {
00176       for(int m = 0; m != 4; m++)
00177          for(int i = 0; i != 10; i++)
00178             for(int j = 0; j != 10; j++)
00179                HBmatrix[m][i][j] += tempmatrix[m][i][j];
00180       HBcount++;
00181    }
00182    if(hcalid.subdet()==2)
00183    {
00184       for(int m = 0; m != 4; m++)
00185          for(int i = 0; i != 10; i++)
00186             for(int j = 0; j != 10; j++)
00187                HEmatrix[m][i][j] += tempmatrix[m][i][j];
00188       HEcount++;
00189    }
00190    if(hcalid.subdet()==3)
00191    {
00192       for(int m = 0; m != 4; m++)
00193          for(int i = 0; i != 10; i++)
00194             for(int j = 0; j != 10; j++)
00195                HOmatrix[m][i][j] += tempmatrix[m][i][j];
00196       HOcount++;
00197    }
00198    if(hcalid.subdet()==4)
00199    {
00200       for(int m = 0; m != 4; m++)
00201          for(int i = 0; i != 10; i++)
00202             for(int j = 0; j != 10; j++)
00203                HFmatrix[m][i][j] += tempmatrix[m][i][j];
00204       HFcount++;
00205    }
00206 
00207 
00208    
00209    outMatrices->addValues(*item);
00210 
00211    }
00212 
00213    for(int m = 0; m != 4; m++)
00214    {
00215       for(int i = 0; i != 10; i++)
00216       {
00217          for(int j = 0; j != 10; j++)
00218          {
00219             HBmatrix [m][i][j] /= HBcount;
00220             HEmatrix [m][i][j] /= HEcount;
00221             HFmatrix [m][i][j] /= HFcount;
00222             HOmatrix [m][i][j] /= HOcount;
00223          }
00224       }
00225    }
00226 
00227   std::vector<DetId> listResult = outMatrices->getAllChannels();
00228 
00229   int n_avg = 0;
00230 
00231   edm::ESHandle<HcalElectronicsMap> refEMap;
00232   iSetup.get<HcalElectronicsMapRcd>().get(refEMap);
00233 //  iSetup.get<HcalElectronicsMapRcd>().get("reference",refEMap);
00234   const HcalElectronicsMap* myRefEMap = refEMap.product();
00235   std::vector<HcalGenericDetId> listEMap = myRefEMap->allPrecisionId();
00236 
00237     for (std::vector<HcalGenericDetId>::const_iterator it = listEMap.begin(); it != listEMap.end(); it++)
00238       {
00239       DetId mydetid = DetId(it->rawId());
00240         if (std::find(listResult.begin(), listResult.end(), mydetid ) == listResult.end()  )
00241           {
00242 //              std::cout << "Cholesky Matrix not found for cell " <<  HcalGenericDetId(it->rawId());
00243               if(it->isHcalDetId())
00244               {
00245               std::cout << "Cholesky Matrix not found for cell " <<  HcalGenericDetId(it->rawId());
00246                  HcalDetId hcalid2(it->rawId());
00247                  HcalCholeskyMatrix * item = new HcalCholeskyMatrix(it->rawId());
00248                  for(int m = 0; m != 4; m++)
00249                  {
00250                     for(int i = 0; i != 10; i++)
00251                     {
00252                        for(int j = 0; j != 10; j++)
00253                        {
00254                           if(j <= i){
00255                           if(hcalid2.subdet()==1) item->setValue(m,i,j,HBmatrix [m][i][j]);
00256                           if(hcalid2.subdet()==2) item->setValue(m,i,j,HEmatrix [m][i][j]);
00257                           if(hcalid2.subdet()==3) item->setValue(m,i,j,HFmatrix [m][i][j]);
00258                           if(hcalid2.subdet()==4) item->setValue(m,i,j,HOmatrix [m][i][j]);
00259                           }else{
00260                           if(hcalid2.subdet()==1) item->setValue(m,i,j,0);
00261                           if(hcalid2.subdet()==2) item->setValue(m,i,j,0);
00262                           if(hcalid2.subdet()==3) item->setValue(m,i,j,0);
00263                           if(hcalid2.subdet()==4) item->setValue(m,i,j,0);
00264                           }
00265                        }
00266                     }
00267                  }
00268                  outMatrices->addValues(*item);
00269                  std::cout << "... Average Matrix used\n";
00270                  n_avg++;
00271               }
00272           }
00273       }
00274 
00275    std::vector<DetId> checkResult = outMatrices->getAllChannels();
00276    int count = 0;
00277    for(std::vector<DetId>::const_iterator it = checkResult.begin(); it != checkResult.end(); it++) count ++;
00278 
00279    std::cout << "There are " << count << " channels with Cholesky matrices.\n" << "Used  " << n_avg << " average values.\n";
00280 
00281    ofstream cmatout(outfile.c_str());
00282    HcalDbASCIIIO::dumpObject(cmatout, *outMatrices);
00283 }
00284 
00285 // ------------ meth(d called once each job just before starting event loop  ------------
00286 void 
00287 HcalCholeskyDecomp::beginJob()
00288 {
00289 }
00290 
00291 // ------------ method called once each job just after ending the event loop  ------------
00292 void 
00293 HcalCholeskyDecomp::endJob() {
00294 }
00295 
00296 //define this as a plug-in
00297 DEFINE_FWK_MODULE(HcalCholeskyDecomp);