CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/src/OnlineDB/CSCCondDB/src/CSCAFEBThrAnalysis.cc

Go to the documentation of this file.
00001 #include "OnlineDB/CSCCondDB/interface/CSCAFEBThrAnalysis.h"
00002 #include "OnlineDB/CSCCondDB/interface/CSCToAFEB.h"
00003 #include <OnlineDB/CSCCondDB/interface/CSCFitAFEBThr.h>
00004 #include "OnlineDB/CSCCondDB/interface/CSCOnlineDB.h"
00005 #include "CondFormats/CSCObjects/interface/CSCobject.h"
00006 #include "TMath.h"
00007 
00008 class CSCFitAFEBThr;
00009 
00010 CSCAFEBThrAnalysis::CSCAFEBThrAnalysis() {
00011 
00012 hist_file=0; // set to null
00013 
00014 nmbev=0;
00015 nmbev_no_wire=0;
00016 
00017 npulses=100;
00018 vecDac.clear();
00019 BegDac=1; 
00020 EndDac=29;
00021 EvDac=1; 
00022 StepDac=1;
00023 indDac=0;
00024 
00025 vecDacOccup.assign(150,0);
00026 m_wire_dac.clear();
00027 m_res_for_db.clear();
00028 mh_ChanEff.clear();
00029 mh_FirstTime.clear();
00030 mh_AfebDac.clear();
00031 
00032 }
00033 
00034 void CSCAFEBThrAnalysis::setup(const std::string& histoFileName) {
00036   hist_file=new TFile(histoFileName.c_str(),"RECREATE");
00037   hist_file->cd();
00038 }
00039  
00040 void CSCAFEBThrAnalysis::bookForId(int flag, const int& idint,
00041                                              const std::string& idstring ) {
00042   hist_file->cd();
00043 
00044   std::ostringstream ss;
00045 
00046   if(flag==100) {
00047   ss <<idint<<"_Anode_First_Time";
00048   mh_FirstTime[idint]=new TH2F(ss.str().c_str(),"",675,0.0,675.0,50,0.0,10.0);
00049   mh_FirstTime[idint]->GetXaxis()->SetTitle("(AFEB-1)*16+ch");
00050   mh_FirstTime[idint]->GetYaxis()->SetTitle("Anode First Time Bin");
00051   mh_FirstTime[idint]->SetOption("BOX");
00052   ss.str(""); // clear
00053   }
00054 
00055   if(flag==101) {
00056   ss <<idint<<"_Anode_Chan_Eff";
00057   mh_ChanEff[idint]=new TH1F(ss.str().c_str(),"",675,0.0,675.0);
00058   mh_ChanEff[idint]->GetXaxis()->SetTitle("(AFEB-1)*16+ch");
00059   mh_ChanEff[idint]->GetYaxis()->SetTitle("Entries");
00060   ss.str(""); // clear
00061   }
00062 
00063   if(flag==200) { 
00064   ss <<idint<<"_Anode_AfebDac";
00065   mh_AfebDac[idint]=new TH2F(ss.str().c_str(),"",75,0.0,75.0, 50,0.0,50.0);
00066   mh_AfebDac[idint]->GetXaxis()->SetTitle("Threshold DAC");
00067   mh_AfebDac[idint]->GetYaxis()->SetTitle("AFEB Channel Occupancy");
00068   mh_AfebDac[idint]->SetOption("COL");
00069   ss.str(""); // clear
00070   }
00071 
00072   if(flag==300) { 
00073   ss <<idint<<"_Anode_AfebThrPar";
00074   mh_AfebThrPar[idint]=new TH2F(ss.str().c_str(),"",700,0.0,700.0, 50,0.0,50.0);
00075   mh_AfebThrPar[idint]->GetXaxis()->SetTitle("(AFEB-1)*16+ch");
00076   mh_AfebThrPar[idint]->GetYaxis()->SetTitle("AFEB Channel Threshold (DAC)");
00077   mh_AfebThrPar[idint]->SetOption("BOX");
00078   ss.str(""); // clear
00079   }
00080 
00081   if(flag==400) { 
00082   ss <<idint<<"_Anode_AfebNoisePar";
00083   mh_AfebNoisePar[idint]=new TH2F(ss.str().c_str(),"",700,0.0,700.0, 50,0.0,5.0);
00084   mh_AfebNoisePar[idint]->GetXaxis()->SetTitle("(AFEB-1)*16+ch");
00085   mh_AfebNoisePar[idint]->GetYaxis()->SetTitle("AFEB Channel Noise (DAC)");
00086   mh_AfebNoisePar[idint]->SetOption("BOX");
00087   ss.str(""); // clear
00088   }
00089 
00090   if(flag==500) { 
00091   ss <<idint<<"_Anode_AfebNDF";
00092   mh_AfebNDF[idint]=new TH2F(ss.str().c_str(),"",700,0.0,700.0, 25,-5.0,20.0);
00093   mh_AfebNDF[idint]->GetXaxis()->SetTitle("(AFEB-1)*16+ch");
00094   mh_AfebNDF[idint]->GetYaxis()->SetTitle("AFEB Channel Fit NDF");
00095   mh_AfebNDF[idint]->SetOption("BOX");
00096   ss.str(""); // clear
00097   }
00098 
00099   if(flag==600) { 
00100   ss <<idint<<"_Anode_AfebChi2perNDF";
00101   mh_AfebChi2perNDF[idint]=new TH2F(ss.str().c_str(),"",700,0.0,700.0, 50,0.0,10.0);
00102   mh_AfebChi2perNDF[idint]->GetXaxis()->SetTitle("(AFEB-1)*16+ch");
00103   mh_AfebChi2perNDF[idint]->GetYaxis()->SetTitle("AFEB Channel Fit Chi2/NDF");
00104   mh_AfebChi2perNDF[idint]->SetOption("BOX");
00105   ss.str(""); // clear
00106   }
00107 }
00108 
00109 void CSCAFEBThrAnalysis::hf1ForId(std::map<int, TH1*>& mp, int flag, 
00110 const int& id, float& x, float w) {
00111 
00112   std::map<int,TH1*>::iterator h;
00113   h=mp.find(id);
00114   if (h==mp.end()) {
00115      bookForId(flag,id,"");
00116      h=mp.find(id);
00117   }
00118   h->second->Fill(x,w);
00119 }
00120 
00121 void CSCAFEBThrAnalysis::hf2ForId(std::map<int, TH2*>& mp, int flag,
00122 const int& id, float& x, float& y,  float w) {
00123                                                                                 
00124   std::map<int,TH2*>::iterator h;
00125   h=mp.find(id);
00126   if (h==mp.end()) {
00127      bookForId(flag,id,"");
00128      h=mp.find(id);
00129   }
00130   h->second->Fill(x,y,w);
00131 }
00132 
00133 
00134 /* Analyze the hits */
00135 void CSCAFEBThrAnalysis::analyze(const CSCWireDigiCollection& wirecltn) {
00136 
00137 std::ostringstream ss;
00138 std::map<int,std::vector<int> >::iterator intIt;
00139 std::map<int, std::vector<std::vector<int> > >::iterator wiredacIt;
00140 
00141 std::vector<int> vec; 
00142 int afeb,ch;
00143 float x,y;
00144 
00145 m_wire_ev.clear();
00146 
00148 
00149 nmbev++;
00150 //indDac=(nmbev-1)/EvDac;
00151 float dac=BegDac+StepDac*indDac;
00152 if(vecDac.size() <= indDac) vecDac.push_back(dac);
00153 
00154 //std::cout<<"  Event "<<nmbev;
00155 //std::cout<<"  "<<indDac<<" "<<vecDac[indDac]<<std::endl;
00156 
00157 //Anode wires
00158 
00159   CSCWireDigiCollection::DigiRangeIterator wiredetUnitIt;
00160   if(wirecltn.begin() == wirecltn.end())  nmbev_no_wire++; 
00161 
00162   if(wirecltn.begin() !=  wirecltn.end()) {
00163 
00164   vecDacOccup[indDac]=vecDacOccup[indDac]+1;
00165 
00166   for (wiredetUnitIt=wirecltn.begin();
00167        wiredetUnitIt!=wirecltn.end();
00168        ++wiredetUnitIt){
00169 
00170     const CSCDetId& id = (*wiredetUnitIt).first;
00171 
00172     const int idchamber=id.endcap()*10000 +id.station()*1000+
00173                         id.ring()*100 +id.chamber();
00174     const int idlayer  =id.endcap()*100000+id.station()*10000+
00175                         id.ring()*1000+id.chamber()*10+id.layer();
00176 
00177     //    std::cout<<idchamber<<" "<<idlayer<<std::endl;
00178 
00179     const int maxwire=csctoafeb.getMaxWire(id.station(),id.ring());
00180     std::vector<int> wireplane(maxwire,0);
00181 
00182     const CSCWireDigiCollection::Range& range = (*wiredetUnitIt).second;
00183     for (CSCWireDigiCollection::const_iterator digiIt =
00184            range.first; digiIt!=range.second; ++digiIt){
00185 
00186       int iwire=(*digiIt).getWireGroup();
00187       if(iwire<=maxwire) {
00188         if(wireplane[iwire-1]==0) {
00189           wireplane[iwire-1]=(*digiIt).getBeamCrossingTag()+1;
00190           ch=csctoafeb.getAfebCh(id.layer(),(*digiIt).getWireGroup());
00191           afeb=csctoafeb.getAfebPos(id.layer(),(*digiIt).getWireGroup());
00192 
00194 
00195           x=(afeb-1)*16+ch;
00196           y=wireplane[iwire-1];
00197           hf2ForId(mh_FirstTime, 100, idchamber,x, y, 1.0);
00198 
00199         } // end if wireplane[iwire-1]==0
00200       }   // end if iwire<=csctoafeb.getMaxWire(id.station(),id.ring()
00201     }     // end of for digis in layer
00202 
00204 
00205     if(m_wire_ev.find(idlayer) == m_wire_ev.end()) 
00206       m_wire_ev[idlayer]=wireplane;
00207 
00208   }       // end of cycle on detUnit
00209 
00210 
00212 
00213   for(intIt=m_wire_ev.begin(); intIt!=m_wire_ev.end(); ++intIt) {
00214     const int idwirev=(*intIt).first;
00215     const std::vector<int> wiretemp=(*intIt).second; // What for?
00216     int nsize=EndDac-BegDac+1;
00217     std::vector<int> zer(nsize,0);
00218 
00219     wiredacIt=m_wire_dac.find(idwirev);
00220     if(wiredacIt==m_wire_dac.end()) {
00221       for(unsigned int j=0;j<wiretemp.size();j++)
00222          m_wire_dac[idwirev].push_back(zer);
00223       wiredacIt=m_wire_dac.find(idwirev);
00224 //      std::cout<<idwirev<<"  "<<wiredacIt->second.size()<<"  "<<
00225 //                 wiredacIt->second[0].size()<<std::endl;      
00226     }
00227     for(unsigned int i=0;i<(*intIt).second.size();i++)
00228       if((*intIt).second[i]>0) wiredacIt->second[i][indDac]=
00229                                wiredacIt->second[i][indDac]+1;          
00230   } // end of adding hits to the map of wire/DAC
00231   } // end of if wire collection not empty
00232     indDac++;
00233     if(dac==(float)EndDac) indDac=0;  
00234 }   // end of void CSCAFEBThrAnalysis
00235 
00236 
00237 void CSCAFEBThrAnalysis::done() {
00238 
00239   float x,y;
00240 
00241   //This is for DB transfer
00242   CSCobject *cn = new CSCobject();
00243   // Unused variable dbon caused compiler warning.
00244   //condbon *dbon = new condbon();
00245   new condbon(); 
00246   // Note:  cn and dbon pointers are never freed, I think. This leaks memory!!!
00247   
00248   std::map<int, std::vector<std::vector<int> > >::iterator mwiredacIt;
00249   std::map<int, std::vector<std::vector<float> > >::iterator mresfordbIt;
00250   std::vector<int>::iterator vecIt;
00251 
00252   std::cout<<"Events analyzed  "<<nmbev<<std::endl;
00253   std::cout<<"Events no anodes "<<nmbev_no_wire<<std::endl<<std::endl;
00254 
00255   std::cout<<"DAC occupancy"<<std::endl;
00256   size_t ndacsize=EndDac-BegDac+1;
00257   for(size_t i=0;i<ndacsize;i++) std::cout
00258                                        <<" "<<vecDacOccup[i];
00259   std::cout<<"\n\n"<<std::endl; 
00260 
00261   std::vector<float> inputx;
00262   std::vector<float> inputy;
00263 
00264   std::vector<float> mypar(2, 0.0);
00265   std::vector<float> ermypar(2, 0.0);
00266   float ercorr, chisq, edm;
00267   int ndf,niter;
00268  
00269   int ch, afeb, idchmb;
00270 
00271   CSCFitAFEBThr * fitAnodeThr;
00272 
00273   std::vector<float> fitres(4,0);
00274 
00275 //  std::cout<<"Threshold curves:\n"<<std::endl;
00276 
00277   for(mwiredacIt=m_wire_dac.begin();mwiredacIt!=m_wire_dac.end();++mwiredacIt){
00278     int idwiredac=(*mwiredacIt).first;
00279 
00280     int layer=idwiredac-(idwiredac/10)*10;   
00281     idchmb=idwiredac/10;
00282 
00283     for(int unsigned iwire=0; iwire<mwiredacIt->second.size();iwire++) {
00284 
00285        afeb=csctoafeb.getAfebPos(layer,iwire+1);
00286        ch=csctoafeb.getAfebCh(layer,iwire+1);
00287        int afebid=(idwiredac/10)*100+csctoafeb.getAfebPos(layer,iwire+1);
00288 
00289        indDac=0;
00290        for(vecIt=mwiredacIt->second[iwire].begin();
00291           vecIt!=mwiredacIt->second[iwire].end(); ++vecIt) {
00292 
00293           x=vecDac[indDac]; y=*vecIt; 
00294           hf2ForId(mh_AfebDac, 200, afebid,x, y,1.0);
00295 
00297           if(indDac==0) {
00298              x=(afeb-1)*16+ch;
00299              y=*vecIt;
00300              hf1ForId(mh_ChanEff, 101, idchmb, x, y);
00301           }
00302 
00303           inputx.push_back(x);
00304           inputy.push_back(y);
00305 
00306           indDac++;
00307        }    
00308        // end of DAC cycle to form vectors of input data (inputx,inputy)for fit
00309  
00310 //           std::cout<<afebid<<" "<<ch<<std::endl;
00311 //           for(unsigned int i=0;i<inputx.size();i++)
00312 //              std::cout<<" "<<inputy[i];
00313 //           std::cout<<std::endl;
00314 
00315 
00316   for(unsigned int i=0;i<2;i++) {mypar[i]=0.0; ermypar[i]=0.0;}
00317   ercorr=0.0; 
00318   chisq=0.0; 
00319   ndf=0; 
00320   niter=0; 
00321   edm=0.0;
00322 
00324   fitAnodeThr=new CSCFitAFEBThr();
00325   
00326 fitAnodeThr->ThresholdNoise(inputx,inputy,npulses,vecDacOccup,mypar,ermypar,ercorr,chisq,ndf,niter,edm);
00327   delete fitAnodeThr;
00328 
00329 //  std::cout<<"Fit "<<mypar[0]<<" "<<mypar[1]<<" "<<ndf<<" "<<chisq
00330 //           <<std::endl<<std::endl;
00331 
00333 
00334     x=(afeb-1)*16+ch;
00335 
00337     y=mypar[0];
00338     hf2ForId(mh_AfebThrPar, 300, idchmb,x, y, 1.0);
00339 
00341     y=mypar[1];
00342     hf2ForId(mh_AfebNoisePar, 400, idchmb,x, y, 1.0);
00343 
00345     y=ndf;
00346     hf2ForId(mh_AfebNDF, 500, idchmb,x, y, 1.0);
00347 
00349     y=0.0;
00350     if(ndf>0) y=chisq/(float)ndf;
00351     hf2ForId(mh_AfebChi2perNDF, 600, idchmb,x, y, 1.0);
00352 
00354     fitres[0]=mypar[0];
00355     fitres[1]=mypar[1];
00356     fitres[2]=ndf;
00357     fitres[3]=0.0;
00358     if(ndf>0) fitres[3]=chisq/(float)ndf;
00359     
00361     
00362     mresfordbIt=m_res_for_db.find(idwiredac);
00363     if(mresfordbIt==m_res_for_db.end())
00364       m_res_for_db[idwiredac].push_back(fitres);
00365     else m_res_for_db[idwiredac].push_back(fitres);
00366 
00367     inputx.clear();
00368     inputy.clear();
00369 
00370 } // end for(int iwire=0)
00371 }               // end of iteration thru m_wire_dac map
00372   
00373   std::cout<<"Size of map for DB "<<m_res_for_db.size()<<std::endl;
00374   
00375   std::cout<<"The following CSCs will go to DB"<<std::endl<<std::endl;
00376 
00377   for(mresfordbIt=m_res_for_db.begin(); mresfordbIt!=m_res_for_db.end(); 
00378       ++mresfordbIt) {
00379       int idlayer=(*mresfordbIt).first;
00380       idchmb=idlayer/10;
00381       int layer=idlayer-idchmb*10;
00382       std::cout<<"CSC "<<idchmb<<"  Layer "<<layer<<"  "
00383                <<(*mresfordbIt).second.size()<<std::endl;          
00384   }
00385   
00386   for(mresfordbIt=m_res_for_db.begin(); mresfordbIt!=m_res_for_db.end(); 
00387       ++mresfordbIt) {
00388       int idlayer=(*mresfordbIt).first;
00389       
00390       //This is for DB transfer
00391       int size = (*mresfordbIt).second.size();
00392       cn->obj[idlayer].resize(size);
00393       
00394       for (unsigned int i=0;i<(*mresfordbIt).second.size();i++) { 
00395         std::cout<<idlayer<<" "<<i+1<<"    ";
00396         
00397         for(int j=0;j<4;j++)
00398           std::cout<< (*mresfordbIt).second[i][j]<<" ";
00399         std::cout<<std::endl;
00400         
00401         //This is for DB transfer
00402         cn->obj[idlayer][i].resize(4);
00403         cn->obj[idlayer][i][0] = (*mresfordbIt).second[i][0];
00404         cn->obj[idlayer][i][1] = (*mresfordbIt).second[i][1];
00405         cn->obj[idlayer][i][2] = (*mresfordbIt).second[i][2];
00406         cn->obj[idlayer][i][3] = (*mresfordbIt).second[i][3];
00407       }
00408   }
00409   
00410   //send data to DB
00411   //dbon->cdbon_last_run("afeb_thresholds",&run);
00412   //std::cout<<"Last AFEB thresholds run "<<run<<" for run file "<<myname<<" saved "<<myTime<<std::endl;
00413   //if(debug) dbon->cdbon_write(cn,"afeb_thresholds",run+1,myTime);
00414   
00415   if(hist_file!=0) { // if there was a histogram file...
00416     hist_file->Write(); // write out the histrograms
00417     delete hist_file; // close and delete the file
00418     hist_file=0; // set to zero to clean up
00419     std::cout << "Hist. file was closed\n";
00420   }
00421 
00422   std::cout<<" End of CSCAFEBThrAnalysis"<<std::endl;  
00423 }