CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC4_patch1/src/Calibration/Tools/src/InvMatrixUtils.cc

Go to the documentation of this file.
00001 
00008 #include "Calibration/Tools/interface/InvMatrixUtils.h"
00009 #include "Calibration/Tools/interface/InvMatrixCommonDefs.h"
00010 #include "TStyle.h"
00011 #include "TROOT.h"
00012 #include "CLHEP/Geometry/Point3D.h"
00013 //#include "ConfigParser.h"
00014 #include "Calibration/Tools/interface/matrixSaver.h"
00015 
00016 #include <iostream>
00017 #include <string>
00018 #include <fstream>
00019 #include <sstream>
00020 #include <vector>
00021 #include <cassert>
00022 
00024 void setStyle ()
00025 {
00026   gROOT->SetStyle ("Plain") ;
00027   gStyle->SetTextSize(0.5);  
00028   //gStyle->SetOptStat (1111111) ;
00029   gStyle->SetOptStat (0) ;
00030   //gStyle->SetOptFit (1111) ;
00031   gStyle->SetOptFit (0) ;
00032   gStyle->SetTitleBorderSize (0) ;
00033   gStyle->SetTitleX (0.08) ;
00034   gStyle->SetTitleY (0.97) ;
00035   gStyle->SetPalette (1,0) ;
00036   gStyle->SetStatColor (0) ;
00037   gStyle->SetFrameFillStyle (0) ;
00038   gStyle->SetFrameFillColor (0) ;
00039   return ;
00040 }
00041 
00042 
00043 // -----------------------------------------------------------------
00044 
00045 
00046 TCanvas * getGlobalCanvas (std::string name) 
00047 {
00048   setStyle () ;
00049   TCanvas * globalCanvas = static_cast<TCanvas*> 
00050                            (gROOT->FindObject (name.c_str ())) ;
00051   if (globalCanvas)
00052     {
00053       globalCanvas->Clear () ;
00054       globalCanvas->UseCurrentStyle () ;
00055       globalCanvas->SetWindowSize (700, 600) ;
00056     
00057     }
00058   else
00059     {
00060       globalCanvas = new TCanvas (name.c_str (),name.c_str (), 700, 600) ;
00061     }
00062   return globalCanvas ;
00063 }
00064 
00065 // -----------------------------------------------------------------
00066 
00067 
00068 TFile * getGlobalTFile (std::string name) 
00069 {
00070 //    std::cout << "writing " << name << std::endl ;
00071 //    setStyle () ;
00072     TFile * globalTFile = (TFile*) gROOT->FindObject (name.c_str()) ;
00073     if (!globalTFile)
00074         {
00075 //        std::cout << "does not exist. creating it " << std::endl;
00076         globalTFile = new TFile (name.c_str(),"RECREATE") ;
00077         }
00078     
00079     return globalTFile ;
00080 }
00081 
00082 
00083 // -----------------------------------------------------------------
00084 
00085 
00086 int saveGlobalTFile (std::string name) 
00087 {
00088   TFile * globalTFile = static_cast<TFile*> 
00089                          (gROOT->FindObject (name.c_str ())) ;
00090   if (!globalTFile) return 1 ;
00091   globalTFile->Write () ;
00092   globalTFile->Close () ;
00093   delete globalTFile ;
00094   return 0 ;
00095 }
00096 
00097 
00098 // -----------------------------------------------------------------
00099 
00100 
00101 CLHEP::HepMatrix * getSavedMatrix (const std::string & name) 
00102 {
00103   matrixSaver reader ;
00104   CLHEP::HepMatrix * savedMatrix ;
00105   if (reader.touch (name)) 
00106     {
00107        savedMatrix = static_cast<CLHEP::HepMatrix *> (
00108                        reader.getMatrix (name)
00109                      );
00110     }
00111   else
00112     {
00113        savedMatrix = new CLHEP::HepMatrix (SCMaxEta,SCMaxPhi,0) ;
00114     }
00115 
00116   return savedMatrix ;
00117 }
00118 
00119 
00120 
00121 //========================================================================
00122 //da usare 
00123 //HepGeom::Point3D<double> TBimpactPoint = TBposition (amplitude,m_beamEnergy) ;
00124 
00125 /* 
00126 dove trovare il codice di Chiara in CMSSW, per la ricostruzione
00127 della posizione:
00128 
00129 http://cmssw.cvs.cern.ch/cgi-bin/cmssw.cgi/CMSSW/RecoTBCalo/EcalTBAnalysisCoreTools/src/TBPositionCalc.cc?rev=1.1&cvsroot=CMSSW&content-type=text/vnd.viewcvs-markup
00130 
00131 */
00132 
00134 HepGeom::Point3D<Float_t>
00135 TBposition (const Float_t amplit[7][7], 
00136             const Float_t beamEne,
00137             const Float_t w0,
00138             const Float_t x0,
00139             const Float_t a0,
00140             const Float_t sideX, // crystal geometry, in mm
00141             const Float_t sideY)
00142 {
00143   // variables
00144     Float_t caloX = 0. ;
00145     Float_t caloY = 0. ;
00146     Float_t sumWeight = 0. ;
00147     Float_t depth = x0 * (log (beamEne)+ a0) ;  // shower depthh, in mm
00148     Float_t sin3 = 0.052335956 ; // sin (3 degrees) , sin3 = sin(3.*3.141592654/180.)
00149     
00150     Float_t invE3x3 = 1. / get3x3 (amplit) ;
00151     
00152   // loop over 3x3 crystals
00153   for (int eta = 2; eta <= 4; eta++)
00154       {
00155            for (int phi = 2; phi <= 4; phi++)
00156            {
00157             Float_t weight = log( amplit[eta][phi] * invE3x3) + w0 ;
00158             if ( weight>0 )
00159                {
00160                  caloX +=  (eta-3) * sideX * weight;
00161                  caloY -=  (phi-3) * sideY * weight;  
00162                  sumWeight += weight;
00163                }
00164             }
00165       }
00166       
00167   caloX /=sumWeight;
00168   caloY /=sumWeight;
00169   
00170   // correction for shower depthh
00171   caloX -= depth*sin3;
00172   caloY -= depth*sin3;
00173   
00174   // FIXME the z set to zero
00175   HepGeom::Point3D<Float_t> TBposition (caloX, caloY, 0) ;
00176   
00177   return TBposition ;  
00178   
00179 }   
00180 
00181 // -----------------------------------------------------------------
00182 
00183 
00186 double get5x5 (const Float_t energy[7][7]) 
00187 {
00188   double total = 0. ;
00189 
00190   for (int eta=1 ; eta<6 ; ++eta)
00191     for (int phi=1 ; phi<6 ; ++phi)
00192       total += energy[eta][phi] ;
00193 
00194   return total ;
00195 }
00196 
00197 
00198 // -----------------------------------------------------------------
00199 
00200 
00203 double get3x3 (const Float_t energy[7][7]) 
00204 {
00205   double total = 0. ;
00206 
00207   for (int eta=2 ; eta<5 ; ++eta)
00208     for (int phi=2 ; phi<5 ; ++phi)
00209       total += energy[eta][phi] ;
00210 
00211   return total ;
00212 }
00213 
00214 
00215 // -----------------------------------------------------------------
00216 
00217 
00219 /* int parseConfigFile (const TString& config)
00220 {
00221     if (gConfigParser) return 1 ;
00222     
00223     std::cout << "parsing "
00224         << config << " file"
00225         << std::endl ;
00226     
00227     
00228     gConfigParser = new ConfigParser () ;
00229     if ( !(gConfigParser->init(config)) )
00230         {
00231         std::cout << "Analysis::parseConfigFile: Could not open configuration file "
00232         << config << std::endl;
00233         perror ("Analysis::parseConfigFile: ") ;
00234         exit (-1) ;
00235         }
00236     gConfigParser->print () ;
00237     return 0 ;
00238 }*/
00239 
00240 
00241 // -----------------------------------------------------------------
00242 
00243 
00244 // per un certo eta, il cristallo puo' essere qualsiasi intero
00245 // Calibrationtra xmin e xmax
00246 // lo posso fissare solo sfruttando anche phi 
00247 int xtalFromEtaPhi (const int & myEta, const int & myPhi)
00248 {
00249     int xMin = 20 * myEta + 1 ;  
00250     int xMax = 20 * (myEta + 1) + 1 ;
00251     
00252     int myCryst = 999999 ;
00253     
00254     for (int x = xMin ; x < xMax ; x++)
00255         {
00256           if (phiFromXtal (x) == myPhi)
00257             myCryst = x ;
00258         }
00259     return myCryst ;
00260 }
00261 
00262 
00263 // -----------------------------------------------------------------
00264 
00265 
00266 int xtalFromiEtaiPhi (const int & iEta, const int & iPhi) 
00267 {
00268   assert (iEta >= 1) ;
00269   assert (iEta <= 85) ;
00270   assert (iPhi >= 1) ;
00271   assert (iPhi <= 20) ;
00272   return 20 * (iEta-1) + 21 - iPhi ;
00273 }
00274 
00275 
00276 // -----------------------------------------------------------------
00277 
00278 
00279 int etaFromXtal (const int & xtal) 
00280 {
00281 //  return floor (static_cast<double> ((xtal-1) / 20)) ;
00282   return int (floor ((xtal-1) / 20) );
00283 }
00284 
00285 
00286 // -----------------------------------------------------------------
00287 
00288 
00289 int phiFromXtal (const int & xtal) 
00290 {
00291   int phi = (xtal-1) - 20 * etaFromXtal (xtal) ;
00292   return (20 - phi - 1) ;
00293 }
00294 
00295 
00296 // -----------------------------------------------------------------
00297 
00298 
00299 int ietaFromXtal (const int & xtal) 
00300 {
00301   return etaFromXtal (xtal) + 1 ;
00302 }
00303 
00304 
00305 // -----------------------------------------------------------------
00306 
00307 
00308 int iphiFromXtal (const int & xtal) 
00309 {
00310   return phiFromXtal (xtal) + 1 ;
00311 }
00312 
00313 
00314 // -----------------------------------------------------------------
00315 
00316 
00317 int extract (std::vector<int> * output , const std::string & dati) 
00318   {
00319     std::ifstream _dati (dati.c_str ()) ;
00320     // loop over the file
00321     while (!_dati.eof())
00322       {
00323         // get the line
00324         std::string dataline ;
00325         do { getline (_dati, dataline,'\n') ; } 
00326         while (*dataline.begin () == '#') ;
00327         std::stringstream linea (dataline) ;
00328         // loop over the line
00329         while (!linea.eof ())
00330           {
00331             int buffer = -1 ;
00332             linea >> buffer ;
00333             if (buffer != -1) output->push_back (buffer) ;
00334           } // loop over the line
00335       } // loop over the file     
00336     return output->size () ;
00337   }
00338 
00339 
00340 // -----------------------------------------------------------------
00341 
00342 
00343 // FIXME questi eta, phi sono quelli della matrice CLHEP, 
00344 // FIXME non quelli del super-modulo, giusto?
00345 int writeCalibTxt (const CLHEP::HepMatrix & AmplitudeMatrix,
00346                    const CLHEP::HepMatrix & SigmaMatrix,
00347                    const CLHEP::HepMatrix & StatisticMatrix,
00348                    std::string fileName)
00349 {
00350   // look for the reference crystal
00351   double reference = 0. ;
00352   for (int eta = 0 ; eta<SCMaxEta ; ++eta)
00353     for (int phi = 0 ; phi<SCMaxPhi ; ++phi)
00354       {
00355         if (AmplitudeMatrix[eta][phi] && 
00356             SigmaMatrix[eta][phi] < 100 /*FIXME sigmaCut*/) 
00357           {
00358             reference = AmplitudeMatrix[eta][phi] ;
00359             std::cout << "[InvMatrixUtils][writeCalibTxt] reference crystal: "
00360                       << "(" << eta << "," << phi << ") -> "
00361                       << reference << "\n" ;
00362             break ;
00363           }
00364       }
00365   if (!reference)
00366     {
00367       std::cerr << "ERROR: no calibration coefficients found" << std::endl ;
00368       return 1 ;
00369     }
00370 
00371   // open the file for output
00372   std::ofstream txt_outfile ;
00373   txt_outfile.open (fileName.c_str ()) ;  
00374   txt_outfile << "# xtal\tcoeff\tsigma\tevt\tisGood\n" ; 
00375 
00376   // loop over the crystals
00377   for (int eta = 0 ; eta<SCMaxEta ; ++eta)
00378     for (int phi = 0 ; phi<SCMaxPhi ; ++phi)
00379       {
00380         int isGood = 1 ;
00381         if (AmplitudeMatrix[eta][phi] == 0) isGood = 0 ;
00382         if (SigmaMatrix[eta][phi] > 100 /*FIXME sigmaCut*/) isGood = 0 ;
00383         txt_outfile << xtalFromEtaPhi (eta,phi) 
00384                     << "\t" << AmplitudeMatrix[eta][phi]/reference 
00385                     << "\t" << SigmaMatrix[eta][phi]
00386                     << "\t" << StatisticMatrix[eta][phi]
00387                     << "\t" << isGood <<"\n" ;
00388       }
00389 
00390   // save and close the file 
00391   txt_outfile.close () ;
00392   return 0 ;
00393 }
00394 
00395 
00396 // -----------------------------------------------------------------
00397 
00398 
00399 int writeCMSSWCoeff (const CLHEP::HepMatrix & amplMatrix,
00400                      double calibThres,
00401                      float ERef,
00402                      const CLHEP::HepMatrix & sigmaMatrix,
00403                      const CLHEP::HepMatrix & statisticMatrix,
00404                      std::string fileName,
00405                      std::string genTag,
00406                      std::string method,
00407                      std::string version,
00408                      std::string type) 
00409 {
00410   // open the file for output
00411   std::ofstream txt_outfile ;
00412   txt_outfile.open (fileName.c_str ()) ;  
00413   txt_outfile << "1\n" ; // super-module number 
00414   txt_outfile << "-1\n" ; // number of events
00415   txt_outfile << genTag << "\n" ;
00416   txt_outfile << method << "\n" ;
00417   txt_outfile << version << "\n" ;
00418   txt_outfile << type << "\n" ;
00419 
00420   double reference = ERef ;
00421 
00422   // loop over crystals
00423   for (int eta = 0 ; eta < SCMaxEta ; ++eta)
00424     for (int phi = 0 ; phi < SCMaxPhi ; ++phi)
00425       {
00426         if (amplMatrix[eta][phi] <= calibThres) 
00427           txt_outfile << xtalFromiEtaiPhi (eta+1,phi+1) 
00428                       << "\t" << 1 
00429                       << "\t" << -1
00430                       << "\t" << -1
00431                       << "\t" << 0 <<"\n" ;
00432         else  
00433           txt_outfile << xtalFromiEtaiPhi (eta+1,phi+1) 
00434                       << "\t" << reference / amplMatrix[eta][phi] 
00435                       << "\t" << sigmaMatrix[eta][phi]
00436                       << "\t" << statisticMatrix[eta][phi]
00437                       << "\t" << 1 <<"\n" ;
00438       } // loop over crystals
00439 
00440   // save and close the file 
00441   txt_outfile.close () ;
00442   return 0 ;
00443 }                     
00444 
00445 
00446 // -----------------------------------------------------------------
00447 
00448 
00449 int writeCMSSWCoeff (const CLHEP::HepMatrix & amplMatrix,
00450                      double calibThres,
00451                      int etaRef, int phiRef,
00452                      const CLHEP::HepMatrix & sigmaMatrix,
00453                      const CLHEP::HepMatrix & statisticMatrix,
00454                      std::string fileName,
00455                      std::string genTag,
00456                      std::string method,
00457                      std::string version,
00458                      std::string type) 
00459 {
00460   // open the file for output
00461     std::ofstream txt_outfile ;
00462     txt_outfile.open (fileName.c_str ()) ;  
00463     txt_outfile << "1\n" ; // super-module number 
00464     txt_outfile << "-1\n" ; // number of events
00465     txt_outfile << genTag << "\n" ;
00466     txt_outfile << method << "\n" ;
00467     txt_outfile << version << "\n" ;
00468     txt_outfile << type << "\n" ;
00469     
00470     if (amplMatrix[etaRef-1][phiRef-1] == 0)
00471         {
00472         std::cerr << "The reference crystal: ("
00473         << etaRef << "," << phiRef
00474         << ") is out of range\n" ;
00475         return 1 ;
00476         }
00477     double reference = amplMatrix[etaRef-1][phiRef-1] ;
00478     
00479   // loop over crystals
00480     for (int eta = 0 ; eta < SCMaxEta ; ++eta)
00481         for (int phi = 0 ; phi < SCMaxPhi ; ++phi)
00482             {
00483             if (amplMatrix[eta][phi] <= calibThres) 
00484                 txt_outfile << xtalFromiEtaiPhi (eta+1,phi+1) 
00485                     << "\t" << 1 
00486                     << "\t" << -1
00487                     << "\t" << -1
00488                     << "\t" << 0 <<"\n" ;
00489             else  
00490                 txt_outfile << xtalFromiEtaiPhi (eta+1,phi+1) 
00491                     << "\t" << reference / amplMatrix[eta][phi] 
00492                     << "\t" << sigmaMatrix[eta][phi]
00493                     << "\t" << statisticMatrix[eta][phi]
00494                     << "\t" << 1 <<"\n" ;
00495             } // loop over crystals
00496             
00497   // save and close the file 
00498     txt_outfile.close () ;
00499     return 0 ;
00500 }                    
00501 
00502 
00503 // -----------------------------------------------------------------
00504 
00505 
00506 int translateCoeff (const CLHEP::HepMatrix & calibcoeff,
00507                     const CLHEP::HepMatrix & sigmaMatrix,
00508                     const CLHEP::HepMatrix & statisticMatrix,
00509                     std::string SMnumber,
00510                     double calibThres,
00511                     std::string fileName,
00512                     std::string genTag,
00513                     std::string method,
00514                     std::string version,
00515                     std::string type) 
00516 {
00517     // open the file for output
00518     std::ofstream txt_outfile ;
00519     txt_outfile.open (fileName.c_str ()) ;  
00520     txt_outfile << SMnumber << "\n" ; // super-module number 
00521     txt_outfile << "-1\n" ; // number of events
00522     txt_outfile << genTag << "\n" ;
00523     txt_outfile << method << "\n" ;
00524     txt_outfile << version << "\n" ;
00525     txt_outfile << type << "\n" ;
00526     
00527     // loop over crystals
00528     for (int eta = 0 ; eta < SCMaxEta ; ++eta)
00529         for (int phi = 0 ; phi < SCMaxPhi ; ++phi)
00530             {
00531             if (calibcoeff[eta][phi] < calibThres) 
00532               {
00533                 txt_outfile << xtalFromiEtaiPhi (eta+1,phi+1) 
00534                     << "\t" << 1 
00535                     << "\t" << -1
00536                     << "\t" << -1
00537                     << "\t" << 0 <<"\n" ;
00538                 std::cout << "[translateCoefff][" << SMnumber 
00539                     << "]\t WARNING crystal " << xtalFromiEtaiPhi (eta+1,phi+1)
00540                     << " calib coeff below threshold: " 
00541                     << "\t" << 1 
00542                     << "\t" << -1
00543                     << "\t" << -1
00544                     << "\t" << 0 <<"\n" ;
00545               }
00546             else  
00547                 txt_outfile << xtalFromiEtaiPhi (eta+1,phi+1) 
00548                     << "\t" << calibcoeff[eta][phi] 
00549                     << "\t" << sigmaMatrix[eta][phi]
00550                     << "\t" << statisticMatrix[eta][phi]
00551                     << "\t" << 1 <<"\n" ;
00552             } // loop over crystals
00553             
00554     // save and close the file 
00555     txt_outfile.close () ;
00556     return 0 ;
00557 }                    
00558 
00559 
00560 // -----------------------------------------------------------------
00561 
00562 
00563 int readCMSSWcoeff (CLHEP::HepMatrix & calibcoeff,
00564                     const std::string & inputFileName,
00565                     double defaultVal) 
00566 {
00567     std::ifstream CMSSWfile ;
00568     CMSSWfile.open (inputFileName.c_str ()) ; 
00569     std::string buffer ; 
00570     CMSSWfile >> buffer ;  
00571     CMSSWfile >> buffer ; 
00572     CMSSWfile >> buffer ;
00573     CMSSWfile >> buffer ;
00574     CMSSWfile >> buffer ;
00575     CMSSWfile >> buffer ;
00576     while (!CMSSWfile.eof ())
00577       {
00578         int xtalnum ;
00579         CMSSWfile >> xtalnum ;
00580         double coeff ;
00581         CMSSWfile >> coeff ;
00582         double buffer ;
00583         CMSSWfile >> buffer ;
00584         int good ;
00585         CMSSWfile >> good ;
00586         CMSSWfile >> good ;
00587         if (!good) coeff = defaultVal ; //FIXME 0 o 1?
00588         calibcoeff[etaFromXtal (xtalnum)][phiFromXtal (xtalnum)] = coeff ;          
00589       }       
00590     return 0 ;
00591 
00592 }                    
00593 
00594 
00595 // -----------------------------------------------------------------
00596 
00597 
00598 int readCMSSWcoeffForComparison (CLHEP::HepMatrix & calibcoeff,
00599                     const std::string & inputFileName) 
00600 {
00601     std::ifstream CMSSWfile ;
00602     CMSSWfile.open (inputFileName.c_str ()) ; 
00603     std::string buffer ; 
00604     CMSSWfile >> buffer ;  
00605     CMSSWfile >> buffer ; 
00606     CMSSWfile >> buffer ;
00607     CMSSWfile >> buffer ;
00608     CMSSWfile >> buffer ;
00609     CMSSWfile >> buffer ;
00610     while (!CMSSWfile.eof ())
00611       {
00612         int xtalnum ;
00613         CMSSWfile >> xtalnum ;
00614         double coeff ;
00615         CMSSWfile >> coeff ;
00616         double buffer ;
00617         CMSSWfile >> buffer ;
00618         int good ;
00619         CMSSWfile >> good ;
00620         CMSSWfile >> good ;
00621         if (!good) coeff = 0. ; //FIXME 0 o 1?
00622         calibcoeff[etaFromXtal (xtalnum)][phiFromXtal (xtalnum)] = coeff ;          
00623       }       
00624     return 0 ;
00625 
00626 }                    
00627 
00628 
00629 // -----------------------------------------------------------------
00630 
00631 
00632 TH1D * smartProfile (TH2F * strip, double width) 
00633 {
00634   TProfile * stripProfile = strip->ProfileX () ;
00635 
00636   // (from FitSlices of TH2.h)
00637 
00638   double xmin = stripProfile->GetXaxis ()->GetXmin () ;
00639   double xmax = stripProfile->GetXaxis ()->GetXmax () ;
00640   int profileBins = stripProfile->GetNbinsX () ;
00641 
00642   std::string name = strip->GetName () ;
00643   name += "_smart" ; 
00644   TH1D * prof = new TH1D
00645       (name.c_str (),strip->GetTitle (),profileBins,xmin,xmax) ;
00646    
00647   int cut = 0 ; // minimum number of entries per fitted bin
00648   int nbins = strip->GetXaxis ()->GetNbins () ;
00649   int binmin = 1 ;
00650   int ngroup = 1 ; // bins per step
00651   int binmax = nbins ;
00652 
00653   // loop over the strip bins
00654   for (int bin=binmin ; bin<=binmax ; bin += ngroup) 
00655     {
00656       TH1D *hpy = strip->ProjectionY ("_temp",bin,bin+ngroup-1,"e") ;
00657       if (hpy == 0) continue ;
00658       int nentries = Int_t (hpy->GetEntries ()) ;
00659       if (nentries == 0 || nentries < cut) {delete hpy ; continue ;} 
00660  
00661       Int_t biny = bin + ngroup/2 ;
00662       
00663       hpy->GetXaxis ()->SetRangeUser ( hpy->GetMean () - width * hpy->GetRMS (), 
00664                                        hpy->GetMean () + width * hpy->GetRMS ()) ;         
00665       prof->Fill (strip->GetXaxis ()->GetBinCenter (biny),
00666                   hpy->GetMean ()) ;       
00667       prof->SetBinError (biny,hpy->GetRMS()) ;
00668       
00669       delete hpy ;
00670     } // loop over the bins
00671 
00672   delete stripProfile ;
00673   return prof ;
00674 }
00675 
00676 
00677 // -----------------------------------------------------------------
00678 
00679 
00680 TH1D * smartGausProfile (TH2F * strip, double width) 
00681 {
00682   TProfile * stripProfile = strip->ProfileX () ;
00683 
00684   // (from FitSlices of TH2.h)
00685 
00686   double xmin = stripProfile->GetXaxis ()->GetXmin () ;
00687   double xmax = stripProfile->GetXaxis ()->GetXmax () ;
00688   int profileBins = stripProfile->GetNbinsX () ;
00689 
00690   std::string name = strip->GetName () ;
00691   name += "_smartGaus" ; 
00692   TH1D * prof = new TH1D
00693       (name.c_str (),strip->GetTitle (),profileBins,xmin,xmax) ;
00694    
00695   int cut = 0 ; // minimum number of entries per fitted bin
00696   int nbins = strip->GetXaxis ()->GetNbins () ;
00697   int binmin = 1 ;
00698   int ngroup = 1 ; // bins per step
00699   int binmax = nbins ;
00700 
00701   // loop over the strip bins
00702   for (int bin=binmin ; bin<=binmax ; bin += ngroup) 
00703     {
00704       TH1D *hpy = strip->ProjectionY ("_temp",bin,bin+ngroup-1,"e") ;
00705       if (hpy == 0) continue ;
00706       int nentries = Int_t (hpy->GetEntries ()) ;
00707       if (nentries == 0 || nentries < cut) {delete hpy ; continue ;} 
00708  
00709       Int_t biny = bin + ngroup/2 ;
00710 
00711       TF1 * gaussian = new TF1 ("gaussian","gaus", hpy->GetMean () - width * hpy->GetRMS (),
00712                                                    hpy->GetMean () + width * hpy->GetRMS ()) ; 
00713       gaussian->SetParameter (1,hpy->GetMean ()) ;
00714       gaussian->SetParameter (2,hpy->GetRMS ()) ;
00715       hpy->Fit ("gaussian","RQL") ;           
00716 
00717       hpy->GetXaxis ()->SetRangeUser ( hpy->GetMean () - width * hpy->GetRMS (), 
00718                                        hpy->GetMean () + width * hpy->GetRMS ()) ;         
00719       prof->Fill (strip->GetXaxis ()->GetBinCenter (biny),
00720                   gaussian->GetParameter (1)) ;       
00721       prof->SetBinError (biny,gaussian->GetParameter (2)) ;
00722       
00723       delete gaussian ;
00724       delete hpy ;
00725     } // loop over the bins
00726 
00727   delete stripProfile ;
00728   return prof ;
00729 }
00730 
00731 
00732 // -----------------------------------------------------------------
00733 
00734 
00735 TH1D * smartError (TH1D * strip)
00736 {
00737 
00738   double xmin = strip->GetXaxis ()->GetXmin () ;
00739   double xmax = strip->GetXaxis ()->GetXmax () ;
00740   int stripsBins = strip->GetNbinsX () ;
00741 
00742   std::string name = strip->GetName () ;
00743   name += "_error" ; 
00744   TH1D * error = new TH1D
00745       (name.c_str (),strip->GetTitle (),stripsBins,xmin,xmax) ;
00746 
00747   int binmin = 1 ;
00748   int ngroup = 1 ; // bins per step
00749   int binmax = stripsBins ;
00750   for (int bin=binmin ; bin<=binmax ; bin += ngroup) 
00751     {
00752       double dummyError = strip->GetBinError (bin) ; 
00753       error->SetBinContent (bin,dummyError) ;
00754     }
00755   return error;  
00756 }
00757 
00758 
00759 // -----------------------------------------------------------------
00760 
00761 
00762 double effectiveSigma (TH1F & histogram, int vSteps) 
00763 {
00764   double totInt = histogram.Integral () ;
00765   int maxBin = histogram.GetMaximumBin () ;
00766   int maxBinVal = int(histogram.GetBinContent (maxBin)) ;
00767   int totBins = histogram.GetNbinsX () ;
00768   double area = totInt ;
00769   double threshold = 0 ;
00770   double vStep = maxBinVal / vSteps ;
00771   int leftBin = 1 ;
00772   int rightBin = totBins - 1 ;
00773   //loop over the vertical range
00774   while (area/totInt > 0.683)
00775     {
00776       threshold += vStep ;
00777       // loop toward the left
00778       for (int back = maxBin ; back > 0 ; --back)
00779          {
00780            if (histogram.GetBinContent (back) < threshold)
00781              {
00782                leftBin = back ;
00783                break ;
00784              }
00785          } // loop toward the left
00786 
00787       // loop toward the right   
00788       for (int fwd = maxBin ; fwd < totBins ; ++fwd)
00789          {
00790            if (histogram.GetBinContent (fwd) < threshold)
00791              {
00792                rightBin = fwd ;
00793                break ;
00794              }
00795          } // loop toward the right
00796        area = histogram.Integral (leftBin,rightBin) ;
00797     } //loop over the vertical range
00798 
00799   histogram.GetXaxis ()->SetRange (leftBin,rightBin) ;
00800   // double sigmaEff = histogram.GetRMS () ;
00801   double halfWidthRange = 0.5 * (histogram.GetBinCenter (rightBin) - histogram.GetBinCenter (leftBin)) ;
00802   return halfWidthRange ;
00803 }
00804 
00805 
00806 // -----------------------------------------------------------------
00807 
00808 
00809 std::pair<int,int> findSupport (TH1F & histogram, double thres) 
00810 {
00811   int totBins = histogram.GetNbinsX () ;
00812   if (thres >= histogram.GetMaximum ()) 
00813     return std::pair<int,int> (0, totBins) ;
00814 
00815   int leftBin = totBins - 1 ;
00816   // search from left for the minimum
00817   for (int bin=1 ; bin<totBins ; ++bin)
00818     {
00819       if (histogram.GetBinContent (bin) > thres)
00820         {
00821           leftBin = bin ;
00822           break ; 
00823         }    
00824     } // search from left for the minimum
00825   int rightBin = 1 ;
00826   // search from right for the maximum
00827   for (int bin=totBins - 1 ; bin> 0 ; --bin)
00828     {
00829       if (histogram.GetBinContent (bin) > thres)
00830         {
00831           rightBin = bin ;
00832           break ; 
00833         }    
00834     } // search from right for the maximum
00835   return std::pair<int,int> (leftBin,rightBin) ;  
00836 }
00837 
00838 
00839 // -----------------------------------------------------------------
00840 
00841 
00842 void
00843 mtrTransfer (double output[SCMaxEta][SCMaxPhi], 
00844              CLHEP::HepMatrix * input, 
00845              double Default)
00846 {
00847   for (int eta = 0 ; eta < SCMaxEta ; ++eta)                              
00848     for (int phi = 0 ; phi < SCMaxPhi ; ++phi)                              
00849       {
00850         if ((*input)[eta][phi]) 
00851         output[eta][phi] = (*input)[eta][phi] ;
00852         else output[eta][phi] = Default ;
00853       }
00854   return ;
00855 }
00856 
00857 // -----------------------------------------------------------------
00858 
00859 double etaCorrE1E25 (int eta)
00860 {
00861     double p0 = 0.807883 ;
00862     double p1 = 0.000182551 ;
00863     double p2 = -5.76961e-06 ;
00864     double p3 = 7.41903e-08 ;
00865     double p4 = -2.25384e-10 ;
00866     
00867     double corr ;
00868     if (eta < 6) corr = p0 ;
00869     else corr = p0 + p1*eta + p2*eta*eta + p3*eta*eta*eta + p4*eta*eta*eta*eta;
00870     return corr/p0 ;
00871 }
00872 // -----------------------------------------------------------------
00873 
00874 double etaCorrE1E49 (int eta)
00875 {
00876     double p0 = 0.799895 ;
00877     double p1 = 0.000235487 ;
00878     double p2 = -8.26496e-06 ;
00879     double p3 = 1.21564e-07 ;
00880     double p4 = -4.83286e-10 ;
00881     
00882     double corr ;
00883     if (eta < 8) corr = p0 ;
00884     else corr = p0 + p1*eta + p2*eta*eta + p3*eta*eta*eta + p4*eta*eta*eta*eta;
00885     return corr/p0 ;
00886 }
00887 // -----------------------------------------------------------------
00888 
00889 double etaCorrE1E9 (int eta)
00890 {
00891     if (eta < 4) return 1.0 ;
00892     // grazie Paolo
00893     double p0 = 0.834629 ;
00894     double p1 = 0.00015254 ;
00895     double p2 = -4.91784e-06 ;
00896     double p3 = 6.54652e-08 ;
00897     double p4 = -2.4894e-10 ;
00898     
00899     double corr ;
00900     if (eta < 6) corr = p0 ;
00901     else corr = p0 + p1*eta + p2*eta*eta + p3*eta*eta*eta + p4*eta*eta*eta*eta;
00902     return corr/p0 ;
00903 }
00904