CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_4/src/CalibMuon/DTCalibration/plugins/DTT0CalibrationNew.cc

Go to the documentation of this file.
00001 /*
00002  *  See header file for a description of this class.
00003  *
00004  *  $Date: 2012/02/17 16:39:06 $
00005  *  $Revision: 1.5.2.1 $
00006  *  \author S. Bolognesi - INFN Torino
00007  *  06/08/2008 Mofified by Antonio.Vilela.Pereira@cern.ch
00008  */
00009 
00010 #include "CalibMuon/DTCalibration/plugins/DTT0CalibrationNew.h"
00011 #include "CalibMuon/DTCalibration/interface/DTCalibDBUtils.h"
00012 
00013 #include "FWCore/Framework/interface/Event.h"
00014 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00015 
00016 #include "Geometry/Records/interface/MuonGeometryRecord.h"
00017 #include "Geometry/Records/interface/MuonNumberingRecord.h"
00018 
00019 #include "DataFormats/DTDigi/interface/DTDigiCollection.h"
00020 #include "CondFormats/DTObjects/interface/DTT0.h"
00021 
00022 #include <CondFormats/DTObjects/interface/DTTtrig.h>
00023 #include <CondFormats/DataRecord/interface/DTTtrigRcd.h>
00024 
00025 #include "TH1I.h"
00026 #include "TFile.h"
00027 #include "TKey.h"
00028 #include "TSpectrum.h"
00029 #include "TF1.h"
00030 
00031 using namespace std;
00032 using namespace edm;
00033 // using namespace cond;
00034 
00035 // Constructor
00036 DTT0CalibrationNew::DTT0CalibrationNew(const edm::ParameterSet& pset) {
00037   // Get the debug parameter for verbose output
00038   debug = pset.getUntrackedParameter<bool>("debug");
00039   if(debug) 
00040     cout << "[DTT0CalibrationNew]Constructor called!" << endl;
00041 
00042   // Get the label to retrieve digis from the event
00043   digiLabel = pset.getUntrackedParameter<string>("digiLabel");
00044 
00045   dbLabel  = pset.getUntrackedParameter<string>("dbLabel", "");
00046 
00047   // The root file which contain the histos per layer
00048   string rootFileName = pset.getUntrackedParameter<string>("rootFileName","DTT0PerLayer.root");
00049   theFile = new TFile(rootFileName.c_str(), "RECREATE");
00050  
00051   theCalibWheel =  pset.getUntrackedParameter<string>("calibWheel", "All"); //FIXME amke a vector of integer instead of a string
00052   if(theCalibWheel != "All") {
00053     stringstream linestr;
00054     int selWheel;
00055     linestr << theCalibWheel;
00056     linestr >> selWheel;
00057     cout << "[DTT0CalibrationNewPerLayer] chosen wheel " << selWheel << endl;
00058   }
00059 
00060   // Sector/s to calibrate
00061   theCalibSector =  pset.getUntrackedParameter<string>("calibSector", "All"); //FIXME amke a vector of integer instead of a string
00062   if(theCalibSector != "All") {
00063     stringstream linestr;
00064     int selSector;
00065     linestr << theCalibSector;
00066     linestr >> selSector;
00067     cout << "[DTT0CalibrationNewPerLayer] chosen sector " << selSector << endl;
00068   }
00069 
00070   vector<string> defaultCell;
00071   defaultCell.push_back("None");
00072   cellsWithHistos = pset.getUntrackedParameter<vector<string> >("cellsWithHisto", defaultCell);
00073   for(vector<string>::const_iterator cell = cellsWithHistos.begin(); cell != cellsWithHistos.end(); cell++){
00074     if((*cell) != "None"){
00075       stringstream linestr;
00076       int wheel,sector,station,sl,layer,wire;
00077       linestr << (*cell);
00078       linestr >> wheel >> sector >> station >> sl >> layer >> wire;
00079       wireIdWithHistos.push_back(DTWireId(wheel,station,sector,sl,layer,wire));
00080     }
00081   }
00082 
00083   hT0SectorHisto=0;
00084 
00085   nevents=0;
00086   eventsForLayerT0 = pset.getParameter<unsigned int>("eventsForLayerT0");
00087   eventsForWireT0 = pset.getParameter<unsigned int>("eventsForWireT0");
00088   tpPeakWidth = pset.getParameter<double>("tpPeakWidth");
00089   tpPeakWidthPerLayer = pset.getParameter<double>("tpPeakWidthPerLayer");
00090   timeBoxWidth = pset.getParameter<unsigned int>("timeBoxWidth"); 
00091   rejectDigiFromPeak = pset.getParameter<unsigned int>("rejectDigiFromPeak"); 
00092 
00093   spectrum = new TSpectrum(5);
00094   retryForLayerT0 = 0;  
00095 }
00096 
00097 // Destructor
00098 DTT0CalibrationNew::~DTT0CalibrationNew(){
00099   if(debug) 
00100     cout << "[DTT0CalibrationNew]Destructor called!" << endl;
00101 
00102   delete spectrum;
00103   theFile->Close();
00104 }
00105 
00107 void DTT0CalibrationNew::analyze(const edm::Event & event, const edm::EventSetup& eventSetup) {
00108   if(debug || event.id().event() % 500==0)
00109     cout << "--- [DTT0CalibrationNew] Analysing Event: #Run: " << event.id().run()
00110          << " #Event: " << event.id().event() << endl;
00111   nevents++;
00112 
00113   // Get the digis from the event
00114   Handle<DTDigiCollection> digis; 
00115   event.getByLabel(digiLabel, digis);
00116 
00117   // Get the DT Geometry
00118   eventSetup.get<MuonGeometryRecord>().get(dtGeom);
00119 
00120   // Get ttrig DB
00121   edm::ESHandle<DTTtrig> tTrigMap;
00122   eventSetup.get<DTTtrigRcd>().get(dbLabel,tTrigMap);
00123   
00124   // Iterate through all digi collections ordered by LayerId   
00125   DTDigiCollection::DigiRangeIterator dtLayerIt;
00126   for (dtLayerIt = digis->begin();
00127        dtLayerIt != digis->end();
00128        ++dtLayerIt){
00129     // Get the iterators over the digis associated with this LayerId
00130     const DTDigiCollection::Range& digiRange = (*dtLayerIt).second;
00131   
00132     // Get the layerId
00133     const DTLayerId layerId = (*dtLayerIt).first; //FIXME: check to be in the right sector
00134     const DTChamberId chamberId = layerId.superlayerId().chamberId();
00135 
00136     if((theCalibWheel != "All") && (layerId.superlayerId().chamberId().wheel() != selWheel))
00137       continue;
00138     if((theCalibSector != "All") && (layerId.superlayerId().chamberId().sector() != selSector))
00139       continue;
00140  
00141     //if(debug) {
00142     //  cout << "Layer " << layerId<<" with "<<distance(digiRange.first, digiRange.second)<<" digi"<<endl;
00143     //}
00144 
00145     float tTrig,tTrigRMS, kFactor;
00146     tTrigMap->get(layerId.superlayerId(), tTrig, tTrigRMS, kFactor, DTTimeUnits::counts );
00147     if(debug&&(nevents <= 1)){
00148         cout << "  Superlayer: " << layerId.superlayerId() << endl 
00149              << "            tTrig,tTrigRMS= " << tTrig << ", " << tTrigRMS << endl;
00150     }   
00151 
00152     // Loop over all digis in the given layer
00153     for (DTDigiCollection::const_iterator digi = digiRange.first;
00154          digi != digiRange.second;
00155          digi++) {
00156       double t0 = (*digi).countsTDC();
00157 
00158       //Use first bunch of events to fill t0 per layer
00159       if(nevents < eventsForLayerT0){
00160         //Get the per-layer histo from the map
00161         TH1I *hT0LayerHisto = theHistoLayerMap[layerId];
00162         //If it doesn't exist, book it
00163         if(hT0LayerHisto == 0){
00164           theFile->cd();
00165           float hT0Min = tTrig - 2*tTrigRMS;
00166           float hT0Max = hT0Min + timeBoxWidth;
00167           hT0LayerHisto = new TH1I(getHistoName(layerId).c_str(),
00168                                    "T0 from pulses by layer (TDC counts, 1 TDC count = 0.781 ns)",
00169                                    timeBoxWidth,hT0Min,hT0Max);
00170           if(debug)
00171             cout << "  New T0 per Layer Histo: " << hT0LayerHisto->GetName() << endl;
00172           theHistoLayerMap[layerId] = hT0LayerHisto;
00173         }
00174     
00175         //Fill the histos
00176         theFile->cd();
00177         if(hT0LayerHisto != 0) {
00178           //  if(debug)
00179           // cout<<"Filling histo "<<hT0LayerHisto->GetName()<<" with digi "<<t0<<" TDC counts"<<endl;
00180           hT0LayerHisto->Fill(t0);
00181         }
00182       }
00183 
00184       //Use all the remaining events to compute t0 per wire
00185       if(nevents>eventsForLayerT0){
00186         // Get the wireId
00187         const DTWireId wireId(layerId, (*digi).wire());
00188         if(debug) {
00189           cout << "   Wire: " << wireId << endl
00190                << "       time (TDC counts): " << (*digi).countsTDC()<< endl;
00191         }   
00192 
00193         //Fill the histos per wire for the chosen cells
00194         vector<DTWireId>::iterator it = find(wireIdWithHistos.begin(),wireIdWithHistos.end(),wireId);
00195         if (it!=wireIdWithHistos.end()){
00196           //Get the per-wire histo from the map
00197           TH1I *hT0WireHisto = theHistoWireMap[wireId]; 
00198           //If it doesn't exist, book it
00199           if(hT0WireHisto == 0){
00200             theFile->cd(); 
00201             hT0WireHisto = new TH1I(getHistoName(wireId).c_str(),"T0 from pulses by wire (TDC counts, 1 TDC count = 0.781 ns)",7000,0,7000);
00202             //hT0SectorHisto->GetBinCenter(hT0SectorHisto->GetMaximumBin())-100,
00203             //hT0SectorHisto->GetBinCenter(hT0SectorHisto->GetMaximumBin())+100);
00204             if(debug)
00205               cout << "  New T0 per wire Histo: " << hT0WireHisto->GetName() << endl;
00206             theHistoWireMap[wireId] = hT0WireHisto;
00207           }
00208           //Fill the histos
00209           theFile->cd();
00210           if(hT0WireHisto != 0) {
00211             //if(debug)
00212             // cout<<"Filling histo "<<hT0WireHisto->GetName()<<" with digi "<<t0<<" TDC counts"<<endl;
00213             hT0WireHisto->Fill(t0);
00214           }
00215         }
00216 
00217         //Check the tzero has reasonable value
00218         //float hT0Min = tTrig - 2*tTrigRMS;
00219         //float hT0Max = hT0Min + timeBoxWidth;
00220         /*if(abs(hT0SectorHisto->GetBinCenter(hT0SectorHisto->GetMaximumBin()) - t0) > rejectDigiFromPeak){
00221           if(debug)
00222             cout<<"digi skipped because t0 per sector "<<hT0SectorHisto->GetBinCenter(hT0SectorHisto->GetMaximumBin())<<endl;
00223           continue;
00224         }*/
00225         /*if((t0 < hT0Min)||(t0 > hT0Max)){
00226           if(debug)
00227             cout<<"digi skipped because t0 outside of interval (" << hT0Min << "," << hT0Max << ")" <<endl;
00228           continue;
00229         }*/
00230         //Select per layer
00231         if(fabs(theTPPeakMap[layerId] - t0) > rejectDigiFromPeak){
00232           if(debug)
00233             cout<<"digi skipped because t0 too far from peak " << theTPPeakMap[layerId] << endl;
00234           continue;     
00235         }
00236 
00237         //Find to ref. per chamber
00238         theSumT0ByChamber[chamberId] = theSumT0ByChamber[chamberId] + t0;
00239         theCountT0ByChamber[chamberId]++;
00240 
00241         //Use second bunch of events to compute a t0 reference per wire
00242         if(nevents< (eventsForLayerT0 + eventsForWireT0)){
00243           if(!nDigiPerWire_ref[wireId]){
00244             mK_ref[wireId] = 0;
00245           }
00246           nDigiPerWire_ref[wireId] = nDigiPerWire_ref[wireId] + 1;
00247           mK_ref[wireId] = mK_ref[wireId] + (t0-mK_ref[wireId])/nDigiPerWire_ref[wireId];
00248         }
00249         //Use last all the remaining events to compute the mean and sigma t0 per wire
00250         else if(nevents>(eventsForLayerT0 + eventsForWireT0)){
00251           if(abs(t0-mK_ref[wireId]) > tpPeakWidth)
00252             continue;
00253           if(!nDigiPerWire[wireId]){
00254             theAbsoluteT0PerWire[wireId] = 0;
00255             qK[wireId] = 0;
00256             mK[wireId] = 0;
00257           }
00258           nDigiPerWire[wireId] = nDigiPerWire[wireId] + 1;
00259           theAbsoluteT0PerWire[wireId] = theAbsoluteT0PerWire[wireId] + t0;
00260           //theSigmaT0PerWire[wireId] = theSigmaT0PerWire[wireId] + (t0*t0);
00261           qK[wireId] = qK[wireId] + ((nDigiPerWire[wireId]-1)*(t0-mK[wireId])*(t0-mK[wireId])/nDigiPerWire[wireId]);
00262           mK[wireId] = mK[wireId] + (t0-mK[wireId])/nDigiPerWire[wireId];
00263         }
00264       }//end if(nevents>1000)
00265     }//end loop on digi
00266   }//end loop on layer
00267 
00268   //Use the t0 per layer histos to have an indication about the t0 position 
00269   if(nevents == eventsForLayerT0){
00270     bool increaseEvtsForLayerT0 = false;        
00271     for(map<DTLayerId, TH1I*>::const_iterator lHisto = theHistoLayerMap.begin();
00272         lHisto != theHistoLayerMap.end();
00273         lHisto++){
00274       if(debug)
00275         cout<<"Reading histogram "<<(*lHisto).second->GetName()<<" with mean "<<(*lHisto).second->GetMean()<<" and RMS "<<(*lHisto).second->GetRMS() << endl;
00276 
00277       //Find peaks
00278       //int npeaks = spectrum->Search((*lHisto).second,0.5,"goff");
00279       //int npeaks = spectrum->Search((*lHisto).second,(tpPeakWidthPerLayer/2.),"goff",0.3);
00280       int npeaks = spectrum->Search((*lHisto).second,(tpPeakWidthPerLayer/2.),"",0.3);
00281 
00282       float *peaks = spectrum->GetPositionX();  
00283       //Put in a std::vector<float>
00284       vector<float> peakMeans(peaks,peaks + npeaks);
00285       //Sort the peaks in ascending order
00286       sort(peakMeans.begin(),peakMeans.end());
00287                                 
00288       //Find best peak -- preliminary criteria: find peak closest to center of time box 
00289       float tTrig,tTrigRMS, kFactor;
00290       tTrigMap->get((*lHisto).first.superlayerId(), tTrig, tTrigRMS, kFactor, DTTimeUnits::counts );
00291 
00292       float timeBoxCenter = (2*tTrig + (float)timeBoxWidth)/2.; 
00293       float hMin = (*lHisto).second->GetXaxis()->GetXmin();
00294       float hMax = (*lHisto).second->GetXaxis()->GetXmax();             
00295       vector<float>::const_iterator tpPeak = peakMeans.end();
00296       for(vector<float>::const_iterator it = peakMeans.begin(); it != peakMeans.end(); ++it){
00297         float mean = *it;
00298 
00299         int bin = (*lHisto).second->GetXaxis()->FindBin(mean);
00300         float yp = (*lHisto).second->GetBinContent(bin);
00301         if(debug) cout << "Peak : (" << mean << "," << yp << ")" << endl; 
00302 
00303         //Find RMS
00304         float previous_peak = (it == peakMeans.begin())?hMin:*(it - 1);
00305         float next_peak = (it == (peakMeans.end()-1))?hMax:*(it + 1);
00306 
00307         float rangemin = mean - (mean - previous_peak)/8.;
00308         float rangemax = mean + (next_peak - mean)/8.;
00309         int binmin = (*lHisto).second->GetXaxis()->FindBin(rangemin);
00310         int binmax = (*lHisto).second->GetXaxis()->FindBin(rangemax);
00311         (*lHisto).second->GetXaxis()->SetRange(binmin,binmax);
00312         //RMS estimate
00313         float rms_seed = (*lHisto).second->GetRMS();
00314 
00315         /*rangemin = mean - 2*rms_seed;
00316         rangemax = mean + 2*rms_seed;
00317         if(debug) cout << "Seed for RMS, Fit min, Fit max: " << rms_seed << ", " << rangemin << ", " << rangemax << endl;
00318         //Fit to gaussian
00319         string funcname("fitFcn_");
00320         funcname += (*lHisto).second->GetName();
00321         if(debug) cout << "Fitting function " << funcname << endl; 
00322         TF1* func = new TF1(funcname.c_str(),"gaus",rangemin,rangemax);
00323         func->SetParameters(yp,mean,rms_seed);
00324         (*lHisto).second->Fit(func,"Q","",rangemin,rangemax);
00325 
00326         float fitconst = func->GetParameter(0);
00327         float fitmean = func->GetParameter(1);
00328         float fitrms = func->GetParameter(2);
00329         float chisquare = func->GetChisquare()/func->GetNDF();
00330         if(debug) cout << "Gaussian fit constant,mean,RMS,chi2= " << fitconst << ", " << fitmean << ", " << fitrms << ", " << chisquare << endl;*/
00331 
00332         //Reject peaks with RMS larger than specified
00333         //if(fitrms > tpPeakWidth) continue;
00334         if(rms_seed > tpPeakWidthPerLayer) continue;
00335 
00336         if(fabs(mean - timeBoxCenter) < fabs(*tpPeak - timeBoxCenter)) tpPeak = it;
00337       } 
00338       //Didn't find peak        
00339       /*if(tpPeak == peakMeans.end()){
00340         if(retryForLayerT0 < 2){
00341           increaseEvtsForLayerT0 = true;
00342           retryForLayerT0++;
00343           break;
00344         } 
00345       }*/
00346 
00347       float selPeak = (tpPeak != peakMeans.end())?*tpPeak:(*lHisto).second->GetBinCenter((*lHisto).second->GetMaximumBin());            
00348       if(debug) cout << "Peak selected at " << selPeak << endl;
00349         
00350       theTPPeakMap[(*lHisto).first] = selPeak;
00351                 
00352       //Take the mean as a first t0 estimation
00353       /*if((*lHisto).second->GetRMS() < tpPeakWidth){
00354         if(hT0SectorHisto == 0){
00355           hT0SectorHisto = new TH1D("hT0AllLayerOfSector","T0 from pulses per layer in sector", 
00356                                     //20, (*lHisto).second->GetMean()-100, (*lHisto).second->GetMean()+100);
00357                                     700, 0, 7000);
00358                                     //300,3300,3600);   
00359         }
00360         if(debug)
00361           cout<<" accepted"<<endl;
00362         //TH1I* aux_histo = (*lHisto).second;
00363         //aux_histo->GetXaxis()->SetRangeUser(3300,3600);
00364         hT0SectorHisto->Fill((*lHisto).second->GetMean());
00365         //hT0SectorHisto->Fill(aux_histo->GetMean());
00366       }
00367       //Take the mean of noise + 400ns as a first t0 estimation
00368       //if((*lHisto).second->GetRMS()>10.0 && ((*lHisto).second->GetRMS()<15.0)){
00369       //double t0_estim = (*lHisto).second->GetMean() + 400;
00370       //if(hT0SectorHisto == 0){
00371       //  hT0SectorHisto = new TH1D("hT0AllLayerOfSector","T0 from pulses per layer in sector", 
00372       //                            //20, t0_estim-100, t0_estim+100);
00373       //                            700, 0, 7000);
00374       //}
00375       //if(debug)
00376       //  cout<<" accepted + 400ns"<<endl;
00377       //hT0SectorHisto->Fill((*lHisto).second->GetMean() + 400);
00378       //}
00379       if(debug)
00380         cout<<endl;
00381 
00382       theT0LayerMap[(*lHisto).second->GetName()] = (*lHisto).second->GetMean();
00383       theSigmaT0LayerMap[(*lHisto).second->GetName()] = (*lHisto).second->GetRMS();*/
00384     }
00385     /*if(!hT0SectorHisto){
00386       cout<<"[DTT0CalibrationNew]: All the t0 per layer are still uncorrect: trying with greater number of events"<<endl;
00387       eventsForLayerT0 = eventsForLayerT0*2;
00388       return;
00389     }
00390     if(debug)
00391       cout<<"[DTT0CalibrationNew] t0 reference for this sector "<<
00392         hT0SectorHisto->GetBinCenter(hT0SectorHisto->GetMaximumBin())<<endl;*/
00393     if(increaseEvtsForLayerT0){
00394         cout<<"[DTT0CalibrationNew]: t0 per layer are still uncorrect: trying with greater number of events"<<endl;
00395         eventsForLayerT0 = eventsForLayerT0*2;
00396         return;
00397     }           
00398   } 
00399 }
00400 
00401 void DTT0CalibrationNew::endJob() {
00402 
00403   DTT0* t0s = new DTT0();
00404   DTT0* t0sWRTChamber = new DTT0();
00405 
00406   if(debug) 
00407     cout << "[DTT0CalibrationNewPerLayer]Writing histos to file!" << endl;
00408 
00409   theFile->cd();
00410   //hT0SectorHisto->Write();
00411   for(map<DTWireId, TH1I*>::const_iterator wHisto = theHistoWireMap.begin();
00412       wHisto != theHistoWireMap.end();
00413       wHisto++) {
00414     (*wHisto).second->Write(); 
00415   }
00416   for(map<DTLayerId, TH1I*>::const_iterator lHisto = theHistoLayerMap.begin();
00417       lHisto != theHistoLayerMap.end();
00418       lHisto++) {
00419     (*lHisto).second->Write(); 
00420   }  
00421 
00422   if(debug) 
00423     cout << "[DTT0CalibrationNew] Compute and store t0 and sigma per wire" << endl;
00424 
00425   for(map<DTChamberId,double>::const_iterator chamber = theSumT0ByChamber.begin();
00426       chamber != theSumT0ByChamber.end();
00427       ++chamber) theRefT0ByChamber[(*chamber).first] = theSumT0ByChamber[(*chamber).first]/((double)theCountT0ByChamber[(*chamber).first]);
00428   
00429   for(map<DTWireId, double>::const_iterator wiret0 = theAbsoluteT0PerWire.begin();
00430       wiret0 != theAbsoluteT0PerWire.end();
00431       wiret0++){
00432     if(nDigiPerWire[(*wiret0).first]){
00433       double t0 = (*wiret0).second/nDigiPerWire[(*wiret0).first];
00434       DTChamberId chamberId = ((*wiret0).first).chamberId();
00435       //theRelativeT0PerWire[(*wiret0).first] = t0 - hT0SectorHisto->GetBinCenter(hT0SectorHisto->GetMaximumBin());
00436       theRelativeT0PerWire[(*wiret0).first] = t0 - theRefT0ByChamber[chamberId];        
00437       cout<<"Wire "<<(*wiret0).first<<" has    t0 "<<t0<<"(absolute) "<<theRelativeT0PerWire[(*wiret0).first]<<"(relative)";
00438 
00439       //theSigmaT0PerWire[(*wiret0).first] = sqrt((theSigmaT0PerWire[(*wiret0).first] / nDigiPerWire[(*wiret0).first]) - t0*t0);
00440       theSigmaT0PerWire[(*wiret0).first] = sqrt(qK[(*wiret0).first]/nDigiPerWire[(*wiret0).first]);
00441       cout<<"    sigma "<<theSigmaT0PerWire[(*wiret0).first]<<endl;
00442     }
00443     else{
00444       cout<<"[DTT0CalibrationNew] ERROR: no digis in wire "<<(*wiret0).first<<endl;
00445       abort();
00446     }
00447   }
00448 
00450   // Get all the sls from the setup
00451   const vector<DTSuperLayer*> superLayers = dtGeom->superLayers();     
00452   // Loop over all SLs
00453   for(vector<DTSuperLayer*>::const_iterator  sl = superLayers.begin();
00454       sl != superLayers.end(); sl++) {
00455 
00456   
00457     //Compute mean for odd and even superlayers
00458     double oddLayersMean=0;
00459     double evenLayersMean=0; 
00460     double oddLayersDen=0;
00461     double evenLayersDen=0;
00462     for(map<DTWireId, double>::const_iterator wiret0 = theRelativeT0PerWire.begin();
00463         wiret0 != theRelativeT0PerWire.end();
00464         wiret0++){
00465       if((*wiret0).first.layerId().superlayerId() == (*sl)->id()){
00466         if(debug)
00467           cout<<"[DTT0CalibrationNew] Superlayer "<<(*sl)->id()
00468               <<"layer " <<(*wiret0).first.layerId().layer()<<" with "<<(*wiret0).second<<endl;
00469         if(((*wiret0).first.layerId().layer()) % 2){
00470           oddLayersMean = oddLayersMean + (*wiret0).second;
00471           oddLayersDen++;
00472         }
00473         else{
00474           evenLayersMean = evenLayersMean + (*wiret0).second;
00475           evenLayersDen++;
00476         }
00477       }
00478     }
00479     oddLayersMean = oddLayersMean/oddLayersDen;
00480     evenLayersMean = evenLayersMean/evenLayersDen;
00481     if(debug && oddLayersMean)
00482       cout<<"[DTT0CalibrationNew] Relative T0 mean for  odd layers "<<oddLayersMean<<"  even layers"<<evenLayersMean<<endl;
00483 
00484     //Compute sigma for odd and even superlayers
00485     double oddLayersSigma=0;
00486     double evenLayersSigma=0;
00487     for(map<DTWireId, double>::const_iterator wiret0 = theRelativeT0PerWire.begin();
00488         wiret0 != theRelativeT0PerWire.end();
00489         wiret0++){
00490       if((*wiret0).first.layerId().superlayerId() == (*sl)->id()){
00491         if(((*wiret0).first.layerId().layer()) % 2){
00492           oddLayersSigma = oddLayersSigma + ((*wiret0).second - oddLayersMean) * ((*wiret0).second - oddLayersMean);
00493         }
00494         else{
00495           evenLayersSigma = evenLayersSigma + ((*wiret0).second - evenLayersMean) * ((*wiret0).second - evenLayersMean);
00496         }
00497       }
00498     }
00499     oddLayersSigma = oddLayersSigma/oddLayersDen;
00500     evenLayersSigma = evenLayersSigma/evenLayersDen;
00501     oddLayersSigma = sqrt(oddLayersSigma);
00502     evenLayersSigma = sqrt(evenLayersSigma);
00503 
00504     if(debug && oddLayersMean)
00505       cout<<"[DTT0CalibrationNew] Relative T0 sigma for  odd layers "<<oddLayersSigma<<"  even layers"<<evenLayersSigma<<endl;
00506 
00507     //Recompute the mean for odd and even superlayers discarding fluctations
00508     double oddLayersFinalMean=0; 
00509     double evenLayersFinalMean=0;
00510     for(map<DTWireId, double>::const_iterator wiret0 = theRelativeT0PerWire.begin();
00511         wiret0 != theRelativeT0PerWire.end();
00512         wiret0++){
00513       if((*wiret0).first.layerId().superlayerId() == (*sl)->id()){
00514         if(((*wiret0).first.layerId().layer()) % 2){
00515           if(abs((*wiret0).second - oddLayersMean) < (2*oddLayersSigma))
00516             oddLayersFinalMean = oddLayersFinalMean + (*wiret0).second;
00517         }
00518         else{
00519           if(abs((*wiret0).second - evenLayersMean) < (2*evenLayersSigma))
00520             evenLayersFinalMean = evenLayersFinalMean + (*wiret0).second;
00521         }
00522       }
00523     }
00524     oddLayersFinalMean = oddLayersFinalMean/oddLayersDen;
00525     evenLayersFinalMean = evenLayersFinalMean/evenLayersDen;
00526     if(debug && oddLayersMean)
00527       cout<<"[DTT0CalibrationNew] Final relative T0 mean for  odd layers "<<oddLayersFinalMean<<"  even layers"<<evenLayersFinalMean<<endl;
00528 
00529     for(map<DTWireId, double>::const_iterator wiret0 = theRelativeT0PerWire.begin();
00530         wiret0 != theRelativeT0PerWire.end();
00531         wiret0++){
00532       if((*wiret0).first.layerId().superlayerId() == (*sl)->id()){
00533         double t0=-999;
00534         if(((*wiret0).first.layerId().layer()) % 2)
00535           t0 = (*wiret0).second + (evenLayersFinalMean - oddLayersFinalMean);
00536         else
00537           t0 = (*wiret0).second;
00538 
00539         cout<<"[DTT0CalibrationNew] Wire "<<(*wiret0).first<<" has    t0 "<<(*wiret0).second<<" (relative, after even-odd layer corrections)  "
00540             <<"    sigma "<<theSigmaT0PerWire[(*wiret0).first]<<endl;
00541         //Store the results into DB
00542         t0s->set((*wiret0).first, t0, theSigmaT0PerWire[(*wiret0).first],DTTimeUnits::counts); 
00543       }
00544     }
00545   }
00546   
00548   if(debug) 
00549     cout << "[DTT0CalibrationNew]Computing relative t0 wrt to chamber average" << endl;
00550   //Compute the reference for each chamber
00551   map<DTChamberId,double> sumT0ByChamber;
00552   map<DTChamberId,int> countT0ByChamber;
00553   for(DTT0::const_iterator tzero = t0s->begin();
00554       tzero != t0s->end(); tzero++) {
00555 // @@@ NEW DTT0 FORMAT
00556 //    DTChamberId chamberId((*tzero).first.wheelId,
00557 //                        (*tzero).first.stationId,
00558 //                        (*tzero).first.sectorId);
00559 //    sumT0ByChamber[chamberId] = sumT0ByChamber[chamberId] + (*tzero).second.t0mean;
00560     int channelId = tzero->channelId;
00561     if ( channelId == 0 ) continue;
00562     DTWireId wireId(channelId);
00563     DTChamberId chamberId(wireId.chamberId());
00564     sumT0ByChamber[chamberId] = sumT0ByChamber[chamberId] + tzero->t0mean;
00565 // @@@ better DTT0 usage
00566     float t0mean_f;
00567     float t0rms_f;
00568     t0s->get(wireId,t0mean_f,t0rms_f,DTTimeUnits::counts);
00569     sumT0ByChamber[chamberId] = sumT0ByChamber[chamberId] + t0mean_f;
00570 // @@@ NEW DTT0 END
00571     countT0ByChamber[chamberId]++;
00572   }
00573 
00574   //Change reference for each wire and store the new t0s in the new map
00575   for(DTT0::const_iterator tzero = t0s->begin();
00576       tzero != t0s->end(); tzero++) {
00577 // @@@ NEW DTT0 FORMAT
00578 //    DTChamberId chamberId((*tzero).first.wheelId,
00579 //                        (*tzero).first.stationId,
00580 //                        (*tzero).first.sectorId);
00581 //    double t0mean = ((*tzero).second.t0mean) - (sumT0ByChamber[chamberId]/countT0ByChamber[chamberId]);
00582 //    double t0rms = (*tzero).second.t0rms;
00583 //    DTWireId wireId((*tzero).first.wheelId,
00584 //                  (*tzero).first.stationId,
00585 //                  (*tzero).first.sectorId,
00586 //                  (*tzero).first.slId,
00587 //                  (*tzero).first.layerId,
00588 //                  (*tzero).first.cellId);
00589     int channelId = tzero->channelId;
00590     if ( channelId == 0 ) continue;
00591     DTWireId wireId( channelId );
00592     DTChamberId chamberId(wireId.chamberId());
00593     double t0mean = (tzero->t0mean) - (sumT0ByChamber[chamberId]/countT0ByChamber[chamberId]);
00594     double t0rms = tzero->t0rms;
00595 // @@@ better DTT0 usage
00596     float t0mean_f;
00597     float t0rms_f;
00598     t0s->get(wireId,t0mean_f,t0rms_f,DTTimeUnits::counts);
00599     t0mean = t0mean_f - (sumT0ByChamber[chamberId]/countT0ByChamber[chamberId]);
00600     t0rms = t0rms_f;
00601 // @@@ NEW DTT0 END
00602     t0sWRTChamber->set(wireId,
00603                        t0mean,
00604                        t0rms,
00605                        DTTimeUnits::counts);
00606     if(debug){
00607       //cout<<"Chamber "<<chamberId<<" has reference "<<(sumT0ByChamber[chamberId]/countT0ByChamber[chamberId]);
00608 //      cout<<"Changing t0 of wire "<<wireId<<" from "<<(*tzero).second.t0mean<<" to "<<t0mean<<endl;
00609       cout<<"Changing t0 of wire "<<wireId<<" from "<<tzero->t0mean<<" to "<<t0mean<<endl;
00610     }
00611   }
00612 
00614   if(debug) 
00615    cout << "[DTT0CalibrationNew]Writing values in DB!" << endl;
00616   // FIXME: to be read from cfg?
00617   string t0Record = "DTT0Rcd";
00618   // Write the t0 map to DB
00619   DTCalibDBUtils::writeToDB(t0Record, t0sWRTChamber);
00620 }
00621 
00622 string DTT0CalibrationNew::getHistoName(const DTWireId& wId) const {
00623   string histoName;
00624   stringstream theStream;
00625   theStream << "Ch_" << wId.wheel() << "_" << wId.station() << "_" << wId.sector()
00626             << "_SL" << wId.superlayer() << "_L" << wId.layer() << "_W"<< wId.wire() <<"_hT0Histo";
00627   theStream >> histoName;
00628   return histoName;
00629 }
00630 
00631 string DTT0CalibrationNew::getHistoName(const DTLayerId& lId) const {
00632   string histoName;
00633   stringstream theStream;
00634   theStream << "Ch_" << lId.wheel() << "_" << lId.station() << "_" << lId.sector()
00635             << "_SL" << lId.superlayer() << "_L" << lId.layer() <<"_hT0Histo";
00636   theStream >> histoName;
00637   return histoName;
00638 }
00639