CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_7_hltpatch1/src/CalibCalorimetry/EcalLaserAnalyzer/src/TShapeAnalysis.cc

Go to the documentation of this file.
00001 /* 
00002  *  \class TShapeAnalysis
00003  *
00004  *  $Date: 2012/02/09 10:08:10 $
00005  *  original author: Patrice Verrecchia 
00006  *   modified by Julie Malcles - CEA/Saclay
00007  */
00008 
00009 #include <CalibCalorimetry/EcalLaserAnalyzer/interface/TShapeAnalysis.h>
00010 #include <CalibCalorimetry/EcalLaserAnalyzer/interface/TFParams.h>
00011 
00012 #include <iostream>
00013 #include <math.h>
00014 #include <time.h>
00015 #include <cassert>
00016 
00017 #include <TFile.h>
00018 #include <TTree.h>
00019 #include <TBranch.h>
00020 
00021 //ClassImp(TShapeAnalysis)
00022 
00023 using namespace std;
00024 
00025 // Constructor...
00026 TShapeAnalysis::TShapeAnalysis(double alpha0, double beta0, double width0, double chi20)
00027 {
00028   init(alpha0, beta0, width0, chi20);
00029 }
00030 
00031 TShapeAnalysis::TShapeAnalysis(TTree *tAB, double alpha0, double beta0, double width0, double chi20)
00032 {
00033   init(tAB, alpha0, beta0, width0, chi20);
00034 }
00035 
00036 // Destructor
00037 TShapeAnalysis::~TShapeAnalysis()
00038 {
00039 }
00040 
00041 void TShapeAnalysis::init(double alpha0, double beta0, double width0, double chi20)
00042 {
00043   tABinit=NULL;
00044   nchsel=fNchsel;
00045   for(int cris=0;cris<fNchsel;cris++){
00046 
00047     index[cris]=-1;
00048     npass[cris]=0;
00049     npassok[cris]=0.;
00050     
00051     alpha_val[cris]=alpha0;
00052     beta_val[cris]=beta0;
00053     width_val[cris]=width0;
00054     chi2_val[cris]=chi20;
00055     flag_val[cris]=0;
00056     
00057     alpha_init[cris]=alpha0;
00058     beta_init[cris]=beta0;
00059     width_init[cris]=width0;
00060     chi2_init[cris]=chi20;
00061     flag_init[cris]=0;
00062 
00063     phi_init[cris]=0;
00064     eta_init[cris]=0;
00065     side_init[cris]=0;
00066     dcc_init[cris]=0;
00067     tower_init[cris]=0;
00068     ch_init[cris]=0;
00069     assignChannel(cris,cris);
00070     
00071   }
00072 }
00073 
00074 void TShapeAnalysis::init(TTree *tAB, double alpha0, double beta0, double width0, double chi20)
00075 {
00076   init( alpha0, beta0, width0, chi20 );
00077   
00078   tABinit=tAB->CloneTree();
00079 
00080   // Declaration of leaf types
00081   Int_t           sidei;
00082   Int_t           iphii;
00083   Int_t           ietai;
00084   Int_t           dccIDi;
00085   Int_t           towerIDi;
00086   Int_t           channelIDi;
00087   Double_t        alphai;
00088   Double_t        betai;
00089   Double_t        widthi;
00090   Double_t        chi2i;
00091   Int_t           flagi;
00092   
00093   // List of branches
00094   TBranch        *b_iphi;   
00095   TBranch        *b_ieta;   
00096   TBranch        *b_side;   
00097   TBranch        *b_dccID;   
00098   TBranch        *b_towerID;   
00099   TBranch        *b_channelID;   
00100   TBranch        *b_alpha;   
00101   TBranch        *b_beta;   
00102   TBranch        *b_width;   
00103   TBranch        *b_chi2;   
00104   TBranch        *b_flag;   
00105   
00106   if(tABinit){
00107 
00108     tABinit->SetBranchAddress("iphi", &iphii, &b_iphi);
00109     tABinit->SetBranchAddress("ieta", &ietai, &b_ieta);
00110     tABinit->SetBranchAddress("side", &sidei, &b_side);
00111     tABinit->SetBranchAddress("dccID", &dccIDi, &b_dccID);
00112     tABinit->SetBranchAddress("towerID", &towerIDi, &b_towerID);
00113     tABinit->SetBranchAddress("channelID", &channelIDi, &b_channelID);
00114     tABinit->SetBranchAddress("alpha", &alphai, &b_alpha);
00115     tABinit->SetBranchAddress("beta", &betai, &b_beta);
00116     tABinit->SetBranchAddress("width", &widthi, &b_width);
00117     tABinit->SetBranchAddress("chi2", &chi2i, &b_chi2);
00118     tABinit->SetBranchAddress("flag", &flagi, &b_flag);
00119     
00120     nchsel=tABinit->GetEntries();
00121     assert(nchsel<=fNchsel);
00122     
00123     for(int cris=0;cris<nchsel;cris++){
00124       
00125       tABinit->GetEntry(cris);
00126       
00127       //      std::cout<< "Loop 1 "<< cris<<" "<<alphai<< std::endl;
00128 
00129       putalphaVal(cris,alphai);
00130       putchi2Val(cris,chi2i);
00131       putbetaVal(cris,betai);
00132       putwidthVal(cris,widthi);
00133       putflagVal(cris,flagi);
00134       
00135       putalphaInit(cris,alphai);
00136       putchi2Init(cris,chi2i);
00137       putbetaInit(cris,betai);
00138       putwidthInit(cris,widthi);
00139       putflagInit(cris,flagi);
00140       putetaInit(cris,ietai);
00141       putphiInit(cris,iphii);
00142  
00143     }
00144   }
00145 }
00146 
00147 void TShapeAnalysis::set_const(int ns, int ns1, int ns2, int ps, int nevtmax, double noise_val, double chi2_cut)
00148 {
00149   nsamplecristal= ns;
00150   presample=ps;
00151   sampbmax= ns1;
00152   sampamax= ns2;
00153   nevt= nevtmax;
00154   noise= noise_val;
00155   chi2cut=chi2_cut;
00156 }
00157 
00158 
00159 void TShapeAnalysis::set_presample(int ps)
00160 {
00161   presample=ps;
00162 }
00163 void TShapeAnalysis::set_nch(int nch){
00164 
00165   assert (nch<=fNchsel);
00166   if(tABinit) assert(nch==nchsel);
00167   nchsel=nch;
00168 
00169 }
00170 void TShapeAnalysis::assignChannel(int n, int ch)
00171 {
00172     if(n >= nchsel)
00173          printf(" number of channels exceed maximum allowed\n");
00174 
00175     index[n]=ch;
00176 }
00177 
00178 void TShapeAnalysis::putDateStart(long int timecur)
00179 {
00180     timestart=timecur;
00181 }
00182 
00183 void TShapeAnalysis::putDateStop(long int timecur)
00184 {
00185     timestop=timecur;
00186 }
00187 
00188 void TShapeAnalysis::getDateStart()
00189 {
00190     time_t t,timecur;
00191     timecur= time(&t);
00192     timestart= ((long int) timecur);
00193 }
00194 
00195 void TShapeAnalysis::getDateStop()
00196 {
00197     time_t t,timecur;
00198     timecur= time(&t);
00199     timestop= ((long int) timecur);
00200 }
00201 
00202 void TShapeAnalysis::putAllVals(int ch, double* sampl, int ieta, int iphi, int dcc, int side, int tower, int chid)
00203 {
00204   dcc_init[ch]=dcc;
00205   tower_init[ch]=side;
00206   ch_init[ch]=chid;
00207   side_init[ch]=side;
00208   eta_init[ch]=ieta;
00209   phi_init[ch]=iphi;
00210   putAllVals(ch, sampl, ieta, iphi);
00211   
00212 }
00213 
00214 void TShapeAnalysis::putAllVals(int ch, double* sampl, int ieta, int iphi)
00215 {
00216 
00217   int i,k;
00218   int n=-1;
00219   for(i=0;i<nchsel;i++)
00220     if(index[i] == ch) n=i;
00221   
00222   if(n >= 0) {
00223     if(npass[n] < nevt) {
00224       
00225       for(k=0;k<nsamplecristal;k++) {
00226         rawsglu[n][npass[n]][k] = sampl[k];
00227       }
00228       
00229       npass[n]++;
00230     }
00231   } else {
00232     printf("no index found for ch=%d\n",ch);
00233   }
00234 }
00235 
00236 void TShapeAnalysis::computeShape(string namefile, TTree *tAB)
00237 {
00238 
00239   double tm_atmax[200];
00240   double parout[3];
00241 
00242   double chi2_all=0.;
00243 
00244   double **dbi ;
00245   dbi = new double *[200];
00246   for(int k=0;k<200;k++) dbi[k] = new double[2];
00247 
00248   double **signalu ;
00249   signalu = new double *[200];
00250   for(int k=0 ;k<200;k++) signalu[k] = new double[10];        
00251 
00252   TFParams *pjf = new TFParams() ;
00253 
00254   for(int i=0;i<nchsel;i++) {
00255 
00256     if(index[i] >= 0) {
00257       
00258       if(npass[i] <= 10) {
00259 
00260         putalphaVal(i, alpha_init[i]);
00261         putbetaVal(i, beta_init[i]);
00262         putwidthVal(i,width_init[i]);
00263         putchi2Val(i,chi2_init[i]);
00264         putflagVal(i,0);
00265         
00266       } else {
00267         
00268         pjf->set_const(nsamplecristal, sampbmax, sampamax, alpha_init[i], beta_init[i], npass[i]);
00269         
00270         for(int pass=0;pass<npass[i];pass++){
00271           
00272           double ped=0;
00273           for(int k=0;k<presample;k++) {
00274             ped+= rawsglu[i][pass][k];
00275           }
00276           ped/=double(presample);
00277           
00278           for(int k=0;k<nsamplecristal;k++) {
00279             signalu[pass][k]= rawsglu[i][pass][k]-ped;
00280           }
00281         }
00282         
00283         int debug=0;
00284         chi2_all= pjf->fitpj(signalu,&parout[0],dbi,noise, debug);
00285         
00286         if(parout[0]>=0.0 && parout[1]>=0.0 && chi2_all<=chi2cut && chi2_all>0.0){
00287           
00288           putalphaVal(i,parout[0]);
00289           putbetaVal(i,parout[1]);
00290           putchi2Val(i,chi2_all);
00291           putflagVal(i,1);
00292           
00293         }else{
00294           
00295           putalphaVal(i,alpha_init[i]);
00296           putbetaVal(i,beta_init[i]);
00297           putwidthVal(i,width_init[i]);
00298           putchi2Val(i,chi2_init[i]);
00299           putflagVal(i,0);
00300           
00301         } 
00302         
00303         for(int kj=0;kj<npass[i];kj++) { // last event wrong here
00304           tm_atmax[kj]= dbi[kj][1];
00305         }
00306         computetmaxVal(i,&tm_atmax[0]);
00307         
00308       }
00309     }
00310   }
00311   
00312   if(tAB) tABinit=tAB->CloneTree();
00313   
00314   // Declaration of leaf types
00315   Int_t           sidei;
00316   Int_t           iphii;
00317   Int_t           ietai;
00318   Int_t           dccIDi;
00319   Int_t           towerIDi;
00320   Int_t           channelIDi;
00321   Double_t        alphai;
00322   Double_t        betai;
00323   Double_t        widthi;
00324   Double_t        chi2i;
00325   Int_t           flagi;
00326   
00327   // List of branches
00328   TBranch        *b_iphi;   
00329   TBranch        *b_ieta;   
00330   TBranch        *b_side;   
00331   TBranch        *b_dccID;   
00332   TBranch        *b_towerID;   
00333   TBranch        *b_channelID;   
00334   TBranch        *b_alpha;   
00335   TBranch        *b_beta;   
00336   TBranch        *b_width;   
00337   TBranch        *b_chi2;   
00338   TBranch        *b_flag;   
00339   
00340 
00341   if(tABinit){
00342     tABinit->SetBranchAddress("iphi", &iphii, &b_iphi);
00343     tABinit->SetBranchAddress("ieta", &ietai, &b_ieta);
00344     tABinit->SetBranchAddress("side", &sidei, &b_side);
00345     tABinit->SetBranchAddress("dccID", &dccIDi, &b_dccID);
00346     tABinit->SetBranchAddress("towerID", &towerIDi, &b_towerID);
00347     tABinit->SetBranchAddress("channelID", &channelIDi, &b_channelID);
00348     tABinit->SetBranchAddress("alpha", &alphai, &b_alpha);
00349     tABinit->SetBranchAddress("beta", &betai, &b_beta);
00350     tABinit->SetBranchAddress("width", &widthi, &b_width);
00351     tABinit->SetBranchAddress("chi2", &chi2i, &b_chi2);
00352     tABinit->SetBranchAddress("flag", &flagi, &b_flag);
00353   }
00354 
00355   TFile *fABout = new TFile(namefile.c_str(), "RECREATE");
00356   tABout=new TTree("ABCol0","ABCol0");
00357   
00358   // Declaration of leaf types
00359   Int_t           side;
00360   Int_t           iphi;
00361   Int_t           ieta;
00362   Int_t           dccID;
00363   Int_t           towerID;
00364   Int_t           channelID;
00365   Double_t        alpha;
00366   Double_t        beta;
00367   Double_t        width;
00368   Double_t        chi2;
00369   Int_t           flag;
00370   
00371   tABout->Branch( "iphi",      &iphi,       "iphi/I"      );
00372   tABout->Branch( "ieta",      &ieta,       "ieta/I"      );
00373   tABout->Branch( "side",      &side,       "side/I"      );
00374   tABout->Branch( "dccID",     &dccID,      "dccID/I"     );
00375   tABout->Branch( "towerID",   &towerID,    "towerID/I"   );
00376   tABout->Branch( "channelID", &channelID,  "channelID/I" );
00377   tABout->Branch( "alpha",     &alpha,     "alpha/D"    );
00378   tABout->Branch( "beta",      &beta,      "beta/D"     );
00379   tABout->Branch( "width",     &width,     "width/D"    );
00380   tABout->Branch( "chi2",      &chi2,      "chi2/D"     );
00381   tABout->Branch( "flag",      &flag,      "flag/I"     );
00382   
00383   tABout->SetBranchAddress( "ieta",      &ieta       );  
00384   tABout->SetBranchAddress( "iphi",      &iphi       ); 
00385   tABout->SetBranchAddress( "side",      &side       );
00386   tABout->SetBranchAddress( "dccID",     &dccID      );
00387   tABout->SetBranchAddress( "towerID",   &towerID    );
00388   tABout->SetBranchAddress( "channelID", &channelID  );
00389   tABout->SetBranchAddress( "alpha",     &alpha     );
00390   tABout->SetBranchAddress( "beta",      &beta      );
00391   tABout->SetBranchAddress( "width",     &width     );
00392   tABout->SetBranchAddress( "chi2",      &chi2      );
00393   tABout->SetBranchAddress( "flag",      &flag      );
00394   
00395   for(int i=0;i<nchsel;i++) {
00396     
00397     if(tABinit){
00398       
00399       tABinit->GetEntry(i);
00400       iphi=iphii;
00401       ieta=ietai;
00402       side=sidei;
00403       dccID=dccIDi;
00404       towerID=towerIDi;
00405       channelID=channelIDi;
00406    
00407     }else{
00408       
00409       iphi=phi_init[i];
00410       ieta=eta_init[i];
00411       side=side_init[i];
00412       dccID=dcc_init[i];
00413       towerID=tower_init[i];
00414       channelID=ch_init[i];
00415       
00416     }
00417     
00418     alpha=alpha_val[i];
00419     beta=beta_val[i];
00420     width=width_val[i];
00421     chi2=chi2_val[i];
00422     flag=flag_val[i];
00423     
00424     tABout->Fill();
00425   }
00426   
00427   
00428   tABout->Write();
00429   fABout->Close();
00430 
00431   delete pjf;
00432 }
00433 
00434 void TShapeAnalysis::computetmaxVal(int i, double* tm_val)
00435 {
00436   double tm_mean=0; //double tm_sig=0;
00437 
00438         double tm=0.; double sigtm=0.;
00439         for(int k=0;k<npass[i]-1;k++) {
00440                 if(1. < tm_val[k] && tm_val[k] < 10.) {
00441                    npassok[i]++;
00442                    tm+= tm_val[k];
00443                    sigtm+= tm_val[k]*tm_val[k];
00444                 }
00445         }
00446         if(npassok[i] <= 0) {
00447               tm_mean=0.; //tm_sig=0.;
00448         } else {
00449               for(int k=0;k<npass[i]-1;k++) {
00450                     if(1. < tm_val[k] && tm_val[k] < 10.) {
00451                          double ss= (sigtm/npassok[i]-tm/npassok[i]*tm/npassok[i]);
00452                          if(ss < 0.) ss=0.;
00453                          //tm_sig=sqrt(ss);
00454                          tm_mean= tm/npassok[i];
00455                     }
00456               }
00457         }
00458         //printf("npassok[%d]=%f tm_mean=%f tm_sig=%f\n",i,npassok[i],tm_mean,tm_sig);
00459         putwidthVal(i,tm_mean);
00460         
00461 }
00462 
00463 void TShapeAnalysis::putalphaVal(int n, double val)
00464 {
00465     alpha_val[n]= val;
00466 }
00467 
00468 void TShapeAnalysis::putchi2Val(int n, double val)
00469 {
00470     chi2_val[n]= val;
00471 }
00472 void TShapeAnalysis::putbetaVal(int n, double val)
00473 {
00474     beta_val[n]= val;
00475 }
00476 
00477 void TShapeAnalysis::putwidthVal(int n, double val)
00478 {
00479     width_val[n]= val;
00480 }
00481 
00482 void TShapeAnalysis::putflagVal(int n, int val)
00483 {
00484     flag_val[n]= val;
00485 }
00486 
00487 void TShapeAnalysis::putalphaInit(int n, double val)
00488 {
00489     alpha_init[n]= val;
00490 }
00491 
00492 void TShapeAnalysis::putchi2Init(int n, double val)
00493 {
00494     chi2_init[n]= val;
00495 }
00496 void TShapeAnalysis::putbetaInit(int n, double val)
00497 {
00498     beta_init[n]= val;
00499 }
00500 
00501 void TShapeAnalysis::putwidthInit(int n, double val)
00502 {
00503     width_init[n]= val;
00504 }
00505 
00506 void TShapeAnalysis::putetaInit(int n, int val )
00507 {
00508     eta_init[n]= val;
00509 }
00510 
00511 void TShapeAnalysis::putphiInit(int n, int val)
00512 {
00513     phi_init[n]= val;
00514 }
00515 
00516 void TShapeAnalysis::putflagInit(int n, int val)
00517 {
00518     flag_init[n]= val;
00519 }
00520 std::vector<double> TShapeAnalysis::getVals(int n)
00521 {
00522 
00523   std::vector<double> v;
00524   
00525   v.push_back(alpha_val[n]);
00526   v.push_back(beta_val[n]);
00527   v.push_back(width_val[n]);
00528   v.push_back(chi2_val[n]);
00529   v.push_back(flag_val[n]);
00530   
00531   return v;
00532 }
00533 std::vector<double> TShapeAnalysis::getInitVals(int n)
00534 {
00535 
00536   std::vector<double> v;
00537   
00538   v.push_back(alpha_init[n]);
00539   v.push_back(beta_init[n]);
00540   v.push_back(width_init[n]);
00541   v.push_back(chi2_init[n]);
00542   v.push_back(flag_init[n]);
00543   
00544   return v;
00545 }
00546 
00547 void TShapeAnalysis::printshapeData(int gRunNumber)
00548 {
00549      FILE *fd;
00550      int nev;
00551      sprintf(filename,"runABW%d.pedestal",gRunNumber); 
00552      fd = fopen(filename, "w");                                
00553      if(fd == NULL) printf("Error while opening file : %s\n",filename);
00554 
00555      for(int i=0; i<nchsel;i++) {
00556         if(index[i] >= 0) {
00557           nev= (int) npassok[i];
00558           double trise= alpha_val[i]*beta_val[i];
00559           fprintf( fd, "%d %d 1 %ld %ld %f %f %f %f\n",
00560                 index[i],nev,timestart,timestop,alpha_val[i],beta_val[i],trise,width_val[i]);
00561         }
00562      }
00563      int iret=fclose(fd);
00564      printf(" Closing file : %d\n",iret);
00565 
00566 }
00567