CMS 3D CMS Logo

EcalABAnalyzer.cc
Go to the documentation of this file.
1 /*
2  * \class EcalABAnalyzer
3  *
4  * primary author: Julie Malcles - CEA/Saclay
5  * author: Gautier Hamel De Monchenault - CEA/Saclay
6  */
7 
8 #include <TAxis.h>
9 #include <TH1.h>
10 #include <TProfile.h>
11 #include <TTree.h>
12 #include <TChain.h>
13 #include <TFile.h>
14 #include <TMath.h>
15 
17 
18 #include <sstream>
19 #include <iostream>
20 #include <fstream>
21 #include <iomanip>
22 #include <vector>
23 
26 
29 
32 
40 
48 
49 using namespace std;
50 
51 
52 //========================================================================
54 //========================================================================
55  :
56 iEvent(0),
57 
58 // framework parameters with default values
59 _nsamples( iConfig.getUntrackedParameter< unsigned int >( "nSamples", 10 ) ),
60 _presample( iConfig.getUntrackedParameter< unsigned int >( "nPresamples", 2 ) ),
61 _firstsample( iConfig.getUntrackedParameter< unsigned int >( "firstSample", 1 ) ),
62 _lastsample( iConfig.getUntrackedParameter< unsigned int >( "lastSample", 2 ) ),
63 _timingcutlow( iConfig.getUntrackedParameter< unsigned int >( "timingCutLow", 2 ) ),
64 _timingcuthigh( iConfig.getUntrackedParameter< unsigned int >( "timingCutHigh", 9 ) ),
65 _timingquallow( iConfig.getUntrackedParameter< unsigned int >( "timingQualLow", 3 ) ),
66 _timingqualhigh( iConfig.getUntrackedParameter< unsigned int >( "timingQualHigh", 8 ) ),
67 _ratiomincutlow( iConfig.getUntrackedParameter< double >( "ratioMinCutLow", 0.4 ) ),
68 _ratiomincuthigh( iConfig.getUntrackedParameter< double >( "ratioMinCutHigh", 0.95 ) ),
69 _ratiomaxcutlow( iConfig.getUntrackedParameter< double >( "ratioMaxCutLow", 0.8 ) ),
70 _presamplecut( iConfig.getUntrackedParameter< double >( "presampleCut", 5.0 ) ),
71 _niter( iConfig.getUntrackedParameter< unsigned int >( "nIter", 3 ) ),
72 _alpha( iConfig.getUntrackedParameter< double >( "alpha", 1.5076494 ) ),
73 _beta( iConfig.getUntrackedParameter< double >( "beta", 1.5136036 ) ),
74 _nevtmax( iConfig.getUntrackedParameter< unsigned int >( "nEvtMax", 200 ) ),
75 _noise( iConfig.getUntrackedParameter< double >( "noise", 2.0 ) ),
76 _chi2cut( iConfig.getUntrackedParameter< double >( "chi2cut", 100.0 ) ),
77 _ecalPart( iConfig.getUntrackedParameter< std::string >( "ecalPart", "EB" ) ),
78 _qualpercent( iConfig.getUntrackedParameter< double >( "qualPercent", 0.2 ) ),
79 _debug( iConfig.getUntrackedParameter< int >( "debug", 0 ) ),
80 nCrys( NCRYSEB),
81 runType(-1), runNum(0), fedID(-1), dccID(-1), side(2), lightside(2), iZ(1),
82 phi(-1), eta(-1), event(0), color(-1), channelIteratorEE(0)
83 
84  //========================================================================
85 
86 {
87 
88 
89  // Initialization from cfg file
90 
91  resdir_ = iConfig.getUntrackedParameter<std::string>("resDir");
92 
93  digiCollection_ = iConfig.getParameter<std::string>("digiCollection");
94  digiProducer_ = iConfig.getParameter<std::string>("digiProducer");
95 
96  eventHeaderCollection_ = iConfig.getParameter<std::string>("eventHeaderCollection");
97  eventHeaderProducer_ = iConfig.getParameter<std::string>("eventHeaderProducer");
98 
99 
100 
101  // Geometrical constants initialization
102 
103 
104  if (_ecalPart == "EB") {
105  nCrys = NCRYSEB;
106  } else {
107  nCrys = NCRYSEE;
108  }
109  iZ = 1;
110  if(_fedid <= 609 )
111  iZ = -1;
112 
113  for(unsigned int j=0;j<nCrys;j++){
114  iEta[j]=-1;
115  iPhi[j]=-1;
116  iTowerID[j]=-1;
117  iChannelID[j]=-1;
118  idccID[j]=-1;
119  iside[j]=-1;
120  wasTimingOK[j]=true;
121  wasGainOK[j]=true;
122  nevtAB[j]=0 ;
123  }
124 
125  // Quality check flags
126 
127  isGainOK=true;
128  isTimingOK=true;
129 
130  // Objects dealing with pulses
131 
135 
136 
137  // Objects needed for npresample calculation
138 
139  Delta01=new TMom();
140  Delta12=new TMom();
141  _fitab=true;
142 
143 }
144 
145 //========================================================================
147  //========================================================================
148 
149 
150  // do anything here that needs to be done at desctruction time
151  // (e.g. close files, deallocate resources etc.)
152 
153 }
154 
155 
156 
157 //========================================================================
159  //========================================================================
160 
161 
162  //Calculate alpha and beta
163 
164 
165  // Define output results filenames and shape analyzer object (alpha,beta)
166  //=====================================================================
167 
168  // 1) AlphaBeta files
169 
170  doesABTreeExist=true;
171 
172  std::stringstream nameabinitfile;
173  nameabinitfile << resdir_ <<"/ABInit.root";
174  alphainitfile=nameabinitfile.str();
175 
176  std::stringstream nameabfile;
177  std::stringstream link;
178  nameabfile << resdir_ <<"/AB.root";
179 
180  FILE *test;
181  test = fopen(nameabinitfile.str().c_str(),"r");
182  if(test == nullptr) {
183  doesABTreeExist=false;
184  _fitab=true;
185  };
186  delete test;
187 
188 
189  TFile *fAB=nullptr; TTree *ABInit=nullptr;
190  if(doesABTreeExist){
191  fAB=new TFile(nameabinitfile.str().c_str());
192  }
193  if(doesABTreeExist && fAB){
194  ABInit = (TTree*) fAB->Get("ABCol0");
195  }
196 
197  // 2) Shape analyzer
198 
199  if(doesABTreeExist && fAB && ABInit && ABInit->GetEntries()!=0){
200  shapana= new TShapeAnalysis(ABInit, _alpha, _beta, 5.5, 1.0);
201  doesABTreeExist=true;
202  }else{
203  shapana= new TShapeAnalysis(_alpha, _beta, 5.5, 1.0);
204  doesABTreeExist=false;
205  _fitab=true;
206  }
209 
210  if(doesABTreeExist && fAB ) fAB->Close();
211 
212  if(_fitab){
213  alphafile=nameabfile.str();
214  }else{
216  link<< "ln -s "<<resdir_<<"/ABInit.root "<< resdir_<<"/AB.root";
217  system(link.str().c_str());
218  }
219 
220  // Define output results files' names
221 
222  std::stringstream namefile;
223  namefile << resdir_ <<"/AB.root";
224  alphafile=namefile.str();
225 
226 }
227 
228 //========================================================================
230 //========================================================================
231 
232  ++iEvent;
233 
234  // retrieving DCC header
236  const EcalRawDataCollection* DCCHeader=nullptr;
237  try {
239  DCCHeader=pDCCHeader.product();
240  }catch ( std::exception& ex ) {
241  std::cerr << "Error! can't get the product retrieving DCC header" << eventHeaderCollection_.c_str() <<" "<< eventHeaderProducer_.c_str() << std::endl;
242  }
243 
244  //retrieving crystal data from Event
246  const EBDigiCollection* EBDigi=nullptr;
248  const EEDigiCollection* EEDigi=nullptr;
249  if (_ecalPart == "EB") {
250  try {
252  EBDigi=pEBDigi.product();
253  }catch ( std::exception& ex ) {
254  std::cerr << "Error! can't get the product retrieving EB crystal data " << digiCollection_.c_str() << std::endl;
255  }
256  } else if (_ecalPart == "EE") {
257  try {
259  EEDigi=pEEDigi.product();
260  }catch ( std::exception& ex ) {
261  std::cerr << "Error! can't get the product retrieving EE crystal data " << digiCollection_.c_str() << std::endl;
262  }
263  } else {
264  std::cout <<" Wrong ecalPart in cfg file " << std::endl;
265  return;
266  }
267 
268  // retrieving electronics mapping
270  const EcalElectronicsMapping* TheMapping=nullptr;
271  try{
272  c.get< EcalMappingRcd >().get(ecalmapping);
273  TheMapping = ecalmapping.product();
274  }catch ( std::exception& ex ) {
275  std::cerr << "Error! can't get the product EcalMappingRcd"<< std::endl;
276  }
277 
278  // =============================
279  // Decode DCCHeader Information
280  // =============================
281 
282 
283  for ( EcalRawDataCollection::const_iterator headerItr= DCCHeader->begin();headerItr != DCCHeader->end();
284  ++headerItr ) {
285 
286  // Get run type and run number
287 
288  int fed = headerItr->fedId();
289  if(fed!=_fedid && _fedid!=-999) continue;
290 
291  runType=headerItr->getRunType();
292  runNum=headerItr->getRunNumber();
293  event=headerItr->getLV1();
294 
295  dccID=headerItr->getDccInTCCCommand();
296  fedID=headerItr->fedId();
297  lightside=headerItr->getRtHalf();
298 
299  // Check fed corresponds to the DCC in TCC
300 
301  if( 600+dccID != fedID ) continue;
302 
303  // Cut on runType
304 
309 
310  // Retrieve laser color and event number
311 
312  EcalDCCHeaderBlock::EcalDCCEventSettings settings = headerItr->getEventSettings();
313  color = settings.wavelength;
314  if( color <0 ) return;
315 
316  std::vector<int>::iterator iter = find( colors.begin(), colors.end(), color );
317  if( iter==colors.end() ){
318  colors.push_back( color );
319  }
320  }
321 
322 
323  // Cut on fedID
324 
325  if(fedID!=_fedid && _fedid!=-999) return;
326 
327 
328  // ===========================
329  // Decode EBDigis Information
330  // ===========================
331 
332  int adcGain=0;
333 
334  if (EBDigi){
335 
336  // Loop on crystals
337  //===================
338 
339  for ( EBDigiCollection::const_iterator digiItr= EBDigi->begin(); digiItr != EBDigi->end(); ++digiItr ) { // Loop on crystals
340 
341  // Retrieve geometry
342  //===================
343 
344  EBDetId id_crystal(digiItr->id()) ;
345  EBDataFrame df( *digiItr );
346 
347  int etaG = id_crystal.ieta() ; // global
348  int phiG = id_crystal.iphi() ; // global
349 
350  int etaL ; // local
351  int phiL ; // local
352  std::pair<int, int> LocalCoord=MEEBGeom::localCoord( etaG , phiG );
353 
354  etaL=LocalCoord.first ;
355  phiL=LocalCoord.second ;
356 
357  eta = etaG;
358  phi = phiG;
359 
360  side=MEEBGeom::side(etaG,phiG);
361 
362  // Recover the TT id and the electronic crystal numbering from EcalElectronicsMapping
363 
364  EcalElectronicsId elecid_crystal = TheMapping->getElectronicsId(id_crystal);
365 
366  int towerID=elecid_crystal.towerId();
367  int strip=elecid_crystal.stripId();
368  int xtal=elecid_crystal.xtalId();
369  int channelID= 5*(strip-1) + xtal-1;
370 
371  unsigned int channel=MEEBGeom::electronic_channel( etaL, phiL );
372 
373  assert( channel < nCrys );
374 
375  iEta[channel]=eta;
376  iPhi[channel]=phi;
377  iTowerID[channel]=towerID;
378  iChannelID[channel]=channelID;
379  idccID[channel]=dccID;
380  iside[channel]=side;
381 
382  // APD Pulse
383  //===========
384 
385  // Loop on adc samples
386 
387  for (unsigned int i=0; i< (*digiItr).size() ; ++i ) {
388 
389  EcalMGPASample samp_crystal(df.sample(i));
390  adc[i]=samp_crystal.adc() ;
391  adcG[i]=samp_crystal.gainId();
392  adc[i]*=adcG[i];
393  if (i==0) adcGain=adcG[i];
394  if (i>0) adcGain=TMath::Max(adcG[i],adcGain);
395  }
396 
398 
399 
400  // Quality checks
401  //================
402 
403  if(adcGain!=1) nEvtBadGain[channel]++;
404  if(!APDPulse->isTimingQualOK()) nEvtBadTiming[channel]++;
405  nEvtTot[channel]++;
406 
407 
408  // Fill if Pulse is fine
409  //=======================
410 
411  if( APDPulse->isPulseOK() && lightside==side){
412 
415 
416  if( nevtAB[channel] < _nevtmax && _fitab ){
417  if(doesABTreeExist) shapana -> putAllVals(channel, adc, eta, phi);
418  else shapana -> putAllVals(channel, adc, eta, phi, dccID, side, towerID, channelID);
419  nevtAB[channel]++ ;
420  }
421  }
422  }
423 
424  } else if (EEDigi) {
425 
426  // Loop on crystals
427  //===================
428 
429  for ( EEDigiCollection::const_iterator digiItr= EEDigi->begin(); digiItr != EEDigi->end(); ++digiItr ) { // Loop on crystals
430 
431  // Retrieve geometry
432  //===================
433 
434  EEDetId id_crystal(digiItr->id()) ;
435  EEDataFrame df( *digiItr );
436 
437  phi = id_crystal.ix() ;
438  eta = id_crystal.iy() ;
439 
440  int iX = (phi-1)/5+1;
441  int iY = (eta-1)/5+1;
442 
443  side=MEEEGeom::side( iX, iY ,iZ);
444  EcalElectronicsId elecid_crystal = TheMapping->getElectronicsId(id_crystal);
445 
446  int towerID=elecid_crystal.towerId();
447  int channelID=elecid_crystal.channelId()-1;
448 
449  int hashedIndex=100000*eta+phi;
450 
451  if( channelMapEE.count(hashedIndex) == 0 ){
454  }
455 
456  unsigned int channel=channelMapEE[hashedIndex];
457 
458  assert ( channel < nCrys );
459 
460  iEta[channel]=eta;
461  iPhi[channel]=phi;
462  iTowerID[channel]=towerID;
463  iChannelID[channel]=channelID;
464  idccID[channel]=dccID;
465  iside[channel]=side;
466 
467  // APD Pulse
468  //===========
469 
470  if( (*digiItr).size()>10) std::cout <<"SAMPLES SIZE > 10!" << (*digiItr).size()<< std::endl;
471 
472  // Loop on adc samples
473 
474  for (unsigned int i=0; i< (*digiItr).size() ; ++i ) {
475 
476  EcalMGPASample samp_crystal(df.sample(i));
477  adc[i]=samp_crystal.adc() ;
478  adcG[i]=samp_crystal.gainId();
479  adc[i]*=adcG[i];
480 
481  if (i==0) adcGain=adcG[i];
482  if (i>0) adcGain=TMath::Max(adcG[i],adcGain);
483  }
484 
486 
487  // Quality checks
488  //================
489 
490  if(adcGain!=1) nEvtBadGain[channel]++;
491  if(!APDPulse->isTimingQualOK()) nEvtBadTiming[channel]++;
492  nEvtTot[channel]++;
493 
494  // Fill if Pulse is fine
495  //=======================
496 
497  if( APDPulse->isPulseOK() && lightside==side){
498 
501 
502  if( nevtAB[channel] < _nevtmax && _fitab ){
503  if(doesABTreeExist) shapana -> putAllVals(channel, adc, eta, phi);
504  else shapana -> putAllVals(channel, adc, eta, phi, dccID, side, towerID, channelID);
505  nevtAB[channel]++ ;
506  }
507  }
508  }
509  }
510 }// analyze
511 
512 
513 //========================================================================
515 //========================================================================
516 
517  std::cout << "\n\t+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+" << std::endl;
518  std::cout << "\t+=+ Analyzing data: getting (alpha, beta) +=+" << std::endl;
519 
520  // Adjust channel numbers for EE
521  //===============================
522 
523  if( _ecalPart == "EE" ) {
524  nCrys=channelMapEE.size();
526  }
527 
528 
529  // Set presamples number
530  //======================
531 
532  double delta01=Delta01->getMean();
533  double delta12=Delta12->getMean();
534  if(delta12>_presamplecut) {
535  _presample=2;
536  if(delta01>_presamplecut) _presample=1;
537  }
538 
541 
542  // Get alpha and beta
543  //======================
544 
545  if(_fitab){
546  std::cout << "\n\t+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+" << std::endl;
547  std::cout << "\t+=+ Analyzing data: getting (alpha, beta) +=+" << std::endl;
548  TFile *fAB=nullptr; TTree *ABInit=nullptr;
549  if(doesABTreeExist){
550  fAB=new TFile(alphainitfile.c_str());
551  }
552  if(doesABTreeExist && fAB){
553  ABInit = (TTree*) fAB->Get("ABCol0");
554  }
555  shapana->computeShape(alphafile, ABInit);
556 
557  // Set quality flags for gains and timing
558 
559  double BadGainEvtPercentage=0.0;
560  double BadTimingEvtPercentage=0.0;
561 
562  int nChanBadGain=0;
563  int nChanBadTiming=0;
564 
565  for (unsigned int i=0;i<nCrys;i++){
566  if(nEvtTot[i]!=0){
567  BadGainEvtPercentage=double(nEvtBadGain[i])/double(nEvtTot[i]);
568  BadTimingEvtPercentage=double(nEvtBadTiming[i])/double(nEvtTot[i]);
569  }
570  if(BadGainEvtPercentage>_qualpercent) {
571  wasGainOK[i]=false;
572  nChanBadGain++;
573  }
574  if(BadTimingEvtPercentage>_qualpercent){
575  wasTimingOK[i]=false;
576  nChanBadTiming++;
577  }
578  }
579 
580  double BadGainChanPercentage=double(nChanBadGain)/double(nCrys);
581  double BadTimingChanPercentage=double(nChanBadTiming)/double(nCrys);
582 
583  if(BadGainChanPercentage>_qualpercent) isGainOK = false;
584  if(BadTimingChanPercentage>_qualpercent) isTimingOK = false;
585 
586 
587  if( !isGainOK )
588  std::cout << "\t+=+ ............................ WARNING! APD GAIN WAS NOT 1 +=+" << std::endl;
589  if( !isTimingOK )
590  std::cout << "\t+=+ ............................ WARNING! TIMING WAS BAD +=+" << std::endl;
591 
592  std::cout << "\t+=+ .................................... done +=+" << std::endl;
593  std::cout << "\t+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+" << std::endl;
594  }
595 
596 
597 }
598 
600 
void addEntry(double val)
Definition: TMom.cc:110
static XYCoord localCoord(int icr)
Definition: MEEBGeom.cc:153
T getParameter(std::string const &) const
unsigned int nCrys
T getUntrackedParameter(std::string const &, T const &) const
int nEvtBadGain[1700]
std::string digiProducer_
std::string alphainitfile
int xtalId() const
get the channel id
std::string eventHeaderProducer_
boost::transform_iterator< IterHelp, boost::counting_iterator< int > > const_iterator
std::string _ecalPart
int idccID[1700]
int stripId() const
get the tower id
double getDelta(int, int)
Definition: TAPDPulse.cc:95
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
Ecal readout channel identification [32:20] Unused (so far) [19:13] DCC id [12:6] tower [5:3] strip [...
double _ratiomaxcutlow
#define NCRYSEE
bool setPulse(double *)
Definition: TAPDPulse.cc:64
static int side(SuperCrysCoord iX, SuperCrysCoord iY, int iz)
Definition: MEEEGeom.cc:939
double _ratiomincuthigh
std::vector< EcalDCCHeaderBlock >::const_iterator const_iterator
unsigned int _nsamples
void computeShape(std::string namefile, TTree *)
int towerId() const
get the tower id
unsigned int _firstsample
const_iterator begin() const
Definition: TMom.h:7
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:20
unsigned int _nevtmax
double adc[10]
std::string alphafile
int hashedIndex(int ieta, int iphi)
Definition: EcalPyUtils.cc:42
EcalElectronicsId getElectronicsId(const DetId &id) const
Get the electronics id for this det id.
double _presamplecut
~EcalABAnalyzer() override
int iEvent
Definition: GenABIO.cc:230
static int electronic_channel(EBLocalCoord ix, EBLocalCoord iy)
Definition: MEEBGeom.cc:341
#define NCRYSEB
int iChannelID[1700]
void beginJob() override
int iTowerID[1700]
int nEvtTot[1700]
EcalABAnalyzer(const edm::ParameterSet &iConfig)
TShapeAnalysis * shapana
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:535
unsigned int nevtAB[1700]
unsigned int _timingcuthigh
std::string resdir_
const_iterator end() const
T Max(T a, T b)
Definition: MathUtil.h:44
static int side(EBGlobalCoord ieta, EBGlobalCoord iphi)
Definition: MEEBGeom.cc:114
bool isTimingQualOK()
Definition: TAPDPulse.cc:123
unsigned int _lastsample
T const * product() const
Definition: Handle.h:81
std::map< int, int > channelMapEE
TAPDPulse * APDPulse
const_iterator end() const
void analyze(const edm::Event &e, const edm::EventSetup &c) override
std::string digiCollection_
unsigned int _timingqualhigh
void setPresamples(int)
Definition: TAPDPulse.cc:222
bool isPulseOK()
Definition: TAPDPulse.cc:139
unsigned int _presample
std::string eventHeaderCollection_
double _ratiomincutlow
void endJob() override
Definition: colors.py:1
void set_presample(int)
T get() const
Definition: EventSetup.h:63
unsigned int _timingcutlow
bool wasTimingOK[1700]
EcalLogicID towerID(EcalElectronicsId const &)
T const * product() const
Definition: ESHandle.h:86
double getMean()
Definition: TMom.cc:147
unsigned int _timingquallow
int channelId() const
so far for EndCap only :
bool wasGainOK[1700]
const_iterator begin() const
Definition: event.py:1
int nEvtBadTiming[1700]