CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch13/src/DQM/EcalBarrelMonitorTasks/src/EBBeamCaloTask.cc

Go to the documentation of this file.
00001 /*
00002  * \file EBBeamCaloTask.cc
00003  *
00004  * $Date: 2010/08/30 13:14:07 $
00005  * $Revision: 1.80 $
00006  * \author A. Ghezzi
00007  *
00008  */
00009 
00010 #include <iostream>
00011 #include <fstream>
00012 
00013 #include "FWCore/ServiceRegistry/interface/Service.h"
00014 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00015 
00016 #include "DQMServices/Core/interface/MonitorElement.h"
00017 
00018 #include "DQMServices/Core/interface/DQMStore.h"
00019 
00020 #include "DataFormats/EcalRawData/interface/EcalRawDataCollections.h"
00021 #include "DataFormats/EcalDetId/interface/EBDetId.h"
00022 #include "DataFormats/EcalDigi/interface/EBDataFrame.h"
00023 #include "DataFormats/EcalDigi/interface/EcalDigiCollections.h"
00024 #include "DataFormats/EcalRecHit/interface/EcalUncalibratedRecHit.h"
00025 #include "DataFormats/EcalRecHit/interface/EcalRecHitCollections.h"
00026 
00027 #include "TBDataFormats/EcalTBObjects/interface/EcalTBCollections.h"
00028 
00029 #include "DQM/EcalCommon/interface/Numbers.h"
00030 
00031 #include "DQM/EcalBarrelMonitorTasks/interface/EBBeamCaloTask.h"
00032 
00033 EBBeamCaloTask::EBBeamCaloTask(const edm::ParameterSet& ps){
00034 
00035   init_ = false;
00036 
00037   dqmStore_ = edm::Service<DQMStore>().operator->();
00038 
00039   prefixME_ = ps.getUntrackedParameter<std::string>("prefixME", "");
00040 
00041   enableCleanup_ = ps.getUntrackedParameter<bool>("enableCleanup", false);
00042 
00043   mergeRuns_ = ps.getUntrackedParameter<bool>("mergeRuns", false);
00044 
00045   EcalTBEventHeader_ = ps.getParameter<edm::InputTag>("EcalTBEventHeader");
00046   EcalRawDataCollection_ = ps.getParameter<edm::InputTag>("EcalRawDataCollection");
00047   EBDigiCollection_ = ps.getParameter<edm::InputTag>("EBDigiCollection");
00048   EcalUncalibratedRecHitCollection_ = ps.getParameter<edm::InputTag>("EcalUncalibratedRecHitCollection");
00049 
00050   for (int i = 0; i < cryInArray_ ; i++) {
00051     meBBCaloPulseProf_[i]=0;
00052     meBBCaloPulseProfG12_[i]=0;
00053     meBBCaloGains_[i]=0;
00054     meBBCaloEne_[i]=0;
00055 
00056     //meBBCaloPulseProfMoving_[i]=0;
00057     //meBBCaloPulseProfG12Moving_[i]=0;
00058     //meBBCaloGainsMoving_[i]=0;
00059     //meBBCaloEneMoving_[i]=0;
00060   }
00061 
00062   meBBCaloCryRead_ = 0;
00063   //meBBCaloCryReadMoving_ = 0;
00064 
00065   meBBNumCaloCryRead_ = 0;
00066   meBBCaloAllNeededCry_ = 0;
00067 
00068   meBBCaloE3x3_ = 0;
00069   meBBCaloE3x3Moving_ = 0;
00070 
00071   meBBCaloCryOnBeam_ = 0;
00072   meBBCaloMaxEneCry_ = 0;
00073   TableMoving_ = 0;
00074 
00075   CrystalsDone_ = 0;
00076   CrystalInBeam_vs_Event_ = 0;
00077   meEBBCaloReadCryErrors_ = 0;
00078   meEBBCaloE1vsCry_ = 0;
00079   meEBBCaloE3x3vsCry_ = 0;
00080   meEBBCaloEntriesVsCry_ = 0;
00081   meEBBCaloBeamCentered_ = 0;
00082 
00083   meEBBCaloE1MaxCry_ = 0;
00084 //   for(int u=0;u<1701;u++){
00085 //     meBBCaloE3x3Cry_[u]=0;
00086 //     meBBCaloE1Cry_[u]=0;
00087 //   }
00088 
00089   meEBBCaloDesync_ = 0;
00090 
00091 }
00092 
00093 EBBeamCaloTask::~EBBeamCaloTask(){
00094 
00095 }
00096 
00097 void EBBeamCaloTask::beginJob(void){
00098 
00099   ievt_ = 0;
00100 
00101   profileArranged_ = false;
00102 
00103   if ( dqmStore_ ) {
00104     dqmStore_->setCurrentFolder(prefixME_ + "/EBBeamCaloTask");
00105     dqmStore_->rmdir(prefixME_ + "/EBBeamCaloTask");
00106   }
00107 
00108 }
00109 
00110 void EBBeamCaloTask::beginRun(const edm::Run& r, const edm::EventSetup& c) {
00111 
00112   Numbers::initGeometry(c, false);
00113 
00114   if ( ! mergeRuns_ ) this->reset();
00115 
00116 }
00117 
00118 void EBBeamCaloTask::endRun(const edm::Run& r, const edm::EventSetup& c) {
00119 
00120 }
00121 
00122 void EBBeamCaloTask::reset(void) {
00123 
00124     for (int i = 0; i < cryInArray_ ; i++) {
00125       if ( meBBCaloPulseProf_[i] ) meBBCaloPulseProf_[i]->Reset();
00126       if ( meBBCaloPulseProfG12_[i] ) meBBCaloPulseProfG12_[i]->Reset();
00127       if ( meBBCaloGains_[i] ) meBBCaloGains_[i]->Reset();
00128       if ( meBBCaloEne_[i] ) meBBCaloEne_[i]->Reset();
00129 
00130 //       if ( meBBCaloPulseProfMoving_[i] ) meBBCaloPulseProfMoving_[i]->Reset();
00131 //       if ( meBBCaloPulseProfG12Moving_[i] ) meBBCaloPulseProfG12Moving_[i]->Reset();
00132 //       if ( meBBCaloGainsMoving_[i] ) meBBCaloGainsMoving_[i]->Reset();
00133 //       if ( meBBCaloEneMoving_[i] ) meBBCaloEneMoving_[i]->Reset();
00134     }
00135 
00136 //     for(int u=0; u< 1701;u++){
00137 //       if ( meBBCaloE3x3Cry_[u] ) meBBCaloE3x3Cry_[u]->Reset();
00138 //       if ( meBBCaloE1Cry_[u] ) meBBCaloE1Cry_[u]->Reset();
00139 //     }
00140 
00141     if ( meBBCaloCryRead_ ) meBBCaloCryRead_->Reset();
00142 //    if ( meBBCaloCryReadMoving_ ) meBBCaloCryReadMoving_->Reset();
00143     if ( meBBCaloAllNeededCry_ ) meBBCaloAllNeededCry_->Reset();
00144     if ( meBBNumCaloCryRead_ ) meBBNumCaloCryRead_->Reset();
00145     if ( meBBCaloE3x3_ ) meBBCaloE3x3_->Reset();
00146     if ( meBBCaloE3x3Moving_ ) meBBCaloE3x3Moving_->Reset();
00147     if ( meBBCaloCryOnBeam_ ) meBBCaloCryOnBeam_->Reset();
00148     if ( meBBCaloMaxEneCry_ ) meBBCaloMaxEneCry_->Reset();
00149     if ( TableMoving_ ) TableMoving_->Reset();
00150     if ( CrystalsDone_ ) CrystalsDone_->Reset();
00151     if ( CrystalInBeam_vs_Event_ ) CrystalInBeam_vs_Event_->Reset();
00152     if( meEBBCaloReadCryErrors_ ) meEBBCaloReadCryErrors_->Reset();
00153     if( meEBBCaloE1vsCry_ ) meEBBCaloE1vsCry_->Reset();
00154     if( meEBBCaloE3x3vsCry_ ) meEBBCaloE3x3vsCry_->Reset();
00155     if( meEBBCaloEntriesVsCry_ )  meEBBCaloEntriesVsCry_->Reset();
00156     if( meEBBCaloBeamCentered_ ) meEBBCaloBeamCentered_->Reset();
00157     if( meEBBCaloE1MaxCry_ ) meEBBCaloE1MaxCry_->Reset();
00158     if( meEBBCaloDesync_ ) meEBBCaloDesync_->Reset();
00159 
00160 }
00161 
00162 void EBBeamCaloTask::setup(void){
00163 
00164   init_ = true;
00165   profileArranged_= false;
00166   char histo[200];
00167 
00168   PreviousTableStatus_[0]=0;//let's start with stable...
00169   PreviousTableStatus_[1]=0;//let's start with stable...
00170 
00171   PreviousCrystalinBeam_[0] = 0;
00172   PreviousCrystalinBeam_[1] = 0;
00173   PreviousCrystalinBeam_[2] = -1;
00174   // PreviousCrystalinBeam_[2] = -1 is needed to have a correct step vs cry matching
00175   lastStableStatus_=0;
00176   for(int u=0;u<10;u++){cib_[u]=0;}
00177   changed_tb_status_= false;
00178   changed_cry_in_beam_ = false;
00179   evt_after_change_ =0;
00180   wasFakeChange_= false;
00181   table_step_=1;
00182   crystal_step_=1;
00183   event_last_reset_ = 0;
00184   last_cry_in_beam_ = 0;
00185   previous_cry_in_beam_ = 1;
00186 
00187   if ( dqmStore_ ) {
00188     dqmStore_->setCurrentFolder(prefixME_ + "/EBBeamCaloTask");
00189 
00190     for (int i = 0; i < cryInArray_ ; i++) {
00191       sprintf(histo, "EBBCT pulse profile cry %01d", i+1);
00192       //considering the gain the range is 4096*12 ~ 50000
00193       meBBCaloPulseProf_[i] = dqmStore_->bookProfile(histo, histo, 10,0.,10.,50000,0.,50000.,"s");
00194 
00195       sprintf(histo, "EBBCT pulse profile in G12 cry %01d", i+1);
00196       meBBCaloPulseProfG12_[i] = dqmStore_->bookProfile(histo, histo, 10,0.,10.,4096,0.,4096.,"s");
00197       meBBCaloPulseProfG12_[i]->setAxisTitle("#sample", 1);
00198       meBBCaloPulseProfG12_[i]->setAxisTitle("ADC", 2);
00199 
00200       sprintf(histo, "EBBCT found gains cry %01d", i+1);
00201       meBBCaloGains_[i] =  dqmStore_->book1D(histo,histo,14,0.,14.);
00202       meBBCaloGains_[i]->setAxisTitle("gain", 1);
00203       // g1-> bin 2, g6-> bin 7, g12-> bin 13
00204 
00205       sprintf(histo, "EBBCT rec energy cry %01d", i+1);
00206       meBBCaloEne_[i] =  dqmStore_->book1D(histo,histo,500,0.,9000.);
00207       meBBCaloEne_[i]->setAxisTitle("rec ene (ADC)", 1);
00208       //9000 ADC in G12 equivalent is about 330 GeV
00209 
00211 
00212 //       sprintf(histo, "EBBCT pulse profile moving table cry %01d", i+1);
00213 //       //considering the gain the range is 4096*12 ~ 50000
00214 //       meBBCaloPulseProfMoving_[i] = dqmStore_->bookProfile(histo, histo, 10,0.,10.,50000,0.,50000.,"s");
00215 
00216 //       sprintf(histo, "EBBCT pulse profile in G12 moving table cry %01d", i+1);
00217 //       meBBCaloPulseProfG12Moving_[i] = dqmStore_->bookProfile(histo, histo, 10,0.,10.,4096,0.,4096.,"s");
00218 
00219 //       sprintf(histo, "EBBCT found gains moving table cry %01d", i+1);
00220 //       meBBCaloGainsMoving_[i] =  dqmStore_->book1D(histo,histo,14,0.,14.);
00221 //       // g1-> bin 2, g6-> bin 7, g12-> bin 13
00222 
00223 //       sprintf(histo, "EBBCT rec energy moving table cry %01d", i+1);
00224 //       meBBCaloEneMoving_[i] =  dqmStore_->book1D(histo,histo,2000,0.,9000.);
00225 //       //9000 ADC in G12 equivalent is about 330 GeV
00226 
00227     }
00228 
00229 //     dqmStore_->setCurrentFolder(prefixME_ + "/EBBeamCaloTask/EnergyHistos");
00230 //     for(int u=0; u< 1701;u++){
00231 //       sprintf(histo, "EBBCT rec Ene sum 3x3 cry: %04d",u);
00232 //       meBBCaloE3x3Cry_[u] = dqmStore_->book1D(histo,histo,1000,0.,4500.);
00233 
00234 //       sprintf(histo, "EBBCT rec Energy1 cry: %04d",u);
00235 //       meBBCaloE1Cry_[u] = dqmStore_->book1D(histo,histo,1000,0.,4500.);
00236 //     }
00237 
00238 //     dqmStore_->setCurrentFolder(prefixME_ + "/EBBeamCaloTask");
00239     sprintf(histo, "EBBCT readout crystals");
00240     meBBCaloCryRead_ =  dqmStore_->book2D(histo,histo,9,-4.,5.,9,-4.,5.);
00241     //matrix of readout crystal around cry in beam
00242 
00243     //sprintf(histo, "EBBCT readout crystals table moving");
00244     //meBBCaloCryReadMoving_ =  dqmStore_->book2D(histo,histo,9,-4.,5.,9,-4.,5.);
00245     //matrix of readout crystal around cry in beam
00246 
00247     sprintf(histo, "EBBCT all needed crystals readout");
00248     meBBCaloAllNeededCry_ = dqmStore_->book1D(histo,histo,3,-1.,2.);
00249     // not all needed cry are readout-> bin 1, all needed cry are readout-> bin 3
00250 
00251     sprintf(histo, "EBBCT readout crystals number");
00252     meBBNumCaloCryRead_ = dqmStore_->book1D(histo,histo,1701,0.,1701.);
00253     meBBNumCaloCryRead_->setAxisTitle("number of read crystals", 1);
00254 
00255     sprintf(histo, "EBBCT rec Ene sum 3x3");
00256     meBBCaloE3x3_ = dqmStore_->book1D(histo,histo,500,0.,9000.);
00257     meBBCaloE3x3_->setAxisTitle("rec ene (ADC)", 1);
00258     //9000 ADC in G12 equivalent is about 330 GeV
00259 
00260     sprintf(histo, "EBBCT rec Ene sum 3x3 table moving");
00261     meBBCaloE3x3Moving_ = dqmStore_->book1D(histo,histo,500,0.,9000.);
00262     //9000 ADC in G12 equivalent is about 330 GeV
00263 
00264     sprintf(histo, "EBBCT crystal on beam");
00265     meBBCaloCryOnBeam_ = dqmStore_->book2D(histo, histo, 85, 0., 85., 20, 0., 20.);
00266 
00267     sprintf(histo, "EBBCT crystal with maximum rec energy");
00268     meBBCaloMaxEneCry_ = dqmStore_->book2D(histo, histo, 85, 0., 85., 20, 0., 20.);
00269 
00270     sprintf(histo, "EBBCT table is moving");
00271     TableMoving_ = dqmStore_->book1D(histo,histo,2,0.,1.1);
00272     TableMoving_->setAxisTitle("table status (0=stable, 1=moving)", 1);
00273     //table is moving-> bin 2, table is not moving-> bin 1
00274 
00275     sprintf(histo, "EBBCT crystals done");
00276     CrystalsDone_ = dqmStore_->book1D(histo,histo,1700,1.,1701.);
00277     CrystalsDone_->setAxisTitle("crystal", 1);
00278     CrystalsDone_->setAxisTitle("step in the scan", 2);
00279     //for a crystal done the corresponing bin is filled with the step in the
00280     //autoscan pertainig to the given crystales
00281 
00282     sprintf(histo, "EBBCT crystal in beam vs event");
00283     CrystalInBeam_vs_Event_ = dqmStore_->bookProfile(histo, histo, 20000,0.,400000.,1802,-101.,1701.,"s");
00284     CrystalInBeam_vs_Event_->setAxisTitle("event", 1);
00285     CrystalInBeam_vs_Event_->setAxisTitle("crystal in beam", 2);
00286     // 1 bin each 20 events
00287     // when table is moving for just one events fill with -100
00288 
00289     sprintf(histo, "EBBCT readout crystals errors");
00290     meEBBCaloReadCryErrors_ = dqmStore_->book1D(histo, histo, 425,1.,86.);
00291     meEBBCaloReadCryErrors_->setAxisTitle("step in the scan", 1);
00292 
00293     sprintf(histo, "EBBCT average rec energy in the single crystal");
00294     //meEBBCaloE1vsCry_ = dqmStore_->book1D(histo, histo, 85,1.,86.);
00295     meEBBCaloE1vsCry_ = dqmStore_->bookProfile(histo, histo, 1700,1.,1701.,500,0.,9000.,"s");
00296     meEBBCaloE1vsCry_->setAxisTitle("crystal", 1);
00297     meEBBCaloE1vsCry_->setAxisTitle("rec energy (ADC)", 2);
00298 
00299     sprintf(histo, "EBBCT average rec energy in the 3x3 array");
00300     //meEBBCaloE3x3vsCry_= dqmStore_->book1D(histo, histo,85,1.,86.);
00301     meEBBCaloE3x3vsCry_ = dqmStore_->bookProfile(histo, histo, 1700,1.,1701.,500,0.,9000.,"s");
00302     meEBBCaloE3x3vsCry_->setAxisTitle("crystal", 1);
00303     meEBBCaloE3x3vsCry_->setAxisTitle("rec energy (ADC)", 2);
00304 
00305     sprintf(histo, "EBBCT number of entries");
00306     meEBBCaloEntriesVsCry_ = dqmStore_->book1D(histo, histo,1700,1.,1701.);
00307     meEBBCaloEntriesVsCry_->setAxisTitle("crystal", 1);
00308     meEBBCaloEntriesVsCry_->setAxisTitle("number of events (prescaled)", 2);
00309 
00310     sprintf(histo, "EBBCT energy deposition in the 3x3");
00311     meEBBCaloBeamCentered_ = dqmStore_->book2D(histo, histo,3,-1.5,1.5,3,-1.5,1.5);
00312     meEBBCaloBeamCentered_->setAxisTitle("\\Delta \\eta", 1);
00313     meEBBCaloBeamCentered_->setAxisTitle("\\Delta \\phi", 2);
00314 
00315     sprintf(histo, "EBBCT E1 in the max cry");
00316     meEBBCaloE1MaxCry_= dqmStore_->book1D(histo,histo,500,0.,9000.);
00317     meEBBCaloE1MaxCry_->setAxisTitle("rec Ene (ADC)", 1);
00318 
00319     sprintf(histo, "EBBCT Desynchronization vs step");
00320     meEBBCaloDesync_= dqmStore_->book1D(histo, histo, 85 ,1.,86.);
00321     meEBBCaloDesync_->setAxisTitle("step", 1);
00322     meEBBCaloDesync_->setAxisTitle("Desynchronized events", 2);
00323 
00324   }
00325 
00326 }
00327 
00328 void EBBeamCaloTask::cleanup(void){
00329 
00330   if ( ! init_ ) return;
00331 
00332   if ( dqmStore_ ) {
00333     dqmStore_->setCurrentFolder(prefixME_ + "/EBBeamCaloTask");
00334     for (int i = 0; i < cryInArray_ ; i++) {
00335       if ( meBBCaloPulseProf_[i] ) dqmStore_->removeElement( meBBCaloPulseProf_[i]->getName() );
00336       meBBCaloPulseProf_[i] = 0;
00337       if ( meBBCaloPulseProfG12_[i] ) dqmStore_->removeElement( meBBCaloPulseProfG12_[i]->getName() );
00338       meBBCaloPulseProfG12_[i] = 0;
00339       if ( meBBCaloGains_[i] ) dqmStore_->removeElement( meBBCaloGains_[i]->getName() );
00340       meBBCaloGains_[i] = 0;
00341       if ( meBBCaloEne_[i] ) dqmStore_->removeElement( meBBCaloEne_[i]->getName() );
00342       meBBCaloEne_[i] = 0;
00343 
00344 //       if ( meBBCaloPulseProfMoving_[i] ) dqmStore_->removeElement( meBBCaloPulseProfMoving_[i]->getName() );
00345 //       meBBCaloPulseProfMoving_[i] = 0;
00346 //       if ( meBBCaloPulseProfG12Moving_[i] ) dqmStore_->removeElement( meBBCaloPulseProfG12Moving_[i]->getName() );
00347 //       meBBCaloPulseProfG12Moving_[i] = 0;
00348 //       if ( meBBCaloGainsMoving_[i] ) dqmStore_->removeElement( meBBCaloGainsMoving_[i]->getName() );
00349 //       meBBCaloGainsMoving_[i] = 0;
00350 //       if ( meBBCaloEneMoving_[i] ) dqmStore_->removeElement( meBBCaloEneMoving_[i]->getName() );
00351 //       meBBCaloEneMoving_[i] = 0;
00352     }
00353 
00354 //     dqmStore_->setCurrentFolder(prefixME_ + "/EBBeamCaloTask/EnergyHistos");
00355 //     for(int u=0; u< 1701;u++){
00356 //       if ( meBBCaloE3x3Cry_[u] ) dqmStore_->removeElement( meBBCaloE3x3Cry_[u]->getName() );
00357 //       meBBCaloE3x3Cry_[u] = 0;
00358 //       if ( meBBCaloE1Cry_[u] ) dqmStore_->removeElement( meBBCaloE1Cry_[u]->getName() );
00359 //       meBBCaloE1Cry_[u] = 0;
00360 //     }
00361 
00362 //     dqmStore_->setCurrentFolder(prefixME_ + "/EBBeamCaloTask");
00363     if ( meBBCaloCryRead_ ) dqmStore_->removeElement( meBBCaloCryRead_->getName() );
00364     meBBCaloCryRead_ = 0;
00365 //    if ( meBBCaloCryReadMoving_ ) dqmStore_->removeElement( meBBCaloCryReadMoving_->getName() );
00366 //    meBBCaloCryReadMoving_ = 0;
00367     if ( meBBCaloAllNeededCry_ ) dqmStore_->removeElement( meBBCaloAllNeededCry_->getName() );
00368     meBBCaloAllNeededCry_ = 0;
00369     if ( meBBNumCaloCryRead_ ) dqmStore_->removeElement( meBBNumCaloCryRead_->getName() );
00370     meBBNumCaloCryRead_ = 0;
00371     if ( meBBCaloE3x3_ ) dqmStore_->removeElement( meBBCaloE3x3_->getName() );
00372     meBBCaloE3x3_ = 0;
00373     if ( meBBCaloE3x3Moving_ ) dqmStore_->removeElement( meBBCaloE3x3Moving_->getName() );
00374     meBBCaloE3x3Moving_ = 0;
00375     if ( meBBCaloCryOnBeam_ ) dqmStore_->removeElement( meBBCaloCryOnBeam_->getName() );
00376     meBBCaloCryOnBeam_ = 0;
00377     if ( meBBCaloMaxEneCry_ ) dqmStore_->removeElement( meBBCaloMaxEneCry_->getName() );
00378     meBBCaloMaxEneCry_ = 0;
00379     if ( TableMoving_ ) dqmStore_->removeElement( TableMoving_->getName() );
00380     TableMoving_ = 0;
00381     if ( CrystalsDone_ ) dqmStore_->removeElement( CrystalsDone_->getName() );
00382     CrystalsDone_ = 0;
00383     if ( CrystalInBeam_vs_Event_ ) dqmStore_->removeElement( CrystalInBeam_vs_Event_->getName() );
00384     CrystalInBeam_vs_Event_ = 0;
00385     if( meEBBCaloReadCryErrors_ ) dqmStore_->removeElement( meEBBCaloReadCryErrors_->getName() );
00386     meEBBCaloReadCryErrors_ = 0;
00387     if( meEBBCaloE1vsCry_ ) dqmStore_->removeElement( meEBBCaloE1vsCry_->getName() );
00388     meEBBCaloE1vsCry_ = 0;
00389     if( meEBBCaloE3x3vsCry_ ) dqmStore_->removeElement( meEBBCaloE3x3vsCry_->getName() );
00390     meEBBCaloE3x3vsCry_ = 0;
00391     if( meEBBCaloEntriesVsCry_ )  dqmStore_->removeElement( meEBBCaloEntriesVsCry_->getName() );
00392     meEBBCaloEntriesVsCry_ = 0;
00393     if( meEBBCaloBeamCentered_ ) dqmStore_->removeElement( meEBBCaloBeamCentered_->getName() );
00394     meEBBCaloBeamCentered_ = 0;
00395     if( meEBBCaloE1MaxCry_ ) dqmStore_->removeElement(meEBBCaloE1MaxCry_->getName() );
00396     meEBBCaloE1MaxCry_ = 0;
00397     if( meEBBCaloDesync_ ) dqmStore_->removeElement(meEBBCaloDesync_->getName() );
00398     meEBBCaloDesync_ = 0;
00399   }
00400 
00401   init_ = false;
00402 
00403 }
00404 
00405 void EBBeamCaloTask::endJob(void){
00406 
00407   edm::LogInfo("EBBeamCaloTask") << "analyzed " << ievt_ << " events";
00408 
00409   if ( enableCleanup_ ) this->cleanup();
00410 
00411 }
00412 
00413 void EBBeamCaloTask::analyze(const edm::Event& e, const edm::EventSetup& c){
00414 
00415   bool enable = false;
00416 
00417   edm::Handle<EcalRawDataCollection> dcchs;
00418 
00419   if ( e.getByLabel(EcalRawDataCollection_, dcchs) ) {
00420 
00421     for ( EcalRawDataCollection::const_iterator dcchItr = dcchs->begin(); dcchItr != dcchs->end(); ++dcchItr ) {
00422 
00423       if ( Numbers::subDet( *dcchItr ) != EcalBarrel ) continue;
00424 
00425       if ( dcchItr->getRunType() == EcalDCCHeaderBlock::BEAMH4 ||
00426            dcchItr->getRunType() == EcalDCCHeaderBlock::BEAMH2 ) enable = true;
00427 
00428     }
00429 
00430   } else {
00431     edm::LogWarning("EBBeamCaloTask") << EcalRawDataCollection_ << " not available";
00432   }
00433 
00434   if ( ! enable ) return;
00435   if ( ! init_ ) this->setup();
00436   ievt_++;
00437 
00438   edm::Handle<EcalTBEventHeader> pEventHeader;
00439   const EcalTBEventHeader* evtHeader=0;
00440 
00441   if ( e.getByLabel(EcalTBEventHeader_, pEventHeader) ) {
00442     evtHeader = pEventHeader.product(); // get a ptr to the product
00443   } else {
00444     std::cerr << "Error! can't get the product for the event header" << std::endl;
00445   }
00446 
00447   //FIX ME, in the task we should use LV1 instead of ievt_ (prescaling)
00448   int cry_in_beam = 0;
00449   bool tb_moving = false;//just for test, to be filled with info from the event
00450   int event = 0;
00451 
00452   if(evtHeader){
00453     //cry_in_beam = evtHeader->nominalCrystalInBeam();
00454     cry_in_beam = evtHeader->crystalInBeam();
00455     tb_moving = evtHeader->tableIsMoving();
00456     event = evtHeader->eventNumber();
00457     if( evtHeader->syncError() ) {meEBBCaloDesync_->Fill(crystal_step_);}
00458   }
00459   else {
00460     cry_in_beam =   previous_cry_in_beam_;
00461     tb_moving = lastStableStatus_;
00462     event = previous_ev_num_ +10;
00463   }
00464 
00465   previous_cry_in_beam_ = cry_in_beam;
00466   previous_ev_num_ = event;
00467 
00468   //cry_in_beam = 702;//just for test, to be filled with info from the event
00469 
00470   bool reset_histos_stable = false;
00471   bool reset_histos_moving = false;
00472 
00473   bool skip_this_event = false;
00474 
00475 //   if(ievt_ > 500){tb_moving=true; }
00476 //   if(ievt_ > 1000){tb_moving=false; cry_in_beam = 703;}
00477 //   if(ievt_ > 2000){tb_moving=true; }
00478 //   if(ievt_ > 2500){tb_moving=false; cry_in_beam = 704;}
00479 //   if(ievt_ == 3000){cry_in_beam = 702;}
00480 //   if(ievt_ == 3001){cry_in_beam = 703;}
00481 //   if(ievt_ > 3500){tb_moving=true; }
00482 
00483 //   if(ievt_ > 3300){tb_moving=true; }
00484 //   if(ievt_ > 6100){tb_moving=false; cry_in_beam = 705;}
00485 //   if(ievt_ == 6201){tb_moving=true; }
00486 //   if(ievt_ > 9000){tb_moving=true; }
00487 //   if(ievt_ == 11021){tb_moving=false; }
00488 //   if(ievt_ > 12100){tb_moving=false; cry_in_beam = 706;}
00489 //   if(ievt_ > 15320){tb_moving=true; }
00490 //   if(ievt_ > 15500){tb_moving=false; cry_in_beam = 707;}
00491 
00492 
00493  //  //if(ievt_ > 20){tb_moving=true; }
00494 //   //  if(ievt_ == 23){tb_moving=true; }
00495 //   if(ievt_ > 50){tb_moving=false; cry_in_beam = 705;}
00496 //   //if(ievt_ > 90 ){tb_moving=true; }
00497 //   if(ievt_ == 65){tb_moving=false; }
00498 //   if(ievt_ > 110){tb_moving=false; cry_in_beam = 706;}
00499 //   if(ievt_ == 116){cry_in_beam = 709;}
00500 //   //if(ievt_ > 115){tb_moving=true; }
00501 //   if(ievt_ > 150){tb_moving=false; cry_in_beam = 707;}
00502 
00503   //   #include "DQM/EcalBarrelMonitorTasks/interface/cry_in_beam_run_ecs73214.h"
00504 
00505   if(ievt_ < 3){last_cry_in_beam_ = cry_in_beam;}
00506 
00507   if(tb_moving){
00508 
00509     TableMoving_->Fill(1);
00510     if( PreviousTableStatus_[0] == 0 &&  PreviousTableStatus_[1] == 1 && lastStableStatus_ == 0){
00511       reset_histos_moving=true;
00512       wasFakeChange_ = false;
00513       // ! Warning! This works in the assumption that the crystal in beam stay the same
00514       // while the tb is moving and is set to the new one only when the table
00515       // reaches the new position
00516       lastStableStatus_ = 1;
00517 
00518     }
00519     else if( PreviousTableStatus_[1] == 0) {
00520       skip_this_event=true;
00521       changed_tb_status_ = true;
00522       wasFakeChange_ = true;
00523     }
00524     // just skip the first event when the table change status
00525     PreviousTableStatus_[0] = PreviousTableStatus_[1];
00526     PreviousTableStatus_[1] = 1;
00527   }//end of if(tb_moving)
00528 
00529   else {// table is not moving
00530 
00531     TableMoving_->Fill(0);
00532     if( PreviousTableStatus_[0] == 1 &&  PreviousTableStatus_[1] == 0 && lastStableStatus_ == 1){
00533       //reset_histos_stable = true;
00534       wasFakeChange_ = false;
00535       lastStableStatus_ = 0;
00536     }
00537     else if(PreviousTableStatus_[1] == 1) {
00538       skip_this_event=true;
00539       changed_tb_status_ = true;
00540       wasFakeChange_ = true;
00541     }
00542     // just skip the first event when the table change status
00543     PreviousTableStatus_[0]=PreviousTableStatus_[1];
00544     PreviousTableStatus_[1]=0;
00545 
00546     // check also whether cry in beam  has changed
00547     if(  PreviousCrystalinBeam_[0] == PreviousCrystalinBeam_[1]  &&   PreviousCrystalinBeam_[1] != PreviousCrystalinBeam_[2] && PreviousCrystalinBeam_[2] == cry_in_beam ){
00548       reset_histos_stable=true;
00549       wasFakeChange_ = false;
00550     }
00551     else if (PreviousCrystalinBeam_[2] != cry_in_beam){
00552       changed_cry_in_beam_ = true;
00553       skip_this_event=true;
00554       wasFakeChange_ = true;
00555     }
00556     // }
00557 
00558     PreviousCrystalinBeam_[0] = PreviousCrystalinBeam_[1];
00559     PreviousCrystalinBeam_[1] = PreviousCrystalinBeam_[2];
00560     PreviousCrystalinBeam_[2] =  cry_in_beam;
00561   }
00562   //if (! changed_tb_status_ && ! changed_cry_in_beam_ ){// standard data taking
00563 
00564     if( !tb_moving ) {CrystalInBeam_vs_Event_->Fill(event,float(cry_in_beam));}
00565     else{CrystalInBeam_vs_Event_->Fill(event,-100); }
00566     if ( !profileArranged_ ){
00567       float dd=0;
00568       int mbin =0;
00569       for( int bin=1; bin < 20001; bin++ ){
00570         float temp = CrystalInBeam_vs_Event_->getBinContent(bin);
00571         if(temp>0){ dd= temp+0.01; mbin=bin; break;}
00572       }
00573       if(mbin >0) { CrystalInBeam_vs_Event_->Fill(20*mbin-1,dd);}
00574       profileArranged_ = true;
00575     }
00576 
00577     // }
00578 //   else{ // here there is either a step or a daq error
00579 //     // keep 10 events in a buffer waiting to decide for the step or the error
00580 //     if(tb_moving){cib_[evt_after_change_]=-100;}
00581 //     else {cib_[evt_after_change_]=cry_in_beam;}
00582 
00583 //     if(evt_after_change_ >= 9){
00584 //       evt_after_change_ =0;
00585 //       if(wasFakeChange_){// here is an error: put the 10 events in the profile
00586 //      for(int u=0; u<10; u++){
00587 //        CrystalInBeam_vs_Event_->Fill(ievt_-9+u , cib_[u]);
00588 //      }
00589 //       }
00590 //       //for a real change just skip the first 10 events after change
00591 //       changed_tb_status_=false;
00592 //       changed_cry_in_beam_ = false;
00593 //     }
00594 //     else{evt_after_change_ ++;}
00595 //   }
00596 
00597   if(reset_histos_moving){
00598     edm::LogInfo("EBBeamCaloTask") << "event " << ievt_ << " resetting histos for moving table!! ";
00599 
00600     //     meEBBCaloE1vsCry_->setBinContent(crystal_step_ , meBBCaloEne_[4]->getMean() );
00601     //     meEBBCaloE1vsCry_->setBinError(crystal_step_ , meBBCaloEne_[4]->getRMS() );
00602     //     meEBBCaloE3x3vsCry_->setBinContent(crystal_step_ , meBBCaloE3x3_->getMean() );
00603     //     meEBBCaloE3x3vsCry_->setBinError(crystal_step_ , meBBCaloE3x3_->getRMS() );
00604 
00605     //     meEBBCaloEntriesVsCry_->setBinContent(crystal_step_ ,  meBBCaloE3x3_->getEntries() );
00606 
00607     table_step_++;
00608 
00609 
00610     //here the follwowing histos should be reset
00611     // for (int u=0;u<cryInArray_;u++){
00612     //  meBBCaloPulseProfMoving_[u]->Reset();
00613     //  meBBCaloPulseProfG12Moving_[u]->Reset();
00614     //  meBBCaloGainsMoving_[u]->Reset();
00615     //  meBBCaloEneMoving_[u]->Reset();
00616     // }
00617     // meBBCaloCryReadMoving_->Reset();
00618     meBBCaloE3x3Moving_->Reset();
00619 
00620   }
00621 
00622 
00623   if(reset_histos_stable){
00624     if( event - event_last_reset_ > 30){//to be tuned, to avoid a double reset for the change in the table status and
00625                                         //in the crystal in beam. This works ONLY if the crystal in beam stay the same
00626                                         // while the table is moving.
00627                                         //One can also think to remove the reset of the histograms when the table change
00628                                         // status from moving to stable, and to leave the reset only if the cry_in_beam changes.
00629 
00630       edm::LogInfo("EBBeamCaloTask") << "event " << ievt_ << " resetting histos for stable table!! ";
00631 
00632       //       meEBBCaloE1vsCry_->setBinContent(crystal_step_ , meBBCaloEne_[4]->getMean() );
00633       //       meEBBCaloE1vsCry_->setBinError(crystal_step_ , meBBCaloEne_[4]->getRMS() );
00634       //       meEBBCaloE3x3vsCry_->setBinContent(crystal_step_ , meBBCaloE3x3_->getMean() );
00635       //       meEBBCaloE3x3vsCry_->setBinError(crystal_step_ , meBBCaloE3x3_->getRMS() );
00636       //       //
00637       //       meEBBCaloEntriesVsCry_->setBinContent(crystal_step_ ,  meBBCaloE3x3_->getEntries() );
00638 
00639       event_last_reset_ = event;
00640 
00641       last_cry_in_beam_ = cry_in_beam;
00642       crystal_step_++;
00643 
00644       //here the follwowing histos should be reset
00645       for (int u=0;u<cryInArray_;u++){
00646         meBBCaloPulseProf_[u]->Reset();
00647         meBBCaloPulseProfG12_[u]->Reset();
00648         meBBCaloGains_[u]->Reset();
00649         meBBCaloEne_[u]->Reset();
00650       }
00651       meBBCaloCryRead_->Reset();
00652       meBBCaloE3x3_->Reset();
00653       meEBBCaloBeamCentered_->Reset();
00654     }
00655   }
00656 
00657  if(skip_this_event){
00658    edm::LogInfo("EBBeamCaloTask") << "event " << event <<" analyzed: "<<ievt_ << " : skipping this event!! ";
00659    return;}
00660 
00661  // now CrystalsDone_ contains the crystal on beam at the beginning fo a new step, and not when it has finished !!
00662  // <5 just to avoid that we skip the event just after the reset and we do not set CrystalsDone_ .
00663  // if( ievt_ - event_last_reset_ < 5){ CrystalsDone_->setBinContent(cry_in_beam , crystal_step_ );}
00664  CrystalsDone_->setBinContent(cry_in_beam , crystal_step_ );
00665   int eta_c = ( cry_in_beam-1)/20 ;
00666   int phi_c = ( cry_in_beam-1)%20 ;
00667 
00668   float xie = eta_c + 0.5;
00669   float xip = phi_c + 0.5;
00670   if (!tb_moving) {meBBCaloCryOnBeam_->Fill(xie,xip);}
00671 
00672   edm::Handle<EBDigiCollection> digis;
00673   e.getByLabel(EBDigiCollection_, digis);
00674   int nebd = digis->size();
00675 
00676   meBBNumCaloCryRead_->Fill(nebd);
00677 
00678   //matrix 7x7 around cry in beam
00679   int cry_to_beRead[49]; //0 or -1 for non existing crystals (eg 1702)
00680   for(int u=0;u<49;u++){cry_to_beRead[u]=0;}
00681   // chech that all the crystals in the 7x7 exist
00682   for(int de=-3; de<4; de++){
00683     for(int dp=-3; dp<4; dp++){
00684       //int cry_num = (phi_c+dp) + 20*(eta_c+de) +1;
00685       int u = de -7*dp + 24;
00686       bool existing_cry = (phi_c+dp) >= 0 && (phi_c+dp) <= 19 && (eta_c+de) >=0 && (eta_c+de) <= 84;
00687       if(!existing_cry){cry_to_beRead[u]=-1;}
00688     }
00689   }
00690 
00691 
00692   meEBBCaloEntriesVsCry_->Fill(cry_in_beam);
00693 
00694   for ( EBDigiCollection::const_iterator digiItr = digis->begin(); digiItr != digis->end(); ++digiItr ) {
00695 
00696     EBDetId id = digiItr->id();
00697 
00698     int ic = id.ic();
00699     int ie = (ic-1)/20;
00700     int ip = (ic-1)%20;
00701 
00702     int deta_c= ie - eta_c;
00703     int dphi_c= ip - phi_c;
00704     if (! tb_moving){meBBCaloCryRead_->Fill(deta_c, dphi_c);}
00705     //else {meBBCaloCryReadMoving_->Fill(deta_c, dphi_c);}
00706 
00707     if(std::abs(deta_c) > 3 || std::abs(dphi_c) > 3){continue;}
00708     int i_toBeRead = deta_c -7*dphi_c + 24;
00709     if( i_toBeRead > -1 &&  i_toBeRead <49){
00710       cry_to_beRead[i_toBeRead]++;
00711       //if( (ievt_ == 4000 || ievt_ == 13000 || ievt_ == 13002 ) &&   i_toBeRead == 5){ cry_to_beRead[i_toBeRead] -=1;}
00712     }
00713 
00714     if(std::abs(deta_c) > 1 || std::abs(dphi_c) > 1){continue;}
00715     int i_in_array = deta_c -3*dphi_c + 4;
00716 
00717     if( i_in_array < 0 || i_in_array > 8 ){continue;}
00718 
00719     EBDataFrame dataframe = (*digiItr);
00720 
00721     for (int i = 0; i < 10; i++) {
00722       int adc = dataframe.sample(i).adc();
00723       int gainid = dataframe.sample(i).gainId();
00724       //if( (ievt_ == 15400 || ievt_ == 15600 || ievt_ == 15700 ) &&   i_in_array == 4 && i == 4){ gainid =2;}
00725       //if( (ievt_ == 15400 || ievt_ == 15600 || ievt_ == 15700 ) &&   i_in_array == 6 && i == 6){ gainid =3;}
00726 
00727       if ( gainid == 1 ){// gain 12
00728         if(! tb_moving){
00729           meBBCaloPulseProfG12_[i_in_array]->Fill(i,float(adc));
00730           meBBCaloPulseProf_[i_in_array]->Fill(i,float(adc));
00731           meBBCaloGains_[i_in_array]->Fill(12);
00732         }
00733         //else{
00734         //  meBBCaloPulseProfG12Moving_[i_in_array]->Fill(i,float(adc));
00735         //  meBBCaloPulseProfMoving_[i_in_array]->Fill(i,float(adc));
00736         //  meBBCaloGainsMoving_[i_in_array]->Fill(12);
00737         //}
00738       }
00739       else if ( gainid == 2 ){// gain 6
00740         float val = (float(adc)-defaultPede_)*2 + defaultPede_;
00741         if(! tb_moving){
00742           meBBCaloPulseProf_[i_in_array]->Fill(i,val);
00743           meBBCaloGains_[i_in_array]->Fill(6);
00744         }
00745         //else{
00746         //  meBBCaloPulseProfMoving_[i_in_array]->Fill(i,val);
00747         //  meBBCaloGainsMoving_[i_in_array]->Fill(6);
00748         //}
00749       }
00750       else if ( gainid == 3 ){// gain 1
00751         float val = (float(adc)-defaultPede_)*12 + defaultPede_;
00752         if(! tb_moving){
00753         meBBCaloPulseProf_[i_in_array]->Fill(i,val);
00754         meBBCaloGains_[i_in_array]->Fill(1);
00755         }
00756         //else{
00757         //meBBCaloPulseProfMoving_[i_in_array]->Fill(i,val);
00758         //meBBCaloGainsMoving_[i_in_array]->Fill(1);
00759         //}
00760       }
00761     }// end of loop over samples
00762   }// end of loop over digis
00763 
00764   //now  if everything was correct cry_to_beRead should be filled with 1 or -1 but not 0
00765   bool all_cry_readout = true;
00766 
00767   // if( ievt_ == 4000 || ievt_ == 13000 || ievt_ == 13002 ) {all_cry_readout = false;}
00768   if(all_cry_readout){ meBBCaloAllNeededCry_->Fill(1.5);}//bin3
00769   else {
00770     meBBCaloAllNeededCry_->Fill(-0.5);//bin1
00771     if( tb_moving ) {meEBBCaloReadCryErrors_->Fill( crystal_step_+0.5 );}
00772     else {meEBBCaloReadCryErrors_->Fill( crystal_step_ );}
00773   }
00774 
00775   //the part involving rechits
00776 
00777   edm::Handle<EcalUncalibratedRecHitCollection> hits;
00778   e.getByLabel(EcalUncalibratedRecHitCollection_, hits);
00779   int neh = hits->size();
00780   LogDebug("EBBeamCaloTask") << "event " << event <<" analyzed: "<< ievt_ << " hits collection size " << neh;
00781   float ene3x3=0;
00782   float maxEne = 0;
00783   int ieM =-1, ipM = -1;//for the crystal with maximum energy deposition
00784   float cryInBeamEne =0;
00785   for ( EcalUncalibratedRecHitCollection::const_iterator hitItr = hits->begin(); hitItr != hits->end(); ++hitItr ) {
00786 
00787     EBDetId id = hitItr->id();
00788 
00789     int ic = id.ic();
00790     int ie = (ic-1)/20;
00791     int ip = (ic-1)%20;
00792 
00793     int deta_c= ie - eta_c;
00794     int dphi_c= ip - phi_c;
00795 
00796     int i_in_array = deta_c -3*dphi_c + 4;
00797 
00798     float R_ene = hitItr->amplitude();
00799     if ( R_ene <= 0. ) R_ene = 0.0;
00800     if(R_ene > maxEne){
00801       maxEne=R_ene;
00802       ieM =ie; ipM = ip;
00803     }
00804     if(std::abs(deta_c) > 1 || std::abs(dphi_c) > 1){continue;}
00805     meEBBCaloBeamCentered_->Fill(deta_c,dphi_c,R_ene);
00806 
00807     if( i_in_array < 0 || i_in_array > 8 ){continue;}
00808 
00809     if(i_in_array == 4){cryInBeamEne = R_ene;}
00810     if(! tb_moving){meBBCaloEne_[i_in_array]->Fill(R_ene);}
00811     //else{meBBCaloEneMoving_[i_in_array]->Fill(R_ene);}
00812     ene3x3 += R_ene;
00813 
00814   }//end of loop over rechits
00815 
00816   if (!tb_moving){
00817     meBBCaloE3x3_->Fill(ene3x3);
00818     meEBBCaloE1vsCry_->Fill(cry_in_beam , cryInBeamEne );
00819     meEBBCaloE3x3vsCry_->Fill(cry_in_beam, ene3x3 );
00820     //     if( cry_in_beam > 0 && cry_in_beam < 1701){
00821     //       meBBCaloE3x3Cry_[cry_in_beam]->Fill(ene3x3);
00822     //       meBBCaloE1Cry_[cry_in_beam]->Fill(cryInBeamEne);
00823     //     }
00824     meBBCaloMaxEneCry_->Fill(ieM,ipM);
00825     meEBBCaloE1MaxCry_->Fill(maxEne);
00826   }
00827   else{meBBCaloE3x3Moving_->Fill(ene3x3);}
00829 }
00830