--- DarthVader/CalorimeterLevel2/src/CaloLevel0.cpp 2008/02/07 20:02:09 1.16 +++ DarthVader/CalorimeterLevel2/src/CaloLevel0.cpp 2008/05/12 14:36:08 1.21 @@ -95,6 +95,14 @@ /** * Initialize CaloLevel0 object **/ +void CaloLevel0::ProcessingInit(TSQLServer *dbc, UInt_t hs, Int_t &sgnl, TTree *l0tree, Bool_t isdeb, Bool_t isverb){ + if ( !dbc->IsConnected() ) throw -116; + this->InitDo(dbc,hs,sgnl,l0tree,isdeb,isverb); +} + +/** + * Initialize CaloLevel0 object +**/ void CaloLevel0::ProcessingInit(GL_TABLES *glt, UInt_t hs, Int_t &sgnl, TTree *l0tree, Bool_t isdeb, Bool_t isverb){ // const TString host = glt->CGetHost(); @@ -102,6 +110,13 @@ const TString psw = glt->CGetPsw(); TSQLServer *dbc = TSQLServer::Connect(host.Data(),user.Data(),psw.Data()); if ( !dbc->IsConnected() ) throw -116; + this->InitDo(dbc,hs,sgnl,l0tree,isdeb,isverb); + dbc->Close(); + delete dbc; +} + + +void CaloLevel0::InitDo(TSQLServer *dbc, UInt_t hs, Int_t &sgnl, TTree *l0tree, Bool_t isdeb, Bool_t isverb){ stringstream myquery; myquery.str(""); myquery << "SET time_zone='+0:00'"; @@ -182,8 +197,6 @@ // delete glcalo; delete glroot; - dbc->Close(); - delete dbc; // return; // @@ -200,6 +213,11 @@ return(sgnl); } +Int_t CaloLevel0::ChkParam(TSQLServer *dbc, UInt_t runheader, Bool_t mechal){ + Int_t sig = this->ChkParamDo(dbc,runheader,mechal); + return(sig); +} + Int_t CaloLevel0::ChkParam(GL_TABLES *glt, UInt_t runheader, Bool_t mechal){ const TString host = glt->CGetHost(); const TString user = glt->CGetUser(); @@ -211,6 +229,14 @@ myquery << "SET time_zone='+0:00'"; dbc->Query(myquery.str().c_str()); // + Int_t sig = this->ChkParamDo(dbc,runheader,mechal); + dbc->Close(); + delete dbc; + return(sig); +} + +Int_t CaloLevel0::ChkParamDo(TSQLServer *dbc, UInt_t runheader, Bool_t mechal){ + // stringstream calfile; stringstream bmfile; stringstream aligfile; @@ -221,8 +247,6 @@ // if ( calopar1 || ( ttcalopar1 != 0 && ttcalopar1 < runheader ) ){ // - // - // if ( debug ) printf(" calopar1 %i ftcalopar1 %u ttcalopar1 %u runheader %u \n",calopar1,ftcalopar1,ttcalopar1,runheader); // calopar1 = false; @@ -260,6 +284,7 @@ }; // if ( calopar2 || ( ttcalopar2 != 0 && ttcalopar2 < runheader ) ){ + // if ( debug ) printf(" calopar2 %i ftcalopar2 %u ttcalopar2 %u runheader %u \n",calopar2,ftcalopar2,ttcalopar2,runheader); calopar2 = false; // @@ -367,16 +392,16 @@ }; // delete glparam; - dbc->Close(); - delete dbc; // return(0); } +Int_t CaloLevel0::CalcCrossTalkCorr(TSQLServer *dbc, UInt_t runheader){ + Int_t sig = CalcCrossTalkCorrDo(dbc,runheader); + return(sig); +} + Int_t CaloLevel0::CalcCrossTalkCorr(GL_TABLES *glt, UInt_t runheader){ - // - if ( ctground ) return(0); - // const TString host = glt->CGetHost(); const TString user = glt->CGetUser(); const TString psw = glt->CGetPsw(); @@ -387,6 +412,18 @@ myquery << "SET time_zone='+0:00'"; dbc->Query(myquery.str().c_str()); // + Int_t sig = CalcCrossTalkCorrDo(dbc,runheader); + dbc->Close(); + delete dbc; + // + return(sig); + // +} + +Int_t CaloLevel0::CalcCrossTalkCorrDo(TSQLServer *dbc, UInt_t runheader){ + // + if ( ctground ) return(0); + // stringstream bmfile; Int_t error = 0; ifstream badfile; @@ -614,8 +651,6 @@ // // delete glp; - dbc->Close(); - delete dbc; // // Ok, now we can try to calculate the cross-talk correction for each pre-amplifier // @@ -729,63 +764,118 @@ } void CaloLevel0::FindBaseCompress(Int_t l, Int_t m, Int_t pre){ + Int_t n = 0; + Float_t q = 0; + this->FindBaseCompress(l,m,pre,n,q); +} + +void CaloLevel0::FindBaseCompress(Int_t l, Int_t m, Int_t pre, Int_t &nst, Float_t &qp){ for (Int_t e = pre*16; e < (pre+1)*16 ; e++){ dexy[l][m][e] = dexyc[l][m][e]; }; - this->FindBaseRaw(l,m,pre); + this->FindBaseRaw(l,m,pre,nst,qp); } void CaloLevel0::FindBaseRaw(Int_t l, Int_t m, Int_t pre){ - Float_t minstrip = 100000.; - Float_t rms = 0.; + Int_t n = 0; + Float_t q = 0; + this->FindBaseRaw(l,m,pre,n,q); +} + +void CaloLevel0::FindBaseRaw(Int_t l, Int_t m, Int_t pre, Int_t &nst, Float_t &qp){ + // + Float_t minstrip = 100000.; + Float_t rms = 0.; + Int_t process = 0; + Int_t onlmask[16]; + memset(onlmask, 0, 16*sizeof(Int_t)); + // + while ( process < 2 ){ + // + minstrip = 100000.; + rms = 0.; base[l][m][pre] = 0.; + qp = 0.; + // + Int_t spos = -1; + Int_t ee = 0; for (Int_t e = pre*16; e < (pre+1)*16 ; e++){ - if ( calgood[l][m][e] == 0. && obadmask[l][m][e] == 0 && dexy[l][m][e]-calped[l][m][e] < minstrip && dexy[l][m][e] > 0.) { - minstrip = dexy[l][m][e]-calped[l][m][e]; - rms = calthr[l][m][pre]; - }; + if ( calgood[l][m][e] == 0. && obadmask[l][m][e] == 0 && dexy[l][m][e]-calped[l][m][e] < minstrip && dexy[l][m][e] > 0. && onlmask[ee] == 0 ) { + minstrip = dexy[l][m][e]-calped[l][m][e]; + rms = calthr[l][m][pre]; + spos = ee; + }; + ee++; + qp += (dexy[l][m][e]-calped[l][m][e]-sbase[l][m][e]); }; - if ( debug && l==1 ){ - printf("\n BASELINE CALCULATION for view %i pl %i pre %i: \n => minstrip %f rms %f \n",l,m,pre,minstrip,rms); + // + if ( debug && l==0 ){ + printf("\n BASELINE CALCULATION for view %i pl %i pre %i: \n => minstrip %f rms %f \n => qp = %f \n",l,m,pre,minstrip,rms,qp); }; if ( minstrip != 100000. ) { - Float_t strip6s = 0.; - for (Int_t e = pre*16; e < (pre+1)*16 ; e++){ - if ( (dexy[l][m][e]-calped[l][m][e]) >= minstrip && (dexy[l][m][e]-calped[l][m][e]) <= (minstrip+rms) ) { - strip6s += 1.; - base[l][m][pre] += (dexy[l][m][e] - calped[l][m][e]); - }; - // - // compression - // - if ( abs((int)(dexy[l][m][e]-calped[l][m][e])) <= (minstrip+rms) ) { - dexyc[l][m][e] = 0.; - } else { - dexyc[l][m][e] = dexy[l][m][e]; - }; - }; - if ( debug && l==1 ){ - printf(" strip6s %f \n",strip6s); + Float_t strip6s = 0.; + for (Int_t e = pre*16; e < (pre+1)*16 ; e++){ + if ( (dexy[l][m][e]-calped[l][m][e]) >= minstrip && (dexy[l][m][e]-calped[l][m][e]) <= (minstrip+rms) ) { + strip6s += 1.; + base[l][m][pre] += (dexy[l][m][e] - calped[l][m][e]); }; - // if ( strip6s >= 9. ){ - if ( strip6s >= 2. ){ - Double_t arro = base[l][m][pre]/strip6s; - Float_t deci = 1000.*((float)arro - float(int(arro))); - if ( deci < 500. ) { - arro = double(int(arro)); - } else { - arro = 1. + double(int(arro)); - }; - base[l][m][pre] = arro; + // + // compression + // + // if ( abs((int)(dexy[l][m][e]-calped[l][m][e])) <= (minstrip+rms) ) { + // dexyc[l][m][e] = 0.; + // } else { + dexyc[l][m][e] = dexy[l][m][e]; + // }; + }; + // + if ( strip6s == 1. && process < 1 ){ + onlmask[spos] = 1; + process++; + continue; + }; + process += 2; + nst = (Int_t)strip6s; + // + if ( debug && l==1 ){ + printf(" strip6s %f \n",strip6s); + }; + // if ( strip6s >= 9. ){ + if ( (strip6s >= 2. && process == 2) || (strip6s >= 9. && process > 2) ){ + Double_t arro = base[l][m][pre]/strip6s; + Float_t deci = 1000.*((float)arro - float(int(arro))); + if ( deci < 500. ) { + arro = double(int(arro)); } else { - base[l][m][pre] = 31000.; - for (Int_t e = pre*16; e < (pre+1)*16 ; e++){ - dexyc[l][m][e] = dexy[l][m][e]; - }; + arro = 1. + double(int(arro)); }; - } else { + base[l][m][pre] = arro; + // + // if too few strips were used to determine the baseline check if it is comparable with the previous event, if not mark it as bad + // + if ( debug && l==1 ) printf(" Calculated baseline: base %f sbase*1.02 %f \n",base[l][m][pre],1.02*sbase[l][m][pre]); + // + if ( strip6s < 4 && base[l][m][pre] > 1.02*sbase[l][m][pre] && sbase[l][m][pre] > 0. ){ + if ( debug ) printf(" Suspicious calculated baseline: base %f sbase*1.02 %f strip6s %i \n",base[l][m][pre],1.02*sbase[l][m][pre],(Int_t)strip6s); + base[l][m][pre] = 31000.; + for (Int_t e = pre*16; e < (pre+1)*16 ; e++){ + dexyc[l][m][e] = dexy[l][m][e]; + }; + }; + } else { base[l][m][pre] = 31000.; + for (Int_t e = pre*16; e < (pre+1)*16 ; e++){ + dexyc[l][m][e] = dexy[l][m][e]; + }; + }; + } else { + process += 2; + base[l][m][pre] = 31000.; + for (Int_t e = pre*16; e < (pre+1)*16 ; e++){ + dexyc[l][m][e] = dexy[l][m][e]; + }; }; + }; } Int_t CaloLevel0::Calibrate(Int_t ei){ @@ -843,9 +933,10 @@ Float_t ener; Int_t doneb = 0; Int_t donec = 0; - Int_t ck = 0; + Int_t ck[6] = {0,0,0,0,0,0}; Int_t ipre = 0; - Int_t ip[3] = {0}; + // Int_t ip[3] = {0}; + Int_t ip[3] = {0,0,0}; Float_t base0, base1, base2; base0 = 0.; base1 = 0.; @@ -921,28 +1012,52 @@ // pre = -1; cbase0 = 0.; + Int_t nstt[2]; + Float_t rqp[2]; for (Int_t i = 0; i < 3; i++){ + // nstt[0] = 0; // BUG + // nstt[1] = 0; // BUG + nstt[0] = 1000; + nstt[1] = 1000; + rqp[0] = 0.; + rqp[1] = 0.; for (Int_t j = 0; j < 2; j++){ pre = j + i*2; // // baseline check and calculation // if ( !isRAW ){ + // + // if it is a compress event with fully transmitted pre try to calculate the baseline + // if ( de->base[l][m][pre] != 0. && de->base[l][m][pre]<31000. ) { base[l][m][pre] = de->base[l][m][pre] ; } else { - FindBaseCompress(l,m,pre); + FindBaseCompress(l,m,pre,nstt[j],rqp[j]); }; cbase0 += base[l][m][pre]; } else { // - // if it is a raw event and we haven't checked - // yet, calculate the baseline. + // if it is a raw event calculate the baseline. // - FindBaseRaw(l,m,pre); + FindBaseRaw(l,m,pre,nstt[j],rqp[j]); cbase0 += base[l][m][pre]; }; }; + // + // if we are able to calculate the baseline with more than 3 strips on one pre and not in the other one choose the pre with more calculated strips + // + if ( nstt[0] < 4 && nstt[1] >= 4 && nstt[0] != 1000 && nstt[1] != 1000 ) base[l][m][pre-1] = 31000.; + if ( nstt[0] >= 4 && nstt[1] < 4 && nstt[0] != 1000 && nstt[1] != 1000 ) base[l][m][pre] = 31000.; + // if ( nstt[0] < 4 && nstt[1] >= 4 ) base[l][m][pre-1] = 31000.; // BUG + // if ( nstt[0] >= 4 && nstt[1] < 4 ) base[l][m][pre] = 31000.; // BUG +// // +// // if we are NOT able to calculate the baseline with more than 3 strips on both pres take the baseline (if any) of the one which has less energy +// // +// if ( nstt[0] < 4 && nstt[1] < 4 ){ +// if ( rqp[0] >= rqp[1] ) base[l][m][pre-1] = 31000.; +// if ( rqp[0] < rqp[1] ) base[l][m][pre] = 31000.; +// }; }; // // run over strips @@ -953,11 +1068,11 @@ ip[i] = 0; for (Int_t n = i*32 ; n < (i+1)*32 ; n++){ if (n%16 == 0) { - ck = 0; done = 0; doneb = 0; donec = 0; pre++; + ck[pre] = 0; qpre[pre] = 0.; }; // @@ -967,7 +1082,7 @@ // if ( !done ){ if ( (base[l][m][pre] == 31000. || base[l][m][pre] == 0.) ){ - ck = 1; + ck[pre] = 1; if (pre%2 == 0) { ip[i] = pre + 1; } else { @@ -975,20 +1090,21 @@ }; if ( (base[l][m][ip[i]] == 31000. || base[l][m][ip[i]] == 0. || !crosst ) ){ // - ck = 2; + ck[pre] = 2; if ( sbase[l][m][pre] == 31000. || sbase[l][m][pre] == 0. ) { - ck = 3; + ck[pre] = 3; }; }; - done = 1; + // done = 1; }; + done = 1; }; // // CALIBRATION ALGORITHM // if ( !doneb ){ - if ( debug ) printf(" ck is %i \n",ck); - switch (ck) { + if ( debug ) printf(" ck[pre] is %i \n",ck[pre]); + switch (ck[pre]) { case 0: base0 = base[l][m][pre]; base2 = calbase[l][m][pre]; @@ -1035,23 +1151,31 @@ }; }; if ( crosst ){ - if (ck == 1){ - if (ip[i]%2 == 0) { - ipre = ip[i] + 1; + if (ck[pre] == 1 || ck[pre-1] == 1){ + if (ck[pre] == 1){ + ipre = pre; + ip[i] = pre - 1; } else { - ipre = ip[i] - 1; + ipre = pre - 1; + ip[i] = pre; }; + // if (ip[i]%2 == 0) { + // ipre = ip[i] + 1; + // } else { + // ipre = ip[i] - 1; + // }; for (Int_t j = ipre*16 ; j < (ipre+1)*16 ; j++){ if ( !ctground ){ - clevel1->estrip[j][m][l] += (qpre[ipre] - qpre[ip[i]]) * ctprecor[l][m][ipre]; + clevel1->estrip[j][m][l] += qpre[ipre] * ctprecor[l][m][ipre] - qpre[ip[i]] * ctprecor[l][m][ip[i]]; } else { clevel1->estrip[j][m][l] += (qpre[ipre] - qpre[ip[i]]) * 0.00478; }; }; }; - if (ck == 2){ + if (ck[pre] == 2 && ck[pre-1] == 2){ for (Int_t j = i*32 ; j < (i+1)*32 ; j++){ - ipre = j/16 + 1; + // ipre = j/16 + 1; + ipre = j/16 ; if ( !ctground ){ clevel1->estrip[j][m][l] += qpre[ipre] * ctprecor[l][m][ipre]; } else {