CMS 3D CMS Logo

/data/git/CMSSW_5_3_11_patch5/src/Validation/EcalDigis/src/EcalMixingModuleValidation.cc

Go to the documentation of this file.
00001 /*
00002  * \file EcalMixingModuleValidation.cc
00003  *
00004  * $Date: 2010/03/29 01:30:40 $
00005  * $Revision: 1.26 $
00006  * \author F. Cossutti
00007  *
00008 */
00009 
00010 #include <Validation/EcalDigis/interface/EcalMixingModuleValidation.h>
00011 #include <DataFormats/EcalDetId/interface/EBDetId.h>
00012 #include <DataFormats/EcalDetId/interface/EEDetId.h>
00013 #include <DataFormats/EcalDetId/interface/ESDetId.h>
00014 #include "CalibCalorimetry/EcalTrivialCondModules/interface/EcalTrivialConditionRetriever.h"
00015 #include "Geometry/Records/interface/CaloGeometryRecord.h"
00016 #include "DataFormats/EcalDigi/interface/EcalDataFrame.h"
00017 #include "DQMServices/Core/interface/DQMStore.h"
00018 #include "DQMServices/Core/interface/MonitorElement.h"
00019 
00020 //using namespace cms;
00021 //using namespace edm;
00022 //using namespace std;
00023 
00024 EcalMixingModuleValidation::EcalMixingModuleValidation(const edm::ParameterSet& ps):
00025   HepMCLabel(ps.getParameter<std::string>("moduleLabelMC")),
00026   hitsProducer_(ps.getParameter<std::string>("hitsProducer")),
00027   EBdigiCollection_(ps.getParameter<edm::InputTag>("EBdigiCollection")),
00028   EEdigiCollection_(ps.getParameter<edm::InputTag>("EEdigiCollection")),
00029   ESdigiCollection_(ps.getParameter<edm::InputTag>("ESdigiCollection")){
00030 
00031 
00032   // needed for MixingModule checks
00033 
00034   double simHitToPhotoelectronsBarrel = ps.getParameter<double>("simHitToPhotoelectronsBarrel");
00035   double simHitToPhotoelectronsEndcap = ps.getParameter<double>("simHitToPhotoelectronsEndcap");
00036   double photoelectronsToAnalogBarrel = ps.getParameter<double>("photoelectronsToAnalogBarrel");
00037   double photoelectronsToAnalogEndcap = ps.getParameter<double>("photoelectronsToAnalogEndcap");
00038   double samplingFactor = ps.getParameter<double>("samplingFactor");
00039   double timePhase = ps.getParameter<double>("timePhase");
00040   int readoutFrameSize = ps.getParameter<int>("readoutFrameSize");
00041   int binOfMaximum = ps.getParameter<int>("binOfMaximum");
00042   bool doPhotostatistics = ps.getParameter<bool>("doPhotostatistics");
00043   bool syncPhase = ps.getParameter<bool>("syncPhase");
00044 
00045   doPhotostatistics = false;
00046     
00047   theParameterMap = new EcalSimParameterMap(simHitToPhotoelectronsBarrel, simHitToPhotoelectronsEndcap, 
00048                                             photoelectronsToAnalogBarrel, photoelectronsToAnalogEndcap, 
00049                                             samplingFactor, timePhase, readoutFrameSize, binOfMaximum,
00050                                             doPhotostatistics, syncPhase);
00051   //theEcalShape = new EcalShape(timePhase);
00052 
00053   //theEcalResponse = new CaloHitResponse(theParameterMap, theEcalShape);
00054 
00055 /*
00056   int ESGain = ps.getParameter<int>("ESGain");
00057   double ESNoiseSigma = ps.getParameter<double> ("ESNoiseSigma");
00058   int ESBaseline = ps.getParameter<int>("ESBaseline");
00059   double ESMIPADC = ps.getParameter<double>("ESMIPADC");
00060   double ESMIPkeV = ps.getParameter<double>("ESMIPkeV");
00061 */
00062 
00063   theESShape = new ESShape();
00064   theEBShape = new EBShape(); 
00065   theEEShape = new EEShape(); 
00066 
00067   theESResponse = new CaloHitResponse(theParameterMap, theESShape);
00068   theEBResponse = new CaloHitResponse(theParameterMap, theEBShape);
00069   theEEResponse = new CaloHitResponse(theParameterMap, theEEShape);
00070 
00071 //  double effwei = 1.;
00072 
00073 /*
00074   if (ESGain == 0)
00075     effwei = 1.45;
00076   else if (ESGain == 1)
00077     effwei = 0.9066;
00078   else if (ESGain == 2)
00079     effwei = 0.8815;
00080  
00081   esBaseline_ = (double)ESBaseline;
00082   esADCtokeV_ = 1000000.*ESMIPADC/ESMIPkeV;
00083   esThreshold_ = 3.*effwei*ESNoiseSigma/esADCtokeV_;
00084 */
00085   theMinBunch = -10;
00086   theMaxBunch = 10;
00087     
00088  
00089   // verbosity switch
00090   verbose_ = ps.getUntrackedParameter<bool>("verbose", false);
00091                                                                                                                                            
00092   dbe_ = 0;
00093                                                                                                                                           
00094   // get hold of back-end interface
00095   dbe_ = edm::Service<DQMStore>().operator->();
00096                                                                                                                                           
00097   if ( dbe_ ) {
00098     if ( verbose_ ) {
00099       dbe_->setVerbose(1);
00100     } else {
00101       dbe_->setVerbose(0);
00102     }
00103   }
00104                                                                                                                                           
00105   if ( dbe_ ) {
00106     if ( verbose_ ) dbe_->showDirStructure();
00107   }
00108 
00109   gainConv_[1] = 1.;
00110   gainConv_[2] = 2.;
00111   gainConv_[3] = 12.;
00112   gainConv_[0] = 12.;
00113   barrelADCtoGeV_ = 0.035;
00114   endcapADCtoGeV_ = 0.06;
00115  
00116   meEBDigiMixRatiogt100ADC_ = 0;
00117   meEEDigiMixRatiogt100ADC_ = 0;
00118     
00119   meEBDigiMixRatioOriggt50pc_ = 0;
00120   meEEDigiMixRatioOriggt40pc_ = 0;
00121     
00122   meEBbunchCrossing_ = 0;
00123   meEEbunchCrossing_ = 0;
00124   meESbunchCrossing_ = 0;
00125     
00126   for ( int i = 0 ; i < nBunch ; i++ ) {
00127     meEBBunchShape_[i] = 0;
00128     meEEBunchShape_[i] = 0;
00129     meESBunchShape_[i] = 0;
00130   }
00131 
00132   meEBShape_ = 0;
00133   meEEShape_ = 0;
00134   meESShape_ = 0;
00135 
00136   meEBShapeRatio_ = 0;
00137   meEEShapeRatio_ = 0;
00138   meESShapeRatio_ = 0;
00139     
00140 
00141   Char_t histo[200];
00142  
00143   
00144   if ( dbe_ ) {
00145     dbe_->setCurrentFolder("EcalDigisV/EcalDigiTask");
00146   
00147     sprintf (histo, "EcalDigiTask Barrel maximum Digi over sim signal ratio gt 100 ADC" ) ;
00148     meEBDigiMixRatiogt100ADC_ = dbe_->book1D(histo, histo, 200, 0., 100.) ;
00149       
00150     sprintf (histo, "EcalDigiTask Endcap maximum Digi over sim signal ratio gt 100 ADC" ) ;
00151     meEEDigiMixRatiogt100ADC_ = dbe_->book1D(histo, histo, 200, 0., 100.) ;
00152       
00153     sprintf (histo, "EcalDigiTask Barrel maximum Digi over sim signal ratio signal gt 50pc gun" ) ;
00154     meEBDigiMixRatioOriggt50pc_ = dbe_->book1D(histo, histo, 200, 0., 100.) ;
00155       
00156     sprintf (histo, "EcalDigiTask Endcap maximum Digi over sim signal ratio signal gt 40pc gun" ) ;
00157     meEEDigiMixRatioOriggt40pc_ = dbe_->book1D(histo, histo, 200, 0., 100.) ;
00158       
00159     sprintf (histo, "EcalDigiTask Barrel bunch crossing" ) ;
00160     meEBbunchCrossing_ = dbe_->book1D(histo, histo, 20, -10., 10.) ;
00161       
00162     sprintf (histo, "EcalDigiTask Endcap bunch crossing" ) ;
00163     meEEbunchCrossing_ = dbe_->book1D(histo, histo, 20, -10., 10.) ;
00164       
00165     sprintf (histo, "EcalDigiTask Preshower bunch crossing" ) ;
00166     meESbunchCrossing_ = dbe_->book1D(histo, histo, 20, -10., 10.) ;
00167 
00168     for ( int i = 0 ; i < nBunch ; i++ ) {
00169 
00170       sprintf (histo, "EcalDigiTask Barrel shape bunch crossing %02d", i-10 );
00171       meEBBunchShape_[i] = dbe_->bookProfile(histo, histo, 10, 0, 10, 4000, 0., 400.);
00172 
00173       sprintf (histo, "EcalDigiTask Endcap shape bunch crossing %02d", i-10 );
00174       meEEBunchShape_[i] = dbe_->bookProfile(histo, histo, 10, 0, 10, 4000, 0., 400.);
00175 
00176       sprintf (histo, "EcalDigiTask Preshower shape bunch crossing %02d", i-10 );
00177       meESBunchShape_[i] = dbe_->bookProfile(histo, histo, 3, 0, 3, 4000, 0., 400.);
00178 
00179     }
00180 
00181     sprintf (histo, "EcalDigiTask Barrel shape digi");
00182     meEBShape_ = dbe_->bookProfile(histo, histo, 10, 0, 10, 4000, 0., 2000.);
00183 
00184     sprintf (histo, "EcalDigiTask Endcap shape digi");
00185     meEEShape_ = dbe_->bookProfile(histo, histo, 10, 0, 10, 4000, 0., 2000.);
00186 
00187     sprintf (histo, "EcalDigiTask Preshower shape digi");
00188     meESShape_ = dbe_->bookProfile(histo, histo, 3, 0, 3, 4000, 0., 2000.);
00189 
00190     sprintf (histo, "EcalDigiTask Barrel shape digi ratio");
00191     meEBShapeRatio_ = dbe_->book1D(histo, histo, 10, 0, 10.);
00192 
00193     sprintf (histo, "EcalDigiTask Endcap shape digi ratio");
00194     meEEShapeRatio_ = dbe_->book1D(histo, histo, 10, 0, 10.);
00195 
00196     sprintf (histo, "EcalDigiTask Preshower shape digi ratio");
00197     meESShapeRatio_ = dbe_->book1D(histo, histo, 3, 0, 3.);
00198      
00199   }
00200  
00201 }
00202 
00203 EcalMixingModuleValidation::~EcalMixingModuleValidation(){}
00204 
00205 void EcalMixingModuleValidation::beginRun(edm::Run const &, edm::EventSetup const & c){
00206 
00207   checkCalibrations(c);
00208 
00209 }
00210 
00211 void EcalMixingModuleValidation::endJob(){
00212 }
00213 
00214 void EcalMixingModuleValidation::endRun(const edm::Run& run, const edm::EventSetup& c){
00215 
00216   // add shapes for each bunch crossing and divide the digi by the result
00217   
00218   std::vector<MonitorElement *> theBunches;
00219   for ( int i = 0 ; i < nBunch ; i++ ) {
00220     theBunches.push_back(meEBBunchShape_[i]);
00221   }
00222   bunchSumTest(theBunches , meEBShape_ , meEBShapeRatio_ , EcalDataFrame::MAXSAMPLES);
00223   
00224   theBunches.clear();
00225   
00226   for ( int i = 0 ; i < nBunch ; i++ ) {
00227     theBunches.push_back(meEEBunchShape_[i]);
00228   }
00229   bunchSumTest(theBunches , meEEShape_ , meEEShapeRatio_ , EcalDataFrame::MAXSAMPLES);
00230   
00231   theBunches.clear();
00232   
00233   for ( int i = 0 ; i < nBunch ; i++ ) {
00234     theBunches.push_back(meESBunchShape_[i]);
00235   }
00236   bunchSumTest(theBunches , meESShape_ , meESShapeRatio_ , ESDataFrame::MAXSAMPLES);
00237   
00238 }
00239 
00240 void EcalMixingModuleValidation::bunchSumTest(std::vector<MonitorElement *> & theBunches, MonitorElement* & theTotal, MonitorElement* & theRatio, int nSample)
00241 {
00242 
00243   std::vector<double> bunchSum;
00244   bunchSum.reserve(nSample);
00245   std::vector<double> bunchSumErro;
00246   bunchSumErro.reserve(nSample);
00247   std::vector<double> total;
00248   total.reserve(nSample);
00249   std::vector<double> totalErro;
00250   totalErro.reserve(nSample);
00251   std::vector<double> ratio;
00252   ratio.reserve(nSample);
00253   std::vector<double> ratioErro;
00254   ratioErro.reserve(nSample);
00255 
00256 
00257   for ( int iEl = 0 ; iEl < nSample ; iEl++ ) {
00258     bunchSum[iEl] = 0.;
00259     bunchSumErro[iEl] = 0.;
00260     total[iEl] = 0.;
00261     totalErro[iEl] = 0.;
00262     ratio[iEl] = 0.;
00263     ratioErro[iEl] = 0.;
00264   }
00265 
00266   for ( int iSample = 0 ; iSample < nSample ; iSample++ ) { 
00267 
00268     total[iSample] += theTotal->getBinContent(iSample+1);
00269     totalErro[iSample] += theTotal->getBinError(iSample+1);
00270 
00271     for ( int iBunch = theMinBunch; iBunch <= theMaxBunch; iBunch++ ) {
00272 
00273       int iHisto = iBunch - theMinBunch;
00274 
00275       bunchSum[iSample] += theBunches[iHisto]->getBinContent(iSample+1);
00276       bunchSumErro[iSample] += pow(theBunches[iHisto]->getBinError(iSample+1),2);
00277 
00278     }
00279     bunchSumErro[iSample] = sqrt(bunchSumErro[iSample]);
00280 
00281     if ( bunchSum[iSample] > 0. ) { 
00282       ratio[iSample] = total[iSample]/bunchSum[iSample];
00283       ratioErro[iSample] = sqrt(pow(totalErro[iSample]/bunchSum[iSample],2)+
00284                                 pow((total[iSample]*bunchSumErro[iSample])/(bunchSum[iSample]*bunchSum[iSample]),2));
00285     }
00286 
00287     std::cout << " Sample = " << iSample << " Total = " << total[iSample] << " +- " << totalErro[iSample] << "\n" 
00288               << " Sum   = " << bunchSum[iSample] << " +- " << bunchSumErro[iSample] << "\n" 
00289               << " Ratio = " << ratio[iSample] << " +- " << ratioErro[iSample] << std::endl;
00290       
00291     theRatio->setBinContent(iSample+1, (float)ratio[iSample]);
00292     theRatio->setBinError(iSample+1, (float)ratioErro[iSample]);
00293 
00294   }
00295 
00296 } 
00297 
00298 void EcalMixingModuleValidation::analyze(edm::Event const & e, edm::EventSetup const & c){
00299 
00300   //LogInfo("EventInfo") << " Run = " << e.id().run() << " Event = " << e.id().event();
00301 
00302   checkPedestals(c);
00303 
00304   std::vector<SimTrack> theSimTracks;
00305   std::vector<SimVertex> theSimVertexes;
00306 
00307   edm::Handle<edm::HepMCProduct> MCEvt;
00308   edm::Handle<CrossingFrame<PCaloHit> > crossingFrame;
00309   edm::Handle<EBDigiCollection> EcalDigiEB;
00310   edm::Handle<EEDigiCollection> EcalDigiEE;
00311   edm::Handle<ESDigiCollection> EcalDigiES;
00312 
00313   
00314   bool skipMC = false;
00315   e.getByLabel(HepMCLabel, MCEvt);
00316   if (!MCEvt.isValid()) { skipMC = true; }
00317 
00318   const EBDigiCollection* EBdigis =0;
00319   const EEDigiCollection* EEdigis =0;
00320   const ESDigiCollection* ESdigis =0;
00321 
00322   bool isBarrel = true;
00323   e.getByLabel( EBdigiCollection_, EcalDigiEB );
00324   if (EcalDigiEB.isValid()) { 
00325     EBdigis = EcalDigiEB.product();
00326     LogDebug("DigiInfo") << "total # EBdigis: " << EBdigis->size() ;
00327     if ( EBdigis->size() == 0 ) isBarrel = false;
00328   } else {
00329     isBarrel = false; 
00330   }
00331 
00332   bool isEndcap = true;
00333   e.getByLabel( EEdigiCollection_, EcalDigiEE );
00334   if (EcalDigiEE.isValid()) {
00335     EEdigis = EcalDigiEE.product();
00336     LogDebug("DigiInfo") << "total # EEdigis: " << EEdigis->size() ;
00337     if ( EEdigis->size() == 0 ) isEndcap = false;
00338   } else {
00339     isEndcap = false; 
00340   }
00341 
00342   bool isPreshower = true;
00343   e.getByLabel( ESdigiCollection_, EcalDigiES );
00344   if (EcalDigiES.isValid()) {
00345     ESdigis = EcalDigiES.product();
00346     LogDebug("DigiInfo") << "total # ESdigis: " << ESdigis->size() ;
00347     if ( ESdigis->size() == 0 ) isPreshower = false;
00348   } else {
00349     isPreshower = false; 
00350   }
00351 
00352   double theGunEnergy = 0.;
00353   if ( ! skipMC ) {
00354      for ( HepMC::GenEvent::particle_const_iterator p = MCEvt->GetEvent()->particles_begin();
00355           p != MCEvt->GetEvent()->particles_end(); ++p ) {
00356       
00357       theGunEnergy = (*p)->momentum().e();
00358       }
00359   }
00360   // in case no HepMC available, assume an arbitrary average energy for an interesting "gun"
00361   else { 
00362     edm::LogWarning("DigiInfo") << "No HepMC available, using 30 GeV as giun energy";
00363     theGunEnergy = 30.; 
00364   }
00365 
00366   // BARREL
00367 
00368   // loop over simHits
00369 
00370   if ( isBarrel ) {
00371 
00372     const std::string barrelHitsName(hitsProducer_+"EcalHitsEB");
00373     e.getByLabel("mix",barrelHitsName,crossingFrame);
00374     std::auto_ptr<MixCollection<PCaloHit> > 
00375       barrelHits (new MixCollection<PCaloHit>(crossingFrame.product ()));
00376     
00377     MapType ebSignalSimMap;
00378 
00379     double ebSimThreshold = 0.5*theGunEnergy;
00380 
00381     for (MixCollection<PCaloHit>::MixItr hitItr = barrelHits->begin () ;
00382          hitItr != barrelHits->end () ;
00383          ++hitItr) {
00384       
00385       EBDetId ebid = EBDetId(hitItr->id()) ;
00386       
00387       LogDebug("HitInfo") 
00388         << " CaloHit " << hitItr->getName() << "\n" 
00389         << " DetID = "<<hitItr->id()<< " EBDetId = " << ebid.ieta() << " " << ebid.iphi() << "\n"       
00390         << " Time = " << hitItr->time() << " Event id. = " << hitItr->eventId().rawId() << "\n"
00391         << " Track Id = " << hitItr->geantTrackId() << "\n"
00392         << " Energy = " << hitItr->energy();
00393 
00394       uint32_t crystid = ebid.rawId();
00395 
00396       if ( hitItr->eventId().rawId() == 0 ) ebSignalSimMap[crystid] += hitItr->energy();
00397       
00398       if ( meEBbunchCrossing_ ) meEBbunchCrossing_->Fill(hitItr->eventId().bunchCrossing()); 
00399       
00400     }
00401     
00402     // loop over Digis
00403     
00404     const EBDigiCollection * barrelDigi = EcalDigiEB.product () ;
00405     
00406     std::vector<double> ebAnalogSignal ;
00407     std::vector<double> ebADCCounts ;
00408     std::vector<double> ebADCGains ;
00409     ebAnalogSignal.reserve(EBDataFrame::MAXSAMPLES);
00410     ebADCCounts.reserve(EBDataFrame::MAXSAMPLES);
00411     ebADCGains.reserve(EBDataFrame::MAXSAMPLES);
00412     
00413     for (unsigned int digis=0; digis<EcalDigiEB->size(); ++digis) {
00414 
00415       EBDataFrame ebdf=(*barrelDigi)[digis];
00416       int nrSamples=ebdf.size();
00417       
00418       EBDetId ebid = ebdf.id () ;
00419       
00420       double Emax = 0. ;
00421       int Pmax = 0 ;
00422       
00423       for (int sample = 0 ; sample < nrSamples; ++sample) {
00424         ebAnalogSignal[sample] = 0.;
00425         ebADCCounts[sample] = 0.;
00426         ebADCGains[sample] = -1.;
00427       }
00428       
00429       for (int sample = 0 ; sample < nrSamples; ++sample) {
00430 
00431         EcalMGPASample mySample = ebdf[sample];
00432         
00433         ebADCCounts[sample] = (mySample.adc()) ;
00434         ebADCGains[sample]  = (mySample.gainId ()) ;
00435         ebAnalogSignal[sample] = (ebADCCounts[sample]*gainConv_[(int)ebADCGains[sample]]*barrelADCtoGeV_);
00436         if (Emax < ebAnalogSignal[sample] ) {
00437           Emax = ebAnalogSignal[sample] ;
00438           Pmax = sample ;
00439         }
00440         LogDebug("DigiInfo") << "EB sample " << sample << " ADC counts = " << ebADCCounts[sample] << " Gain Id = " << ebADCGains[sample] << " Analog eq = " << ebAnalogSignal[sample];
00441       }
00442       double pedestalPreSampleAnalog = 0.;
00443       findPedestal( ebid, (int)ebADCGains[Pmax] , pedestalPreSampleAnalog);
00444       pedestalPreSampleAnalog *= gainConv_[(int)ebADCGains[Pmax]]*barrelADCtoGeV_;
00445       double Erec = Emax - pedestalPreSampleAnalog;
00446       
00447       if ( ebSignalSimMap[ebid.rawId()] != 0. ) {
00448         LogDebug("DigiInfo") << " Digi / Signal Hit = " << Erec << " / " << ebSignalSimMap[ebid.rawId()] << " gainConv " << gainConv_[(int)ebADCGains[Pmax]];
00449         if ( Erec > 100.*barrelADCtoGeV_  && meEBDigiMixRatiogt100ADC_  ) meEBDigiMixRatiogt100ADC_->Fill( Erec/ebSignalSimMap[ebid.rawId()] );
00450         if ( ebSignalSimMap[ebid.rawId()] > ebSimThreshold  && meEBDigiMixRatioOriggt50pc_ ) meEBDigiMixRatioOriggt50pc_->Fill( Erec/ebSignalSimMap[ebid.rawId()] );
00451         if ( ebSignalSimMap[ebid.rawId()] > ebSimThreshold  && meEBShape_ ) {
00452           for ( int i = 0; i < 10 ; i++ ) {
00453             pedestalPreSampleAnalog = 0.;
00454             findPedestal( ebid, (int)ebADCGains[i] , pedestalPreSampleAnalog);
00455             pedestalPreSampleAnalog *= gainConv_[(int)ebADCGains[i]]*barrelADCtoGeV_;
00456             meEBShape_->Fill(i, ebAnalogSignal[i]-pedestalPreSampleAnalog );
00457           }
00458         }
00459       }
00460       
00461     } 
00462     
00463     EcalSubdetector thisDet = EcalBarrel;
00464     computeSDBunchDigi(c, *barrelHits, ebSignalSimMap, thisDet, ebSimThreshold);
00465   }
00466   
00467   
00468   // ENDCAP
00469 
00470   // loop over simHits
00471 
00472   if ( isEndcap ) {
00473 
00474     const std::string endcapHitsName(hitsProducer_+"EcalHitsEE");
00475     e.getByLabel("mix",endcapHitsName,crossingFrame);
00476     std::auto_ptr<MixCollection<PCaloHit> > 
00477       endcapHits (new MixCollection<PCaloHit>(crossingFrame.product ()));
00478     
00479     MapType eeSignalSimMap;
00480 
00481     double eeSimThreshold = 0.4*theGunEnergy;
00482     
00483     for (MixCollection<PCaloHit>::MixItr hitItr = endcapHits->begin () ;
00484          hitItr != endcapHits->end () ;
00485          ++hitItr) {
00486       
00487       EEDetId eeid = EEDetId(hitItr->id()) ;
00488       
00489       LogDebug("HitInfo") 
00490         << " CaloHit " << hitItr->getName() << "\n" 
00491         << " DetID = "<<hitItr->id()<< " EEDetId side = " << eeid.zside() << " = " << eeid.ix() << " " << eeid.iy() << "\n"
00492         << " Time = " << hitItr->time() << " Event id. = " << hitItr->eventId().rawId() << "\n"
00493         << " Track Id = " << hitItr->geantTrackId() << "\n"
00494         << " Energy = " << hitItr->energy();
00495       
00496       uint32_t crystid = eeid.rawId();
00497 
00498       if ( hitItr->eventId().rawId() == 0 ) eeSignalSimMap[crystid] += hitItr->energy();
00499       
00500       if ( meEEbunchCrossing_ ) meEEbunchCrossing_->Fill(hitItr->eventId().bunchCrossing()); 
00501 
00502     }
00503     
00504     // loop over Digis
00505     
00506     const EEDigiCollection * endcapDigi = EcalDigiEE.product () ;
00507     
00508     std::vector<double> eeAnalogSignal ;
00509     std::vector<double> eeADCCounts ;
00510     std::vector<double> eeADCGains ;
00511     eeAnalogSignal.reserve(EEDataFrame::MAXSAMPLES);
00512     eeADCCounts.reserve(EEDataFrame::MAXSAMPLES);
00513     eeADCGains.reserve(EEDataFrame::MAXSAMPLES);
00514     
00515     for (unsigned int digis=0; digis<EcalDigiEE->size(); ++digis) {
00516 
00517       EEDataFrame eedf=(*endcapDigi)[digis];
00518       int nrSamples=eedf.size();
00519       
00520       EEDetId eeid = eedf.id () ;
00521       
00522       double Emax = 0. ;
00523       int Pmax = 0 ;
00524       
00525       for (int sample = 0 ; sample < nrSamples; ++sample) {
00526         eeAnalogSignal[sample] = 0.;
00527         eeADCCounts[sample] = 0.;
00528         eeADCGains[sample] = -1.;
00529       }
00530       
00531       for (int sample = 0 ; sample < nrSamples; ++sample) {
00532         
00533         EcalMGPASample mySample = eedf[sample];
00534         
00535         eeADCCounts[sample] = (mySample.adc()) ;
00536         eeADCGains[sample]  = (mySample.gainId()) ;
00537         eeAnalogSignal[sample] = (eeADCCounts[sample]*gainConv_[(int)eeADCGains[sample]]*endcapADCtoGeV_);
00538         if (Emax < eeAnalogSignal[sample] ) {
00539           Emax = eeAnalogSignal[sample] ;
00540           Pmax = sample ;
00541         }
00542         LogDebug("DigiInfo") << "EE sample " << sample << " ADC counts = " << eeADCCounts[sample] << " Gain Id = " << eeADCGains[sample] << " Analog eq = " << eeAnalogSignal[sample];
00543       }
00544       double pedestalPreSampleAnalog = 0.;
00545       findPedestal( eeid, (int)eeADCGains[Pmax] , pedestalPreSampleAnalog);
00546       pedestalPreSampleAnalog *= gainConv_[(int)eeADCGains[Pmax]]*endcapADCtoGeV_;
00547       double Erec = Emax - pedestalPreSampleAnalog;
00548       
00549       if ( eeSignalSimMap[eeid.rawId()] != 0. ) {
00550         LogDebug("DigiInfo") << " Digi / Signal Hit = " << Erec << " / " << eeSignalSimMap[eeid.rawId()] << " gainConv " << gainConv_[(int)eeADCGains[Pmax]];
00551         if ( Erec > 100.*endcapADCtoGeV_  && meEEDigiMixRatiogt100ADC_  ) meEEDigiMixRatiogt100ADC_->Fill( Erec/eeSignalSimMap[eeid.rawId()] );
00552         if ( eeSignalSimMap[eeid.rawId()] > eeSimThreshold  && meEEDigiMixRatioOriggt40pc_ ) meEEDigiMixRatioOriggt40pc_->Fill( Erec/eeSignalSimMap[eeid.rawId()] );
00553         if ( eeSignalSimMap[eeid.rawId()] > eeSimThreshold  && meEBShape_ ) {
00554           for ( int i = 0; i < 10 ; i++ ) {
00555             pedestalPreSampleAnalog = 0.;
00556             findPedestal( eeid, (int)eeADCGains[i] , pedestalPreSampleAnalog);
00557             pedestalPreSampleAnalog *= gainConv_[(int)eeADCGains[i]]*endcapADCtoGeV_;
00558             meEEShape_->Fill(i, eeAnalogSignal[i]-pedestalPreSampleAnalog );
00559           }
00560           }
00561       }
00562     }
00563     
00564     EcalSubdetector thisDet = EcalEndcap;
00565     computeSDBunchDigi(c, *endcapHits, eeSignalSimMap, thisDet, eeSimThreshold);
00566   }
00567 
00568   if ( isPreshower) {
00569 
00570     const std::string preshowerHitsName(hitsProducer_+"EcalHitsES");
00571     e.getByLabel("mix",preshowerHitsName,crossingFrame);
00572     std::auto_ptr<MixCollection<PCaloHit> > 
00573       preshowerHits (new MixCollection<PCaloHit>(crossingFrame.product ()));
00574     
00575     MapType esSignalSimMap;
00576     
00577     for (MixCollection<PCaloHit>::MixItr hitItr = preshowerHits->begin () ;
00578          hitItr != preshowerHits->end () ;
00579          ++hitItr) {
00580       
00581       ESDetId esid = ESDetId(hitItr->id()) ;
00582       
00583       LogDebug("HitInfo") 
00584         << " CaloHit " << hitItr->getName() << "\n" 
00585         << " DetID = "<<hitItr->id()<< "ESDetId: z side " << esid.zside() << "  plane " << esid.plane() << esid.six() << ',' << esid.siy() << ':' << esid.strip() << "\n"
00586         << " Time = " << hitItr->time() << " Event id. = " << hitItr->eventId().rawId() << "\n"
00587         << " Track Id = " << hitItr->geantTrackId() << "\n"
00588         << " Energy = " << hitItr->energy();
00589 
00590       uint32_t stripid = esid.rawId();
00591 
00592       if ( hitItr->eventId().rawId() == 0 ) esSignalSimMap[stripid] += hitItr->energy();
00593       
00594       if ( meESbunchCrossing_ ) meESbunchCrossing_->Fill(hitItr->eventId().bunchCrossing()); 
00595   
00596       // loop over Digis
00597 
00598       const ESDigiCollection * preshowerDigi = EcalDigiES.product () ;
00599 
00600       std::vector<double> esADCCounts ;
00601       std::vector<double> esADCAnalogSignal ;
00602       esADCCounts.reserve(ESDataFrame::MAXSAMPLES);
00603       esADCAnalogSignal.reserve(ESDataFrame::MAXSAMPLES);
00604 
00605       for (unsigned int digis=0; digis<EcalDigiES->size(); ++digis) {
00606 
00607         ESDataFrame esdf=(*preshowerDigi)[digis];
00608         int nrSamples=esdf.size();
00609         
00610         ESDetId esid = esdf.id () ;
00611         
00612         for (int sample = 0 ; sample < nrSamples; ++sample) {
00613           esADCCounts[sample] = 0.;
00614           esADCAnalogSignal[sample] = 0.;
00615         }
00616         
00617         for (int sample = 0 ; sample < nrSamples; ++sample) {
00618 
00619           ESSample mySample = esdf[sample];
00620           
00621           esADCCounts[sample] = (mySample.adc()) ;
00622           esBaseline_ = m_ESpeds->find(esid)->getMean() ;
00623           const double factor ( esADCtokeV_/(*(m_ESmips->getMap().find(esid)) ) ) ;
00624           esThreshold_ = 3.*m_ESeffwei*( (*m_ESpeds->find(esid)).getRms())/factor;
00625           esADCAnalogSignal[sample] = (esADCCounts[sample]-esBaseline_)/factor;
00626         }
00627         LogDebug("DigiInfo") << "Preshower Digi for ESDetId: z side " << esid.zside() << "  plane " << esid.plane() << esid.six() << ',' << esid.siy() << ':' << esid.strip();
00628         for ( int i = 0; i < 3 ; i++ ) {
00629           LogDebug("DigiInfo") << "sample " << i << " ADC = " << esADCCounts[i] << " Analog eq = " << esADCAnalogSignal[i];
00630         }
00631         
00632         if ( esSignalSimMap[esid.rawId()] > esThreshold_  && meESShape_ ) {
00633           for ( int i = 0; i < 3 ; i++ ) {
00634             meESShape_->Fill(i, esADCAnalogSignal[i] );
00635           }
00636         }
00637         
00638       } 
00639       
00640     }
00641     
00642     EcalSubdetector thisDet = EcalPreshower;
00643     computeSDBunchDigi(c, *preshowerHits, esSignalSimMap, thisDet, esThreshold_);
00644     
00645   }
00646   
00647 }                                                                                       
00648 
00649 void  EcalMixingModuleValidation::checkCalibrations(edm::EventSetup const & eventSetup) {
00650   
00651   // ADC -> GeV Scale
00652   edm::ESHandle<EcalADCToGeVConstant> pAgc;
00653   eventSetup.get<EcalADCToGeVConstantRcd>().get(pAgc);
00654   const EcalADCToGeVConstant* agc = pAgc.product();
00655   
00656   EcalMGPAGainRatio * defaultRatios = new EcalMGPAGainRatio();
00657 
00658   gainConv_[1] = 1.;
00659   gainConv_[2] = defaultRatios->gain12Over6() ;
00660   gainConv_[3] = gainConv_[2]*(defaultRatios->gain6Over1()) ;
00661   gainConv_[0] = gainConv_[2]*(defaultRatios->gain6Over1()) ;
00662 
00663   LogDebug("EcalDigi") << " Gains conversions: " << "\n" << " g1 = " << gainConv_[1] << "\n" << " g2 = " << gainConv_[2] << "\n" << " g3 = " << gainConv_[3];
00664 
00665   delete defaultRatios;
00666 
00667   const double barrelADCtoGeV_  = agc->getEBValue();
00668   LogDebug("EcalDigi") << " Barrel GeV/ADC = " << barrelADCtoGeV_;
00669   const double endcapADCtoGeV_ = agc->getEEValue();
00670   LogDebug("EcalDigi") << " Endcap GeV/ADC = " << endcapADCtoGeV_;
00671 
00672 
00673    // ES condition objects
00674    edm::ESHandle<ESGain> esgain_;
00675    edm::ESHandle<ESMIPToGeVConstant> esMIPToGeV_;
00676    edm::ESHandle<ESPedestals> esPedestals_;
00677    edm::ESHandle<ESIntercalibConstants> esMIPs_;
00678 
00679    eventSetup.get<ESGainRcd>().get(esgain_);
00680    eventSetup.get<ESMIPToGeVConstantRcd>().get(esMIPToGeV_);
00681    eventSetup.get<ESPedestalsRcd>().get(esPedestals_);
00682    eventSetup.get<ESIntercalibConstantsRcd>().get(esMIPs_);
00683 
00684    const ESGain *esgain = esgain_.product();
00685    m_ESpeds = esPedestals_.product();
00686    m_ESmips = esMIPs_.product();
00687    const ESMIPToGeVConstant *esMipToGeV = esMIPToGeV_.product();
00688    m_ESgain = (int) esgain->getESGain();  
00689    const double valESMIPToGeV = (m_ESgain == 1) ? esMipToGeV->getESValueLow() : esMipToGeV->getESValueHigh(); 
00690 
00691    theESShape->setGain(m_ESgain);
00692 
00693    esADCtokeV_ = 1000000.*valESMIPToGeV ;
00694  
00695    m_ESeffwei = ( 0 == m_ESgain ? 1.45 :
00696                   ( 1 == m_ESgain ? 0.9066 :
00697                     ( 2 == m_ESgain ? 0.8815 : 1.0 ) ) ) ;
00698 
00699 }
00700 
00701 void EcalMixingModuleValidation::checkPedestals(const edm::EventSetup & eventSetup)
00702 {
00703 
00704   // Pedestals from event setup
00705 
00706   edm::ESHandle<EcalPedestals> dbPed;
00707   eventSetup.get<EcalPedestalsRcd>().get( dbPed );
00708   thePedestals=dbPed.product();
00709   
00710 }
00711 
00712 void EcalMixingModuleValidation::findPedestal(const DetId & detId, int gainId, double & ped) const
00713 {
00714   EcalPedestalsMapIterator mapItr 
00715     = thePedestals->getMap().find(detId);
00716   // should I care if it doesn't get found?
00717   if(mapItr == thePedestals->getMap().end()) {
00718     edm::LogError("EcalMMValid") << "Could not find pedestal for " << detId.rawId() << " among the " << thePedestals->getMap().size();
00719   } else {
00720     EcalPedestals::Item item = (*mapItr);
00721 
00722     switch(gainId) {
00723     case 0:
00724       ped = item.mean_x1;
00725     case 1:
00726       ped = item.mean_x12;
00727       break;
00728     case 2:
00729       ped = item.mean_x6;
00730       break;
00731     case 3:
00732       ped = item.mean_x1;
00733       break;
00734     default:
00735       edm::LogError("EcalMMValid") << "Bad Pedestal " << gainId;
00736       break;
00737     }
00738     LogDebug("EcalMMValid") << "Pedestals for " << detId.rawId() << " gain range " << gainId << " : \n" << "Mean = " << ped;
00739   }
00740 }
00741 
00742 void EcalMixingModuleValidation::computeSDBunchDigi(const edm::EventSetup & eventSetup, MixCollection<PCaloHit> & theHits, MapType & SignalSimMap, const EcalSubdetector & thisDet, const double & theSimThreshold)
00743 {
00744 
00745   if ( thisDet != EcalBarrel && thisDet != EcalEndcap && thisDet != EcalPreshower ) {
00746     edm::LogError("EcalMMValid") << "Invalid subdetector type";
00747     return;
00748   }
00749   //bool isCrystal = true;
00750   //if ( thisDet == EcalPreshower ) isCrystal = false;
00751 
00752   // load the geometry
00753 
00754   edm::ESHandle<CaloGeometry> hGeometry;
00755   eventSetup.get<CaloGeometryRecord>().get(hGeometry);
00756 
00757   const CaloGeometry * pGeometry = &*hGeometry;
00758   
00759   // see if we need to update
00760   if(pGeometry != theGeometry) {
00761     theGeometry = pGeometry;
00762     //theEcalResponse->setGeometry(theGeometry); 
00763     theESResponse->setGeometry(theGeometry); 
00764     theEEResponse->setGeometry(theGeometry); 
00765     theEBResponse->setGeometry(theGeometry); 
00766 
00767   }
00768 
00769   // vector of DetId with energy above a fraction of the gun's energy
00770 
00771   const std::vector<DetId>& theSDId = theGeometry->getValidDetIds( DetId::Ecal, thisDet );
00772 
00773   std::vector<DetId> theOverThresholdId;
00774   for ( unsigned int i = 0 ; i < theSDId.size() ; i++ ) {
00775 
00776     int sdId = theSDId[i].rawId();
00777     if ( SignalSimMap[sdId] > theSimThreshold ) theOverThresholdId.push_back( theSDId[i] );
00778 
00779   }
00780 
00781   int limit = CaloSamples::MAXSAMPLES;
00782   if ( thisDet == EcalPreshower ) limit = ESDataFrame::MAXSAMPLES;
00783 
00784    for (int iBunch = theMinBunch ; iBunch <= theMaxBunch ; iBunch++ ) {
00785 
00786      //if ( isCrystal ) {
00787      if( thisDet == EcalBarrel ) {
00788        theEBResponse->setBunchRange(iBunch, iBunch);
00789        theEBResponse->clear();
00790        theEBResponse->run(theHits);
00791      }
00792      else if( thisDet == EcalEndcap ) {
00793        theEEResponse->setBunchRange(iBunch, iBunch);
00794        theEEResponse->clear();
00795        theEEResponse->run(theHits);
00796      }
00797      else {
00798        theESResponse->setBunchRange(iBunch, iBunch);
00799        theESResponse->clear();
00800        theESResponse->run(theHits);
00801      }
00802 
00803      int iHisto = iBunch - theMinBunch;
00804 
00805      for ( std::vector<DetId>::const_iterator idItr = theOverThresholdId.begin() ; idItr != theOverThresholdId.end() ; ++idItr ) {
00806        
00807        CaloSamples * analogSignal;
00808        //if ( isCrystal ) 
00809        if( thisDet == EcalBarrel ) { 
00810          analogSignal = theEBResponse->findSignal(*idItr); 
00811        }
00812        else if( thisDet == EcalEndcap ) { 
00813          analogSignal = theEEResponse->findSignal(*idItr);
00814        }
00815        else { 
00816          analogSignal = theESResponse->findSignal(*idItr); 
00817        }
00818 
00819        if ( analogSignal ) {
00820         
00821          (*analogSignal) *= theParameterMap->simParameters(analogSignal->id()).photoelectronsToAnalog();
00822 
00823          for ( int i = 0 ; i < limit ; i++ ) {
00824            if ( thisDet == EcalBarrel ) { meEBBunchShape_[iHisto]->Fill(i,(float)(*analogSignal)[i]); }
00825            else if ( thisDet == EcalEndcap ) { meEEBunchShape_[iHisto]->Fill(i,(float)(*analogSignal)[i]); }
00826            else if ( thisDet == EcalPreshower ) { meESBunchShape_[iHisto]->Fill(i,(float)(*analogSignal)[i]); }
00827          }
00828        }
00829 
00830      }
00831 
00832    }
00833 
00834  }