64 #include "TObjString.h"
74 #include <ext/hash_map>
81 using namespace __gnu_cxx;
82 using __gnu_cxx::hash_map;
122 virtual void algoEndJob ()
override;
126 void algoAnalyzeTheTree();
127 void algoComputeMPVandGain();
130 void getPeakOfLandau(TH1* InputHisto,
double* FitResults,
double LowRange=50,
double HighRange=5400);
131 bool IsGoodLandauFit(
double* FitResults);
133 void MakeCalibrationMap();
134 bool produceTagFilter();
195 template <
class T>
bool operator () (
const T& PseudoDetId1,
const T& PseudoDetId2) {
return PseudoDetId1==PseudoDetId2; }
199 __gnu_cxx::hash_map<unsigned int, stAPVGain*, __gnu_cxx::hash<unsigned int>,
isEqual >
APVsColl;
240 cout <<
"algoBeginRun start" << endl;
242 cout <<
" booking start" << endl;
243 dbe->setCurrentFolder(
"AlCaReco/SiStripGains/");
244 Charge_Vs_Index =
dbe->book2D(
"Charge_Vs_Index" ,
"Charge_Vs_Index" , 72785, 0 , 72784,1000,0,2000);
257 auto const &
Det = tkGeom->dets();
261 if(!gainHandle.
isValid()){printf(
"\n#####################\n\nERROR --> gainHandle is not valid\n\n#####################\n\n");
exit(0);}
263 for(
unsigned int i=0;
i<gainHandle->getNumberOfTags();
i++){
264 printf(
"Reccord %i --> Rcd Name = %s Label Name = %s\n",
i,gainHandle->getRcdName(
i).c_str(), gainHandle->getLabelName(
i).c_str());
270 unsigned int Index=0;
271 for(
unsigned int i=0;
i<
Det.size();
i++){
279 if(!DetUnit)
continue;
282 unsigned int NAPV = Topo.
nstrips()/128;
283 for(
unsigned int j=0;
j<NAPV;
j++){
297 APV->
x = DetUnit->position().basicVector().x();
298 APV->
y = DetUnit->position().basicVector().y();
299 APV->
z = DetUnit->position().basicVector().z();
300 APV->
Eta = DetUnit->position().basicVector().eta();
301 APV->
Phi = DetUnit->position().basicVector().phi();
302 APV->
R = DetUnit->position().basicVector().transverse();
303 APV->
Thickness = DetUnit->surface().bounds().thickness();
308 if(gainHandle->getNumberOfTags()!=2){printf(
"ERROR: NUMBER OF GAIN TAG IS EXPECTED TO BE 2\n");fflush(stdout);
exit(0);};
331 cout <<
"algoBeginRun end" << endl;
345 cout <<
" retrieving from DQMStore" << endl;
368 FitResults[0] = -0.5;
370 FitResults[2] = -0.5;
372 FitResults[4] = -0.5;
377 TF1* MyLandau =
new TF1(
"MyLandau",
"landau",LowRange, HighRange);
378 MyLandau->SetParameter(1,300);
379 InputHisto->Fit(MyLandau,
"0QR WW");
382 FitResults[0] = MyLandau->GetParameter(1);
383 FitResults[1] = MyLandau->GetParError(1);
384 FitResults[2] = MyLandau->GetParameter(2);
385 FitResults[3] = MyLandau->GetParError(2);
386 FitResults[4] = MyLandau->GetChisquare() / MyLandau->GetNDF();
392 if(FitResults[0] <= 0 )
return false;
402 printf(
"Openning file %3i/%3i --> %s\n",
i+1, (
int)
VInputFiles.size(), (
char*)(
VInputFiles[
i].c_str())); fflush(stdout);
403 TChain*
tree =
new TChain(
"commonCalibrationTree/tree");
406 TString EventPrefix(
"");
407 TString EventSuffix(
"");
409 TString TrackPrefix(
"track");
410 TString TrackSuffix(
"");
412 TString CalibPrefix(
"GainCalibration");
413 TString CalibSuffix(
"");
415 unsigned int eventnumber = 0; tree->SetBranchAddress(EventPrefix +
"event" + EventSuffix, &eventnumber ,
NULL);
416 unsigned int runnumber = 0; tree->SetBranchAddress(EventPrefix +
"run" + EventSuffix, &runnumber ,
NULL);
417 std::vector<bool>* TrigTech = 0; tree->SetBranchAddress(EventPrefix +
"TrigTech" + EventSuffix, &TrigTech ,
NULL);
419 std::vector<double>* trackchi2ndof = 0; tree->SetBranchAddress(TrackPrefix +
"chi2ndof" + TrackSuffix, &trackchi2ndof ,
NULL);
420 std::vector<float>* trackp = 0; tree->SetBranchAddress(TrackPrefix +
"momentum" + TrackSuffix, &trackp ,
NULL);
421 std::vector<float>* trackpt = 0; tree->SetBranchAddress(TrackPrefix +
"pt" + TrackSuffix, &trackpt ,
NULL);
422 std::vector<double>* tracketa = 0; tree->SetBranchAddress(TrackPrefix +
"eta" + TrackSuffix, &tracketa ,
NULL);
423 std::vector<double>* trackphi = 0; tree->SetBranchAddress(TrackPrefix +
"phi" + TrackSuffix, &trackphi ,
NULL);
424 std::vector<unsigned int>* trackhitsvalid = 0; tree->SetBranchAddress(TrackPrefix +
"hitsvalid" + TrackSuffix, &trackhitsvalid,
NULL);
426 std::vector<int>* trackindex = 0; tree->SetBranchAddress(CalibPrefix +
"trackindex" + CalibSuffix, &trackindex ,
NULL);
427 std::vector<unsigned int>* rawid = 0; tree->SetBranchAddress(CalibPrefix +
"rawid" + CalibSuffix, &rawid ,
NULL);
428 std::vector<float>* localdirx = 0; tree->SetBranchAddress(CalibPrefix +
"localdirx" + CalibSuffix, &localdirx ,
NULL);
429 std::vector<float>* localdiry = 0; tree->SetBranchAddress(CalibPrefix +
"localdiry" + CalibSuffix, &localdiry ,
NULL);
430 std::vector<float>* localdirz = 0; tree->SetBranchAddress(CalibPrefix +
"localdirz" + CalibSuffix, &localdirz ,
NULL);
431 std::vector<unsigned short>* firststrip = 0; tree->SetBranchAddress(CalibPrefix +
"firststrip" + CalibSuffix, &firststrip ,
NULL);
432 std::vector<unsigned short>* nstrips = 0; tree->SetBranchAddress(CalibPrefix +
"nstrips" + CalibSuffix, &nstrips ,
NULL);
433 std::vector<bool>* saturation = 0; tree->SetBranchAddress(CalibPrefix +
"saturation" + CalibSuffix, &saturation ,
NULL);
434 std::vector<bool>* overlapping = 0; tree->SetBranchAddress(CalibPrefix +
"overlapping" + CalibSuffix, &overlapping ,
NULL);
435 std::vector<bool>* farfromedge = 0; tree->SetBranchAddress(CalibPrefix +
"farfromedge" + CalibSuffix, &farfromedge ,
NULL);
436 std::vector<unsigned int>* charge = 0; tree->SetBranchAddress(CalibPrefix +
"charge" + CalibSuffix, &charge ,
NULL);
437 std::vector<float>*
path = 0; tree->SetBranchAddress(CalibPrefix +
"path" + CalibSuffix, &path ,
NULL);
438 std::vector<float>* chargeoverpath = 0; tree->SetBranchAddress(CalibPrefix +
"chargeoverpath" + CalibSuffix, &chargeoverpath,
NULL);
439 std::vector<unsigned char>* amplitude = 0; tree->SetBranchAddress(CalibPrefix +
"amplitude" + CalibSuffix, &litude ,
NULL);
440 std::vector<double>* gainused = 0; tree->SetBranchAddress(CalibPrefix +
"gainused" + CalibSuffix, &gainused ,
NULL);
443 printf(
"Number of Events = %i + %i = %i\n",
NEvent,(
unsigned int)tree->GetEntries(),(
unsigned int)(
NEvent+tree->GetEntries()));
444 printf(
"Progressing Bar :0%% 20%% 40%% 60%% 80%% 100%%\n");
445 printf(
"Looping on the Tree :");
446 int TreeStep = tree->GetEntries()/50;
if(TreeStep<=1)TreeStep=1;
447 for (
unsigned int ientry = 0; ientry < tree->GetEntries(); ientry++) {
448 if(ientry%TreeStep==0){printf(
".");fflush(stdout);}
449 tree->GetEntry(ientry);
457 unsigned int FirstAmplitude=0;
458 for(
unsigned int i=0;
i<(*chargeoverpath).size();
i++){
459 FirstAmplitude+=(*nstrips)[
i];
460 int TI = (*trackindex)[
i];
467 if((*farfromedge)[
i] ==
false )
continue;
468 if((*overlapping)[
i] ==
true )
continue;
488 bool Saturation =
false;
489 for(
unsigned int s=0;
s<(*nstrips)[
i];
s++){
490 int StripCharge = (*amplitude)[FirstAmplitude-(*nstrips)[
i]+
s];
494 if(StripCharge>1024){
497 }
else if(StripCharge>254){
501 Charge += StripCharge;
505 Charge = (*charge)[
i];
510 double ClusterChargeOverPath = ( (double) Charge )/(*path)[
i] ;
511 if(
Validation) {ClusterChargeOverPath/=(*gainused)[
i];}
528 }
else if(APV->
Eta>0){
546 double FitResults[5];
547 double MPVmean = 300;
552 printf(
"Progressing Bar :0%% 20%% 40%% 60%% 80%% 100%%\n");
553 printf(
"Fitting Charge Distribution :");
556 if(I%TreeStep==0){printf(
".");fflush(stdout);}
558 if(APV->
Bin<0) APV->
Bin = chvsidx->GetXaxis()->FindBin(APV->
Index);
562 Proj = (TH1F*)(chvsidx->ProjectionY(
"",chvsidx->GetXaxis()->FindBin(APV->
Index),chvsidx->GetXaxis()->FindBin(APV->
Index),
"e"));
567 int SecondAPVId = APV->
APVId;
568 if(SecondAPVId%2==0){ SecondAPVId = SecondAPVId+1; }
else{ SecondAPVId = SecondAPVId-1; }
570 if(APV2->
Bin<0) APV2->
Bin = chvsidx->GetXaxis()->FindBin(APV2->
Index);
571 TH1F* Proj2 = (TH1F*)(chvsidx->ProjectionY(
"",APV2->
Bin,APV2->
Bin,
"e"));
572 if(Proj2){Proj->Add(Proj2,1);
delete Proj2;}
574 for(
unsigned int i=0;
i<6;
i++){
575 __gnu_cxx::hash_map<unsigned int, stAPVGain*, __gnu_cxx::hash<unsigned int>,
isEqual >::iterator tmpit;
580 if(APV2->
Bin<0) APV2->
Bin = chvsidx->GetXaxis()->FindBin(APV2->
Index);
581 TH1F* Proj2 = (TH1F*)(chvsidx->ProjectionY(
"",APV2->
Bin,APV2->
Bin,
"e"));
582 if(Proj2){Proj->Add(Proj2,1);
delete Proj2;}
590 APV->
FitMPV = FitResults[0];
616 unsigned int tree_Index;
617 unsigned int tree_Bin;
618 unsigned int tree_DetId;
619 unsigned char tree_APVId;
620 unsigned char tree_SubDet;
627 float tree_Thickness;
629 float tree_FitMPVErr;
631 float tree_FitWidthErr;
632 float tree_FitChi2NDF;
634 double tree_PrevGain;
635 double tree_NEntries;
639 MyTree =
tfs->
make<TTree> (
"APVGain",
"APVGain");
640 MyTree->Branch(
"Index" ,&tree_Index ,
"Index/i");
641 MyTree->Branch(
"Bin" ,&tree_Bin ,
"Bin/i");
642 MyTree->Branch(
"DetId" ,&tree_DetId ,
"DetId/i");
643 MyTree->Branch(
"APVId" ,&tree_APVId ,
"APVId/b");
644 MyTree->Branch(
"SubDet" ,&tree_SubDet ,
"SubDet/b");
645 MyTree->Branch(
"x" ,&tree_x ,
"x/F");
646 MyTree->Branch(
"y" ,&tree_y ,
"y/F");
647 MyTree->Branch(
"z" ,&tree_z ,
"z/F");
648 MyTree->Branch(
"Eta" ,&tree_Eta ,
"Eta/F");
649 MyTree->Branch(
"R" ,&tree_R ,
"R/F");
650 MyTree->Branch(
"Phi" ,&tree_Phi ,
"Phi/F");
651 MyTree->Branch(
"Thickness" ,&tree_Thickness ,
"Thickness/F");
652 MyTree->Branch(
"FitMPV" ,&tree_FitMPV ,
"FitMPV/F");
653 MyTree->Branch(
"FitMPVErr" ,&tree_FitMPVErr ,
"FitMPVErr/F");
654 MyTree->Branch(
"FitWidth" ,&tree_FitWidth ,
"FitWidth/F");
655 MyTree->Branch(
"FitWidthErr" ,&tree_FitWidthErr,
"FitWidthErr/F");
656 MyTree->Branch(
"FitChi2NDF" ,&tree_FitChi2NDF ,
"FitChi2NDF/F");
657 MyTree->Branch(
"Gain" ,&tree_Gain ,
"Gain/D");
658 MyTree->Branch(
"PrevGain" ,&tree_PrevGain ,
"PrevGain/D");
659 MyTree->Branch(
"NEntries" ,&tree_NEntries ,
"NEntries/D");
660 MyTree->Branch(
"isMasked" ,&tree_isMasked ,
"isMasked/O");
663 FILE* Gains = stdout;
665 fprintf(Gains,
"NEvents = %i\n",
NEvent);
666 fprintf(Gains,
"NTracks = %i\n",
NTrack);
667 fprintf(Gains,
"NClusters = %i\n",
NCluster);
668 fprintf(Gains,
"Number of APVs = %lu\n",static_cast<unsigned long>(
APVsColl.size()));
669 fprintf(Gains,
"GoodFits = %i BadFits = %i ratio = %f\n",
GOOD,
BAD,(100.0*
GOOD)/(
GOOD+
BAD));
672 fprintf(Gains,
"NEvents = %i\n",
NEvent);
673 fprintf(Gains,
"NTracks = %i\n",
NTrack);
674 fprintf(Gains,
"NClusters = %i\n",
NCluster);
675 fprintf(Gains,
"Number of APVs = %lu\n",static_cast<unsigned long>(
APVsColl.size()));
676 fprintf(Gains,
"GoodFits = %i BadFits = %i ratio = %f\n",
GOOD,
BAD,(100.0*
GOOD)/(
GOOD+
BAD));
680 if(APV==
NULL)
continue;
684 tree_Index = APV->
Index;
686 tree_DetId = APV->
DetId;
687 tree_APVId = APV->
APVId;
688 tree_SubDet = APV->
SubDet;
696 tree_FitMPV = APV->
FitMPV;
700 tree_FitChi2NDF = APV->
FitChi2;
701 tree_Gain = APV->
Gain;
717 cout <<
"[SiStripGainFromCalibTree] produceTagFilter -> Return false: Statistics is too low : " <<
Charge_Vs_Index->
getTH2F()->Integral() << endl;
721 cout <<
"[SiStripGainFromCalibTree] produceTagFilter -> Return false: ratio of GOOD/TOTAL is too low: " << (1.0 *
GOOD) / (
GOOD+
BAD) << endl;
733 cout <<
"[SiStripGainFromCalibTree] getNewObject -> will not produce a paylaod because produceTagFilter returned false " << endl;
739 std::vector<float>* theSiStripVector =
NULL;
740 unsigned int PreviousDetId = 0;
744 if(APV==
NULL){ printf(
"Bug\n");
continue; }
745 if(APV->
DetId != PreviousDetId){
746 if(theSiStripVector!=
NULL){
748 if ( !obj->
put(PreviousDetId,range) ) printf(
"Bug to put detId = %i\n",PreviousDetId);
750 theSiStripVector =
new std::vector<float>;
751 PreviousDetId = APV->
DetId;
753 theSiStripVector->push_back(APV->
Gain);
755 if(theSiStripVector!=
NULL){
757 if ( !obj->
put(PreviousDetId,range) ) printf(
"Bug to put detId = %i\n",PreviousDetId);
771 TChain* t1 =
new TChain(
"SiStripCalib/APVGain");
774 unsigned int tree_DetId;
775 unsigned char tree_APVId;
778 t1->SetBranchAddress(
"DetId" ,&tree_DetId );
779 t1->SetBranchAddress(
"APVId" ,&tree_APVId );
780 t1->SetBranchAddress(
"Gain" ,&tree_Gain );
782 for (
unsigned int ientry = 0; ientry < t1->GetEntries(); ientry++) {
783 t1->GetEntry(ientry);
810 const Trajectory* traj = association->key.get();
822 vector<TrajectoryMeasurement> measurements = traj->
measurements();
823 for(vector<TrajectoryMeasurement>::const_iterator measurement_it = measurements.begin(); measurement_it!=measurements.end(); measurement_it++){
825 if( !trajState.
isValid() )
continue;
828 const SiStripRecHit1D* sistripsimple1dhit =
dynamic_cast<const SiStripRecHit1D*
>(hit);
829 const SiStripRecHit2D* sistripsimplehit =
dynamic_cast<const SiStripRecHit2D*
>(hit);
830 const SiStripMatchedRecHit2D* sistripmatchedhit =
dynamic_cast<const SiStripMatchedRecHit2D*
>(hit);
835 for(
unsigned int h=0;
h<2;
h++){
836 if(!sistripmatchedhit &&
h==1){
838 }
else if(sistripmatchedhit &&
h==0){
839 Cluster = &sistripmatchedhit->monoCluster();
840 DetId = sistripmatchedhit->monoId();
841 }
else if(sistripmatchedhit &&
h==1){
842 Cluster = &sistripmatchedhit->stereoCluster();;
843 DetId = sistripmatchedhit->stereoId();
844 }
else if(sistripsimplehit){
845 Cluster = (sistripsimplehit->cluster()).
get();
846 DetId = sistripsimplehit->geographicalId().rawId();
847 }
else if(sistripsimple1dhit){
848 Cluster = (sistripsimple1dhit->cluster()).
get();
849 DetId = sistripsimple1dhit->geographicalId().rawId();
855 double cosine = trackDirection.
z()/trackDirection.
mag();
856 const vector<uint8_t>& Ampls = Cluster->
amplitudes();
858 int APVId = FirstStrip/128;
859 bool Saturation =
false;
860 bool Overlapping =
false;
861 unsigned int charge = 0;
862 double PrevGain = -1;
869 PrevGain = *(detGainRange.first + APVId);
872 for(
unsigned int a=0;
a<Ampls.size();
a++){
874 if(Ampls[
a] >=254)Saturation =
true;
877 if(FirstStrip==0 )Overlapping=
true;
878 if(FirstStrip==128 )Overlapping=
true;
879 if(FirstStrip==256 )Overlapping=
true;
880 if(FirstStrip==384 )Overlapping=
true;
881 if(FirstStrip==512 )Overlapping=
true;
882 if(FirstStrip==640 )Overlapping=
true;
884 if(FirstStrip<=127 && FirstStrip+Ampls.size()>127)Overlapping=
true;
885 if(FirstStrip<=255 && FirstStrip+Ampls.size()>255)Overlapping=
true;
886 if(FirstStrip<=383 && FirstStrip+Ampls.size()>383)Overlapping=
true;
887 if(FirstStrip<=511 && FirstStrip+Ampls.size()>511)Overlapping=
true;
888 if(FirstStrip<=639 && FirstStrip+Ampls.size()>639)Overlapping=
true;
890 if(FirstStrip+Ampls.size()==127 )Overlapping=
true;
891 if(FirstStrip+Ampls.size()==255 )Overlapping=
true;
892 if(FirstStrip+Ampls.size()==383 )Overlapping=
true;
893 if(FirstStrip+Ampls.size()==511 )Overlapping=
true;
894 if(FirstStrip+Ampls.size()==639 )Overlapping=
true;
895 if(FirstStrip+Ampls.size()==767 )Overlapping=
true;
899 if(Overlapping ==
true )
continue;
907 bool Saturation =
false;
908 for(
unsigned int s=0;
s<Ampls.size();
s++){
909 int StripCharge = Ampls[
s];
913 if(StripCharge>1024){
916 }
else if(StripCharge>254){
920 Charge += StripCharge;
929 double ClusterChargeOverPath = ( (double) Charge )/
Path ;
930 if(
Validation) {ClusterChargeOverPath/=PrevGain;}
947 }
else if(APV->
Eta>0){
969 if (dynamic_cast<const StripGeomDetUnit*>(it)==0 && dynamic_cast<const PixelGeomDetUnit*>(it)==0) {
970 std::cout <<
"this detID doesn't seem to belong to the Tracker" << std::endl;
975 const TrapezoidalPlaneBounds* trapezoidalBounds( dynamic_cast<const TrapezoidalPlaneBounds*>(&(plane.bounds())));
976 const RectangularPlaneBounds* rectangularBounds( dynamic_cast<const RectangularPlaneBounds*>(&(plane.bounds())));
978 double DistFromBorder = 1.0;
981 if(trapezoidalBounds)
983 std::array<const float, 4>
const &
parameters = (*trapezoidalBounds).parameters();
985 HalfLength = parameters[3];
986 }
else if(rectangularBounds){
990 if (fabs(HitLocalPos.
y())+HitLocalError.
yy() >= (HalfLength - DistFromBorder) )
return false;
double p() const
momentum vector magnitude
T getParameter(std::string const &) const
virtual int nstrips() const =0
T getUntrackedParameter(std::string const &, T const &) const
unsigned int MinTrackHits
MonitorElement * Charge_Vs_PathlengthTIB
virtual float length() const =0
edm::InputTag theTracksLabel
SiStripApvGain * getNewObject() override
friend struct const_iterator
virtual void algoBeginRun(const edm::Run &run, const edm::EventSetup &iSetup) override
~SiStripGainFromCalibTree()
#define DEFINE_FWK_MODULE(type)
LocalVector localDirection() const
LocalPoint localPosition() const
MonitorElement * Charge_Vs_PathlengthTECM2
double tagCondition_GoodFrac
SiStripGainFromCalibTree(const edm::ParameterSet &)
MonitorElement * Charge_Vs_PathlengthTOB
const Bounds & bounds() const
T * make(const Args &...args) const
make new ROOT object
uint16_t firstStrip() const
const Plane & surface() const
The nominal surface of the GeomDet.
LocalError positionError() const
void MakeCalibrationMap()
double MaxTrackChiOverNdf
bool put(const uint32_t &detID, Range input)
uint32_t rawId() const
get the raw id
std::vector< stAPVGain * > APVsCollOrdered
DataContainer const & measurements() const
__gnu_cxx::hash_map< unsigned int, stAPVGain *, __gnu_cxx::hash< unsigned int >, isEqual > APVsColl
MonitorElement * Charge_Vs_PathlengthTECM1
void algoComputeMPVandGain()
MonitorElement * Charge_Vs_PathlengthTECP2
MonitorElement * Charge_Vs_PathlengthTECP1
double eta() const
pseudorapidity of momentum vector
tuple path
else: Piece not in the list, fine.
double chi2() const
chi-squared of the fit
bool IsGoodLandauFit(double *FitResults)
double ndof() const
number of degrees of freedom of the fit
MonitorElement * Charge_Vs_Index_Absolute
std::pair< ContainerIterator, ContainerIterator > Range
const std::complex< double > I
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
unsigned short numberOfValidHits() const
number of valid hits found
const LocalTrajectoryError & localError() const
int subdetId() const
get the contents of the subdetector field (not cast into any detector's numbering enum) ...
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
vector< string > VInputFiles
virtual void algoEndJob() override
virtual void algoAnalyze(const edm::Event &, const edm::EventSetup &) override
bool IsFarFromBorder(TrajectoryStateOnSurface *trajState, const uint32_t detid, const edm::EventSetup *iSetup)
MonitorElement * Charge_Vs_PathlengthTIDM
void getPeakOfLandau(TH1 *InputHisto, double *FitResults, double LowRange=50, double HighRange=5400)
void algoAnalyzeTheTree()
void setDoStore(const bool doStore)
When set to false the payload will not be written to the db.
MonitorElement * Charge_Vs_Index
double tagCondition_NClusters
volatile std::atomic< bool > shutdown_flag false
TH2F * getTH2F(void) const
MonitorElement * Charge_Vs_PathlengthTIDP
const std::vector< uint8_t > & amplitudes() const