CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/src/CalibCalorimetry/CastorCalib/plugins/CastorPedestalsAnalysis.cc

Go to the documentation of this file.
00001 // Original Author:  Steven Won
00002 // Original Author:  Alan Campbell for castor
00003 //         Created:  Fri May  2 15:34:43 CEST 2008
00004 // Written to replace the combination of HcalPedestalAnalyzer and HcalPedestalAnalysis 
00005 // This code runs 1000x faster and produces all outputs from a single run
00006 // (ADC, fC in .txt plus an .xml file)
00007 //
00008 #include <memory>
00009 #include "CalibCalorimetry/CastorCalib/interface/CastorPedestalsAnalysis.h"
00010 
00011 CastorPedestalsAnalysis::CastorPedestalsAnalysis(const edm::ParameterSet& ps)
00012 {
00013    hiSaveFlag = ps.getUntrackedParameter<bool>("hiSaveFlag", false);
00014    dumpXML = ps.getUntrackedParameter<bool>("dumpXML", false);
00015    verboseflag = ps.getUntrackedParameter<bool>("verbose", false);
00016    firstTS = ps.getUntrackedParameter<int>("firstTS", 0);
00017    lastTS = ps.getUntrackedParameter<int>("lastTS", 9);   
00018    firsttime = true;
00019 }
00020 
00021 
00022 CastorPedestalsAnalysis::~CastorPedestalsAnalysis()
00023 {
00024    CastorPedestals* rawPedsItem = new CastorPedestals(true);
00025    CastorPedestalWidths* rawWidthsItem = new CastorPedestalWidths(true);
00026    CastorPedestals* rawPedsItemfc = new CastorPedestals(false);
00027    CastorPedestalWidths* rawWidthsItemfc = new CastorPedestalWidths(false);
00028 
00029    //Calculate pedestal constants
00030    std::cout << "Calculating Pedestal constants...\n";
00031    std::vector<NewPedBunch>::iterator bunch_it;
00032    for(bunch_it=Bunches.begin(); bunch_it != Bunches.end(); bunch_it++)
00033    {
00034       if(bunch_it->usedflag){
00035 
00036       if(verboseflag) std::cout << "Analyzing channel sector= " << bunch_it->detid.sector() 
00037         << " module = " << bunch_it->detid.module() 
00038         << std::endl;
00039       //pedestal constant is the mean
00040       bunch_it->cap[0] /= bunch_it->num[0][0];
00041       bunch_it->cap[1] /= bunch_it->num[1][1];
00042       bunch_it->cap[2] /= bunch_it->num[2][2];
00043       bunch_it->cap[3] /= bunch_it->num[3][3];
00044       bunch_it->capfc[0] /= bunch_it->num[0][0];
00045       bunch_it->capfc[1] /= bunch_it->num[1][1];
00046       bunch_it->capfc[2] /= bunch_it->num[2][2];
00047       bunch_it->capfc[3] /= bunch_it->num[3][3];
00048       //widths are the covariance matrix--assumed symmetric
00049       bunch_it->sig[0][0] = (bunch_it->prod[0][0]/bunch_it->num[0][0])-(bunch_it->cap[0]*bunch_it->cap[0]);
00050       bunch_it->sig[0][1] = (bunch_it->prod[0][1]/bunch_it->num[0][1])-(bunch_it->cap[0]*bunch_it->cap[1]);
00051       bunch_it->sig[0][2] = (bunch_it->prod[0][2]/bunch_it->num[0][2])-(bunch_it->cap[0]*bunch_it->cap[2]);
00052       bunch_it->sig[0][3] = (bunch_it->prod[0][3]/bunch_it->num[0][3])-(bunch_it->cap[0]*bunch_it->cap[3]);
00053       bunch_it->sig[1][0] = (bunch_it->prod[1][0]/bunch_it->num[1][0])-(bunch_it->cap[1]*bunch_it->cap[0]);
00054       bunch_it->sig[1][1] = (bunch_it->prod[1][1]/bunch_it->num[1][1])-(bunch_it->cap[1]*bunch_it->cap[1]);
00055       bunch_it->sig[1][2] = (bunch_it->prod[1][2]/bunch_it->num[1][2])-(bunch_it->cap[1]*bunch_it->cap[2]);
00056       bunch_it->sig[1][3] = (bunch_it->prod[1][3]/bunch_it->num[1][3])-(bunch_it->cap[1]*bunch_it->cap[3]);
00057       bunch_it->sig[2][0] = (bunch_it->prod[2][0]/bunch_it->num[2][0])-(bunch_it->cap[2]*bunch_it->cap[0]);
00058       bunch_it->sig[2][1] = (bunch_it->prod[2][1]/bunch_it->num[2][1])-(bunch_it->cap[2]*bunch_it->cap[1]);
00059       bunch_it->sig[2][2] = (bunch_it->prod[2][2]/bunch_it->num[2][2])-(bunch_it->cap[2]*bunch_it->cap[2]);
00060       bunch_it->sig[2][3] = (bunch_it->prod[2][3]/bunch_it->num[2][3])-(bunch_it->cap[2]*bunch_it->cap[3]);
00061       bunch_it->sig[3][0] = (bunch_it->prod[3][0]/bunch_it->num[3][0])-(bunch_it->cap[3]*bunch_it->cap[0]);
00062       bunch_it->sig[3][1] = (bunch_it->prod[3][1]/bunch_it->num[3][1])-(bunch_it->cap[3]*bunch_it->cap[1]);
00063       bunch_it->sig[3][2] = (bunch_it->prod[3][2]/bunch_it->num[3][2])-(bunch_it->cap[3]*bunch_it->cap[2]);
00064       bunch_it->sig[3][3] = (bunch_it->prod[3][3]/bunch_it->num[3][3])-(bunch_it->cap[3]*bunch_it->cap[3]);
00065 
00066       bunch_it->sigfc[0][0] = (bunch_it->prodfc[0][0]/bunch_it->num[0][0])-(bunch_it->capfc[0]*bunch_it->capfc[0]);
00067       bunch_it->sigfc[0][1] = (bunch_it->prodfc[0][1]/bunch_it->num[0][1])-(bunch_it->capfc[0]*bunch_it->capfc[1]);
00068       bunch_it->sigfc[0][2] = (bunch_it->prodfc[0][2]/bunch_it->num[0][2])-(bunch_it->capfc[0]*bunch_it->capfc[2]);
00069       bunch_it->sigfc[0][3] = (bunch_it->prodfc[0][3]/bunch_it->num[0][3])-(bunch_it->capfc[0]*bunch_it->capfc[3]);
00070       bunch_it->sigfc[1][0] = (bunch_it->prodfc[1][0]/bunch_it->num[1][0])-(bunch_it->capfc[1]*bunch_it->capfc[0]);
00071       bunch_it->sigfc[1][1] = (bunch_it->prodfc[1][1]/bunch_it->num[1][1])-(bunch_it->capfc[1]*bunch_it->capfc[1]);
00072       bunch_it->sigfc[1][2] = (bunch_it->prodfc[1][2]/bunch_it->num[1][2])-(bunch_it->capfc[1]*bunch_it->capfc[2]);
00073       bunch_it->sigfc[1][3] = (bunch_it->prodfc[1][3]/bunch_it->num[1][3])-(bunch_it->capfc[1]*bunch_it->capfc[3]);
00074       bunch_it->sigfc[2][0] = (bunch_it->prodfc[2][0]/bunch_it->num[2][0])-(bunch_it->capfc[2]*bunch_it->capfc[0]);
00075       bunch_it->sigfc[2][1] = (bunch_it->prodfc[2][1]/bunch_it->num[2][1])-(bunch_it->capfc[2]*bunch_it->capfc[1]);
00076       bunch_it->sigfc[2][2] = (bunch_it->prodfc[2][2]/bunch_it->num[2][2])-(bunch_it->capfc[2]*bunch_it->capfc[2]);
00077       bunch_it->sigfc[2][3] = (bunch_it->prodfc[2][3]/bunch_it->num[2][3])-(bunch_it->capfc[2]*bunch_it->capfc[3]);
00078       bunch_it->sigfc[3][0] = (bunch_it->prodfc[3][0]/bunch_it->num[3][0])-(bunch_it->capfc[3]*bunch_it->capfc[0]);
00079       bunch_it->sigfc[3][1] = (bunch_it->prodfc[3][1]/bunch_it->num[3][1])-(bunch_it->capfc[3]*bunch_it->capfc[1]);
00080       bunch_it->sigfc[3][2] = (bunch_it->prodfc[3][2]/bunch_it->num[3][2])-(bunch_it->capfc[3]*bunch_it->capfc[2]);
00081       bunch_it->sigfc[3][3] = (bunch_it->prodfc[3][3]/bunch_it->num[3][3])-(bunch_it->capfc[3]*bunch_it->capfc[3]);
00082 
00083         for(int i = 0; i != 3; i++){
00084             CASTORMeans->Fill(bunch_it->cap[i]);
00085             CASTORWidths->Fill(bunch_it->sig[i][i]);
00086          }
00087 
00088       //if(bunch_it->detid.subdet() == 1){
00089  
00090 
00091 
00092       int fillphi = bunch_it->detid.sector();
00093       //if (bunch_it->detid.depth()==4) fillphi++;
00094 
00095   //    dephist[bunch_it->detid.module()-1]->Fill(bunch_it->detid.ieta(),fillphi,
00096    //             (bunch_it->cap[0]+bunch_it->cap[1]+bunch_it->cap[2]+bunch_it->cap[3])/4);
00097       dephist->Fill( bunch_it->detid.module(),fillphi,
00098                 (bunch_it->cap[0]+bunch_it->cap[1]+bunch_it->cap[2]+bunch_it->cap[3])/4);
00099 
00100       const CastorPedestal item(bunch_it->detid, bunch_it->cap[0], bunch_it->cap[1], bunch_it->cap[2], bunch_it->cap[3],
00101                               bunch_it->sig[0][0], bunch_it->sig[1][1], bunch_it->sig[2][2], bunch_it->sig[3][3]);
00102       rawPedsItem->addValues(item);
00103       CastorPedestalWidth widthsp(bunch_it->detid);
00104       widthsp.setSigma(0,0,bunch_it->sig[0][0]);
00105       widthsp.setSigma(0,1,bunch_it->sig[0][1]);
00106       widthsp.setSigma(0,2,bunch_it->sig[0][2]);
00107       widthsp.setSigma(0,3,bunch_it->sig[0][3]);
00108       widthsp.setSigma(1,0,bunch_it->sig[1][0]);
00109       widthsp.setSigma(1,1,bunch_it->sig[1][1]);
00110       widthsp.setSigma(1,2,bunch_it->sig[1][2]);
00111       widthsp.setSigma(1,3,bunch_it->sig[1][3]);
00112       widthsp.setSigma(2,0,bunch_it->sig[2][0]);
00113       widthsp.setSigma(2,1,bunch_it->sig[2][1]);
00114       widthsp.setSigma(2,2,bunch_it->sig[2][2]);
00115       widthsp.setSigma(2,3,bunch_it->sig[2][3]);
00116       widthsp.setSigma(3,0,bunch_it->sig[3][0]);
00117       widthsp.setSigma(3,1,bunch_it->sig[3][1]);
00118       widthsp.setSigma(3,2,bunch_it->sig[3][2]);
00119       widthsp.setSigma(3,3,bunch_it->sig[3][3]);
00120       rawWidthsItem->addValues(widthsp);
00121 
00122       const CastorPedestal itemfc(bunch_it->detid, bunch_it->capfc[0], bunch_it->capfc[1], bunch_it->capfc[2], bunch_it->capfc[3],
00123                               bunch_it->sigfc[0][0], bunch_it->sigfc[1][1], bunch_it->sigfc[2][2], bunch_it->sigfc[3][3]);
00124       rawPedsItemfc->addValues(itemfc);
00125       CastorPedestalWidth widthspfc(bunch_it->detid);
00126       widthspfc.setSigma(0,0,bunch_it->sigfc[0][0]);
00127       widthspfc.setSigma(0,1,bunch_it->sigfc[0][1]);
00128       widthspfc.setSigma(0,2,bunch_it->sigfc[0][2]);
00129       widthspfc.setSigma(0,3,bunch_it->sigfc[0][3]);
00130       widthspfc.setSigma(1,0,bunch_it->sigfc[1][0]);      
00131       widthspfc.setSigma(1,1,bunch_it->sigfc[1][1]);
00132       widthspfc.setSigma(1,2,bunch_it->sigfc[1][2]);
00133       widthspfc.setSigma(1,3,bunch_it->sigfc[1][3]);
00134       widthspfc.setSigma(2,0,bunch_it->sigfc[2][0]);
00135       widthspfc.setSigma(2,1,bunch_it->sigfc[2][1]);
00136       widthspfc.setSigma(2,2,bunch_it->sigfc[2][2]);
00137       widthspfc.setSigma(2,3,bunch_it->sigfc[2][3]);
00138       widthspfc.setSigma(3,0,bunch_it->sigfc[3][0]);
00139       widthspfc.setSigma(3,1,bunch_it->sigfc[3][1]);
00140       widthspfc.setSigma(3,2,bunch_it->sigfc[3][2]);
00141       widthspfc.setSigma(3,3,bunch_it->sigfc[3][3]);
00142       rawWidthsItemfc->addValues(widthspfc);
00143 
00144       }
00145    }
00146 
00147     // dump the resulting list of pedestals into a file
00148     std::ofstream outStream1(pedsADCfilename.c_str());
00149     CastorDbASCIIIO::dumpObject (outStream1, (*rawPedsItem) );
00150     std::ofstream outStream2(widthsADCfilename.c_str());
00151     CastorDbASCIIIO::dumpObject (outStream2, (*rawWidthsItem) );
00152 
00153     std::ofstream outStream3(pedsfCfilename.c_str());
00154     CastorDbASCIIIO::dumpObject (outStream3, (*rawPedsItemfc) );
00155     std::ofstream outStream4(widthsfCfilename.c_str());
00156     CastorDbASCIIIO::dumpObject (outStream4, (*rawWidthsItemfc) );
00157 
00158     if(dumpXML){
00159        std::ofstream outStream5(XMLfilename.c_str());
00160      //  CastorCondXML::dumpObject (outStream5, runnum, runnum, runnum, XMLtag, 1, (*rawPedsItem), (*rawWidthsItem)); 
00161     }
00162 
00163     if(hiSaveFlag){
00164        theFile->Write();
00165     }else{
00166        theFile->cd();
00167        theFile->cd("CASTOR");
00168        CASTORMeans->Write();
00169        CASTORWidths->Write();
00170  
00171     }
00172     theFile->cd();
00173         dephist->Write();
00174         dephist->SetDrawOption("colz");
00175         dephist->GetXaxis()->SetTitle("module");
00176         dephist->GetYaxis()->SetTitle("sector");
00177     
00178     //for (int n=0; n!= 4; n++) 
00179     //{
00180          //dephist[n]->Write();
00181          //dephist[n]->SetDrawOption("colz");
00182          //dephist[n]->GetXaxis()->SetTitle("i#eta");
00183          //dephist[n]->GetYaxis()->SetTitle("i#phi");
00184     //}
00185 
00186     std::stringstream tempstringout;
00187     tempstringout << runnum;
00188     std::string name1 = tempstringout.str() + "_pedplots_1d.png";
00189     std::string name2 = tempstringout.str() + "_pedplots_2d.png";
00190 
00191     TStyle *theStyle = new TStyle("style","null");
00192     theStyle->SetPalette(1,0);
00193     theStyle->SetCanvasDefH(1200); //Height of canvas
00194     theStyle->SetCanvasDefW(1600); //Width of canvas
00195 
00196     gStyle = theStyle;
00197 /*
00198     TCanvas * c1 = new TCanvas("c1","graph",1);
00199     c1->Divide(2,2);
00200     c1->cd(1);
00201     CASTORMeans->Draw();
00202     c1->SaveAs(name1.c_str());   
00203 
00204     theStyle->SetOptStat("n");
00205     gStyle = theStyle;
00206 
00207     TCanvas * c2 = new TCanvas("c2","graph",1);
00208  //   c2->Divide(2,2);
00209     c2->cd(1);
00210     dephist->Draw();
00211     dephist->SetDrawOption("colz");
00212     //c2->cd(2);
00213     //dephist[1]->Draw();
00214     //dephist[1]->SetDrawOption("colz");
00215     //c2->cd(3);
00216     //dephist[2]->Draw();
00217     //dephist[2]->SetDrawOption("colz");
00218     //c2->cd(4);
00219     //dephist[3]->Draw();
00220     //dephist[3]->SetDrawOption("colz");
00221     c2->SaveAs(name2.c_str());
00222 */
00223     std::cout << "Writing ROOT file... ";
00224     theFile->Close();
00225     std::cout << "ROOT file closed.\n";
00226 }
00227 
00228 // ------------ method called to for each event  ------------
00229 void
00230 CastorPedestalsAnalysis::analyze(const edm::Event& e, const edm::EventSetup& iSetup)
00231 {
00232    using namespace edm;
00233    using namespace std;
00234 
00235    edm::Handle<CastorDigiCollection> castor;              e.getByType(castor);
00236    edm::ESHandle<CastorDbService> conditions;
00237    iSetup.get<CastorDbRecord>().get(conditions);
00238 
00239    const CastorQIEShape* shape = conditions->getCastorShape();
00240 
00241    if(firsttime)
00242    {
00243       runnum = e.id().run();
00244       std::string runnum_string;
00245       std::stringstream tempstringout;
00246       tempstringout << runnum;
00247       runnum_string = tempstringout.str();
00248       ROOTfilename = runnum_string + "-peds_ADC.root";
00249       pedsADCfilename = runnum_string + "-peds_ADC.txt";
00250       pedsfCfilename = runnum_string + "-peds_fC.txt";
00251       widthsADCfilename = runnum_string + "-widths_ADC.txt";
00252       widthsfCfilename = runnum_string + "-widths_fC.txt";
00253       XMLfilename = runnum_string + "-peds_ADC_complete.xml"; 
00254       XMLtag = "Castor_pedestals_" + runnum_string;
00255 
00256       theFile = new TFile(ROOTfilename.c_str(), "RECREATE");
00257       theFile->cd();
00258       // Create sub-directories
00259       theFile->mkdir("CASTOR"); 
00260       theFile->cd();
00261 
00262       CASTORMeans = new TH1F("All Ped Means CASTOR","All Ped Means CASTOR", 100, 0, 9);
00263       CASTORWidths = new TH1F("All Ped Widths CASTOR","All Ped Widths CASTOR", 100, 0, 3);
00264 
00265       dephist = new TH2F("Pedestals (ADC)","All Castor",14, 0., 14.5, 16, .5, 16.5);
00266      // dephist[0] = new TH2F("Pedestals (ADC)","Depth 1",89, -44, 44, 72, .5, 72.5);
00267      // dephist[1] = new TH2F("Pedestals (ADC)","Depth 2",89, -44, 44, 72, .5, 72.5);
00268      // dephist[2] = new TH2F("Pedestals (ADC)","Depth 3",89, -44, 44, 72, .5, 72.5);
00269      // dephist[3] = new TH2F("Pedestals (ADC)","Depth 4",89, -44, 44, 72, .5, 72.5);
00270 
00271       edm::ESHandle<CastorElectronicsMap> refEMap;
00272       iSetup.get<CastorElectronicsMapRcd>().get(refEMap);
00273       const CastorElectronicsMap* myRefEMap = refEMap.product();
00274       std::vector<HcalGenericDetId> listEMap = myRefEMap->allPrecisionId();
00275       for (std::vector<HcalGenericDetId>::const_iterator it = listEMap.begin(); it != listEMap.end(); it++)
00276       {     
00277          HcalGenericDetId mygenid(it->rawId());
00278          if(mygenid.isHcalCastorDetId())
00279          {
00280             NewPedBunch a;
00281             HcalCastorDetId chanid(mygenid.rawId());
00282             a.detid = chanid;
00283             a.usedflag = false;
00284             string type;
00285                         type = "CASTOR";
00286             for(int i = 0; i != 4; i++)
00287             {
00288                a.cap[i] = 0;
00289                a.capfc[i] = 0;
00290                for(int j = 0; j != 4; j++)
00291                {
00292                   a.sig[i][j] = 0;
00293                   a.sigfc[i][j] = 0;
00294                   a.prod[i][j] = 0;
00295                   a.prodfc[i][j] = 0;
00296                   a.num[i][j] = 0;
00297                }
00298             }
00299             Bunches.push_back(a);
00300          }
00301       }
00302       firsttime = false;
00303    }
00304 
00305    std::vector<NewPedBunch>::iterator bunch_it;
00306 
00307    for(CastorDigiCollection::const_iterator j = castor->begin(); j != castor->end(); j++)
00308    {
00309       const CastorDataFrame digi = (const CastorDataFrame)(*j);
00310       for(bunch_it = Bunches.begin(); bunch_it != Bunches.end(); bunch_it++)
00311          if(bunch_it->detid.rawId() == digi.id().rawId()) break;
00312       bunch_it->usedflag = true;
00313       for(int ts = firstTS; ts != lastTS+1; ts++)
00314       {
00315          const CastorQIECoder* coder = conditions->getCastorCoder(digi.id().rawId());
00316          bunch_it->num[digi.sample(ts).capid()][digi.sample(ts).capid()] += 1;
00317          bunch_it->cap[digi.sample(ts).capid()] += digi.sample(ts).adc();
00318          double charge1 = coder->charge(*shape, digi.sample(ts).adc(), digi.sample(ts).capid());
00319          bunch_it->capfc[digi.sample(ts).capid()] += charge1;
00320          bunch_it->prod[digi.sample(ts).capid()][digi.sample(ts).capid()] += (digi.sample(ts).adc() * digi.sample(ts).adc());
00321          bunch_it->prodfc[digi.sample(ts).capid()][digi.sample(ts).capid()] += charge1 * charge1;
00322          if((ts+1 < digi.size()) && (ts+1 < lastTS)){
00323             bunch_it->prod[digi.sample(ts).capid()][digi.sample(ts+1).capid()] += digi.sample(ts).adc()*digi.sample(ts+1).adc();
00324             double charge2 = coder->charge(*shape, digi.sample(ts+1).adc(), digi.sample(ts+1).capid());
00325             bunch_it->prodfc[digi.sample(ts).capid()][digi.sample(ts+1).capid()] += charge1*charge2;
00326             bunch_it->num[digi.sample(ts).capid()][digi.sample(ts+1).capid()] += 1;
00327          }
00328          if((ts+2 < digi.size()) && (ts+2 < lastTS)){
00329             bunch_it->prod[digi.sample(ts).capid()][digi.sample(ts+2).capid()] += digi.sample(ts).adc()*digi.sample(ts+2).adc();
00330             double charge2 = coder->charge(*shape, digi.sample(ts+2).adc(), digi.sample(ts+2).capid());
00331             bunch_it->prodfc[digi.sample(ts).capid()][digi.sample(ts+2).capid()] += charge1*charge2;
00332             bunch_it->num[digi.sample(ts).capid()][digi.sample(ts+2).capid()] += 1;
00333          }
00334          if((ts+3 < digi.size()) && (ts+3 < lastTS)){
00335             bunch_it->prod[digi.sample(ts).capid()][digi.sample(ts+3).capid()] += digi.sample(ts).adc()*digi.sample(ts+3).adc();
00336             double charge2 = coder->charge(*shape, digi.sample(ts+3).adc(), digi.sample(ts+3).capid());
00337             bunch_it->prodfc[digi.sample(ts).capid()][digi.sample(ts+3).capid()] += charge1*charge2;
00338             bunch_it->num[digi.sample(ts).capid()][digi.sample(ts+3).capid()] += 1;
00339          }
00340       }
00341    }
00342 
00343 
00344 //this is the last brace
00345 }
00346 
00347 //define this as a plug-in
00348 DEFINE_FWK_MODULE(CastorPedestalsAnalysis);