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;
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("");
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("");
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("");
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("");
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("");
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("");
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("");
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
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
00151 float dac=BegDac+StepDac*indDac;
00152 if(vecDac.size() <= indDac) vecDac.push_back(dac);
00153
00154
00155
00156
00157
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
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 }
00200 }
00201 }
00202
00204
00205 if(m_wire_ev.find(idlayer) == m_wire_ev.end())
00206 m_wire_ev[idlayer]=wireplane;
00207
00208 }
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;
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
00225
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 }
00231 }
00232 indDac++;
00233 if(dac==(float)EndDac) indDac=0;
00234 }
00235
00236
00237 void CSCAFEBThrAnalysis::done() {
00238
00239 float x,y;
00240
00241
00242 CSCobject *cn = new CSCobject();
00243 condbon *dbon = new condbon();
00244
00245 std::map<int, std::vector<std::vector<int> > >::iterator mwiredacIt;
00246 std::map<int, std::vector<std::vector<float> > >::iterator mresfordbIt;
00247 std::vector<int>::iterator vecIt;
00248
00249 std::cout<<"Events analyzed "<<nmbev<<std::endl;
00250 std::cout<<"Events no anodes "<<nmbev_no_wire<<std::endl<<std::endl;
00251
00252 std::cout<<"DAC occupancy"<<std::endl;
00253 int ndacsize=EndDac-BegDac+1;
00254 for(int i=0;i<ndacsize;i++) std::cout
00255 <<" "<<vecDacOccup[i];
00256 std::cout<<"\n\n"<<std::endl;
00257
00258 std::vector<float> inputx;
00259 std::vector<float> inputy;
00260
00261 std::vector<float> mypar(2, 0.0);
00262 std::vector<float> ermypar(2, 0.0);
00263 float ercorr, chisq, edm;
00264 int ndf,niter;
00265
00266 int ch, afeb, idchmb;
00267
00268 CSCFitAFEBThr * fitAnodeThr;
00269
00270 std::vector<float> fitres(4,0);
00271
00272
00273
00274 for(mwiredacIt=m_wire_dac.begin();mwiredacIt!=m_wire_dac.end();++mwiredacIt){
00275 int idwiredac=(*mwiredacIt).first;
00276
00277 int layer=idwiredac-(idwiredac/10)*10;
00278 idchmb=idwiredac/10;
00279
00280 for(int unsigned iwire=0; iwire<mwiredacIt->second.size();iwire++) {
00281
00282 afeb=csctoafeb.getAfebPos(layer,iwire+1);
00283 ch=csctoafeb.getAfebCh(layer,iwire+1);
00284 int afebid=(idwiredac/10)*100+csctoafeb.getAfebPos(layer,iwire+1);
00285
00286 indDac=0;
00287 for(vecIt=mwiredacIt->second[iwire].begin();
00288 vecIt!=mwiredacIt->second[iwire].end(); ++vecIt) {
00289
00290 x=vecDac[indDac]; y=*vecIt;
00291 hf2ForId(mh_AfebDac, 200, afebid,x, y,1.0);
00292
00294 if(indDac==0) {
00295 x=(afeb-1)*16+ch;
00296 y=*vecIt;
00297 hf1ForId(mh_ChanEff, 101, idchmb, x, y);
00298 }
00299
00300 inputx.push_back(x);
00301 inputy.push_back(y);
00302
00303 indDac++;
00304 }
00305
00306
00307
00308
00309
00310
00311
00312
00313 for(unsigned int i=0;i<2;i++) {mypar[i]=0.0; ermypar[i]=0.0;}
00314 ercorr=0.0;
00315 chisq=0.0;
00316 ndf=0;
00317 niter=0;
00318 edm=0.0;
00319
00321 fitAnodeThr=new CSCFitAFEBThr();
00322
00323 fitAnodeThr->ThresholdNoise(inputx,inputy,npulses,vecDacOccup,mypar,ermypar,ercorr,chisq,ndf,niter,edm);
00324 delete fitAnodeThr;
00325
00326
00327
00328
00330
00331 x=(afeb-1)*16+ch;
00332
00334 y=mypar[0];
00335 hf2ForId(mh_AfebThrPar, 300, idchmb,x, y, 1.0);
00336
00338 y=mypar[1];
00339 hf2ForId(mh_AfebNoisePar, 400, idchmb,x, y, 1.0);
00340
00342 y=ndf;
00343 hf2ForId(mh_AfebNDF, 500, idchmb,x, y, 1.0);
00344
00346 y=0.0;
00347 if(ndf>0) y=chisq/(float)ndf;
00348 hf2ForId(mh_AfebChi2perNDF, 600, idchmb,x, y, 1.0);
00349
00351 fitres[0]=mypar[0];
00352 fitres[1]=mypar[1];
00353 fitres[2]=ndf;
00354 fitres[3]=0.0;
00355 if(ndf>0) fitres[3]=chisq/(float)ndf;
00356
00358
00359 mresfordbIt=m_res_for_db.find(idwiredac);
00360 if(mresfordbIt==m_res_for_db.end())
00361 m_res_for_db[idwiredac].push_back(fitres);
00362 else m_res_for_db[idwiredac].push_back(fitres);
00363
00364 inputx.clear();
00365 inputy.clear();
00366
00367 }
00368 }
00369
00370 std::cout<<"Size of map for DB "<<m_res_for_db.size()<<std::endl;
00371
00372 std::cout<<"The following CSCs will go to DB"<<std::endl<<std::endl;
00373
00374 for(mresfordbIt=m_res_for_db.begin(); mresfordbIt!=m_res_for_db.end();
00375 ++mresfordbIt) {
00376 int idlayer=(*mresfordbIt).first;
00377 idchmb=idlayer/10;
00378 int layer=idlayer-idchmb*10;
00379 std::cout<<"CSC "<<idchmb<<" Layer "<<layer<<" "
00380 <<(*mresfordbIt).second.size()<<std::endl;
00381 }
00382
00383 for(mresfordbIt=m_res_for_db.begin(); mresfordbIt!=m_res_for_db.end();
00384 ++mresfordbIt) {
00385 int idlayer=(*mresfordbIt).first;
00386
00387
00388 int size = (*mresfordbIt).second.size();
00389 cn->obj[idlayer].resize(size);
00390
00391 for (unsigned int i=0;i<(*mresfordbIt).second.size();i++) {
00392 std::cout<<idlayer<<" "<<i+1<<" ";
00393
00394 for(int j=0;j<4;j++)
00395 std::cout<< (*mresfordbIt).second[i][j]<<" ";
00396 std::cout<<std::endl;
00397
00398
00399 cn->obj[idlayer][i].resize(4);
00400 cn->obj[idlayer][i][0] = (*mresfordbIt).second[i][0];
00401 cn->obj[idlayer][i][1] = (*mresfordbIt).second[i][1];
00402 cn->obj[idlayer][i][2] = (*mresfordbIt).second[i][2];
00403 cn->obj[idlayer][i][3] = (*mresfordbIt).second[i][3];
00404 }
00405 }
00406
00407
00408
00409
00410
00411
00412 if(hist_file!=0) {
00413 hist_file->Write();
00414 delete hist_file;
00415 hist_file=0;
00416 std::cout << "Hist. file was closed\n";
00417 }
00418
00419 std::cout<<" End of CSCAFEBThrAnalysis"<<std::endl;
00420 }