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;
382 FitResults[0] = -0.5;
384 FitResults[2] = -0.5;
386 FitResults[4] = -0.5;
391 TF1* MyLandau =
new TF1(
"MyLandau",
"landau",LowRange, HighRange);
392 MyLandau->SetParameter(1,300);
393 InputHisto->Fit(MyLandau,
"0QR WW");
396 FitResults[0] = MyLandau->GetParameter(1);
397 FitResults[1] = MyLandau->GetParError(1);
398 FitResults[2] = MyLandau->GetParameter(2);
399 FitResults[3] = MyLandau->GetParError(2);
400 FitResults[4] = MyLandau->GetChisquare() / MyLandau->GetNDF();
406 if(FitResults[0] <= 0 )
return false;
416 printf(
"Openning file %3i/%3i --> %s\n",
i+1, (
int)
VInputFiles.size(), (
char*)(
VInputFiles[
i].c_str())); fflush(stdout);
417 TChain*
tree =
new TChain(
"gainCalibrationTree/tree");
420 TString EventPrefix(
"");
421 TString EventSuffix(
"");
423 TString TrackPrefix(
"track");
424 TString TrackSuffix(
"");
426 TString CalibPrefix(
"GainCalibration");
427 TString CalibSuffix(
"");
429 unsigned int eventnumber = 0; tree->SetBranchAddress(EventPrefix +
"event" + EventSuffix, &eventnumber ,
NULL);
430 unsigned int runnumber = 0; tree->SetBranchAddress(EventPrefix +
"run" + EventSuffix, &runnumber ,
NULL);
431 std::vector<bool>* TrigTech = 0; tree->SetBranchAddress(EventPrefix +
"TrigTech" + EventSuffix, &TrigTech ,
NULL);
433 std::vector<double>* trackchi2ndof = 0; tree->SetBranchAddress(TrackPrefix +
"chi2ndof" + TrackSuffix, &trackchi2ndof ,
NULL);
434 std::vector<float>* trackp = 0; tree->SetBranchAddress(TrackPrefix +
"momentum" + TrackSuffix, &trackp ,
NULL);
435 std::vector<float>* trackpt = 0; tree->SetBranchAddress(TrackPrefix +
"pt" + TrackSuffix, &trackpt ,
NULL);
436 std::vector<double>* tracketa = 0; tree->SetBranchAddress(TrackPrefix +
"eta" + TrackSuffix, &tracketa ,
NULL);
437 std::vector<double>* trackphi = 0; tree->SetBranchAddress(TrackPrefix +
"phi" + TrackSuffix, &trackphi ,
NULL);
438 std::vector<unsigned int>* trackhitsvalid = 0; tree->SetBranchAddress(TrackPrefix +
"hitsvalid" + TrackSuffix, &trackhitsvalid,
NULL);
440 std::vector<int>* trackindex = 0; tree->SetBranchAddress(CalibPrefix +
"trackindex" + CalibSuffix, &trackindex ,
NULL);
441 std::vector<unsigned int>* rawid = 0; tree->SetBranchAddress(CalibPrefix +
"rawid" + CalibSuffix, &rawid ,
NULL);
442 std::vector<float>* localdirx = 0; tree->SetBranchAddress(CalibPrefix +
"localdirx" + CalibSuffix, &localdirx ,
NULL);
443 std::vector<float>* localdiry = 0; tree->SetBranchAddress(CalibPrefix +
"localdiry" + CalibSuffix, &localdiry ,
NULL);
444 std::vector<float>* localdirz = 0; tree->SetBranchAddress(CalibPrefix +
"localdirz" + CalibSuffix, &localdirz ,
NULL);
445 std::vector<unsigned short>* firststrip = 0; tree->SetBranchAddress(CalibPrefix +
"firststrip" + CalibSuffix, &firststrip ,
NULL);
446 std::vector<unsigned short>* nstrips = 0; tree->SetBranchAddress(CalibPrefix +
"nstrips" + CalibSuffix, &nstrips ,
NULL);
447 std::vector<bool>* saturation = 0; tree->SetBranchAddress(CalibPrefix +
"saturation" + CalibSuffix, &saturation ,
NULL);
448 std::vector<bool>* overlapping = 0; tree->SetBranchAddress(CalibPrefix +
"overlapping" + CalibSuffix, &overlapping ,
NULL);
449 std::vector<bool>* farfromedge = 0; tree->SetBranchAddress(CalibPrefix +
"farfromedge" + CalibSuffix, &farfromedge ,
NULL);
450 std::vector<unsigned int>*
charge = 0; tree->SetBranchAddress(CalibPrefix +
"charge" + CalibSuffix, &charge ,
NULL);
451 std::vector<float>*
path = 0; tree->SetBranchAddress(CalibPrefix +
"path" + CalibSuffix, &path ,
NULL);
452 std::vector<float>* chargeoverpath = 0; tree->SetBranchAddress(CalibPrefix +
"chargeoverpath" + CalibSuffix, &chargeoverpath,
NULL);
453 std::vector<unsigned char>* amplitude = 0; tree->SetBranchAddress(CalibPrefix +
"amplitude" + CalibSuffix, &litude ,
NULL);
454 std::vector<double>* gainused = 0; tree->SetBranchAddress(CalibPrefix +
"gainused" + CalibSuffix, &gainused ,
NULL);
457 printf(
"Number of Events = %i + %i = %i\n",
NEvent,(
unsigned int)tree->GetEntries(),(
unsigned int)(
NEvent+tree->GetEntries()));
458 printf(
"Progressing Bar :0%% 20%% 40%% 60%% 80%% 100%%\n");
459 printf(
"Looping on the Tree :");
460 int TreeStep = tree->GetEntries()/50;
if(TreeStep<=1)TreeStep=1;
461 for (
unsigned int ientry = 0; ientry < tree->GetEntries(); ientry++) {
462 if(ientry%TreeStep==0){printf(
".");fflush(stdout);}
463 tree->GetEntry(ientry);
471 unsigned int FirstAmplitude=0;
472 for(
unsigned int i=0;
i<(*chargeoverpath).size();
i++){
473 FirstAmplitude+=(*nstrips)[
i];
474 int TI = (*trackindex)[
i];
481 if((*farfromedge)[
i] ==
false )
continue;
482 if((*overlapping)[
i] ==
true )
continue;
502 bool Saturation =
false;
503 for(
unsigned int s=0;
s<(*nstrips)[
i];
s++){
504 int StripCharge = (*amplitude)[FirstAmplitude-(*nstrips)[
i]+
s];
508 if(StripCharge>1024){
511 }
else if(StripCharge>254){
515 Charge += StripCharge;
519 Charge = (*charge)[
i];
524 double ClusterChargeOverPath = ( (double) Charge )/(*path)[
i] ;
525 if(
Validation) {ClusterChargeOverPath/=(*gainused)[
i];}
542 }
else if(APV->
Eta>0){
560 double FitResults[5];
561 double MPVmean = 300;
566 printf(
"Progressing Bar :0%% 20%% 40%% 60%% 80%% 100%%\n");
567 printf(
"Fitting Charge Distribution :");
570 if(I%TreeStep==0){printf(
".");fflush(stdout);}
572 if(APV->
Bin<0) APV->
Bin = chvsidx->GetXaxis()->FindBin(APV->
Index);
576 Proj = (TH1F*)(chvsidx->ProjectionY(
"",chvsidx->GetXaxis()->FindBin(APV->
Index),chvsidx->GetXaxis()->FindBin(APV->
Index),
"e"));
581 int SecondAPVId = APV->
APVId;
582 if(SecondAPVId%2==0){ SecondAPVId = SecondAPVId+1; }
else{ SecondAPVId = SecondAPVId-1; }
584 if(APV2->
Bin<0) APV2->
Bin = chvsidx->GetXaxis()->FindBin(APV2->
Index);
585 TH1F* Proj2 = (TH1F*)(chvsidx->ProjectionY(
"",APV2->
Bin,APV2->
Bin,
"e"));
586 if(Proj2){Proj->Add(Proj2,1);
delete Proj2;}
588 for(
unsigned int i=0;
i<6;
i++){
589 __gnu_cxx::hash_map<unsigned int, stAPVGain*, __gnu_cxx::hash<unsigned int>,
isEqual >::iterator tmpit;
594 if(APV2->
Bin<0) APV2->
Bin = chvsidx->GetXaxis()->FindBin(APV2->
Index);
595 TH1F* Proj2 = (TH1F*)(chvsidx->ProjectionY(
"",APV2->
Bin,APV2->
Bin,
"e"));
596 if(Proj2){Proj->Add(Proj2,1);
delete Proj2;}
604 APV->
FitMPV = FitResults[0];
630 unsigned int tree_Index;
631 unsigned int tree_Bin;
632 unsigned int tree_DetId;
633 unsigned char tree_APVId;
634 unsigned char tree_SubDet;
641 float tree_Thickness;
643 float tree_FitMPVErr;
645 float tree_FitWidthErr;
646 float tree_FitChi2NDF;
648 double tree_PrevGain;
649 double tree_NEntries;
653 MyTree =
tfs->
make<TTree> (
"APVGain",
"APVGain");
654 MyTree->Branch(
"Index" ,&tree_Index ,
"Index/i");
655 MyTree->Branch(
"Bin" ,&tree_Bin ,
"Bin/i");
656 MyTree->Branch(
"DetId" ,&tree_DetId ,
"DetId/i");
657 MyTree->Branch(
"APVId" ,&tree_APVId ,
"APVId/b");
658 MyTree->Branch(
"SubDet" ,&tree_SubDet ,
"SubDet/b");
659 MyTree->Branch(
"x" ,&tree_x ,
"x/F");
660 MyTree->Branch(
"y" ,&tree_y ,
"y/F");
661 MyTree->Branch(
"z" ,&tree_z ,
"z/F");
662 MyTree->Branch(
"Eta" ,&tree_Eta ,
"Eta/F");
663 MyTree->Branch(
"R" ,&tree_R ,
"R/F");
664 MyTree->Branch(
"Phi" ,&tree_Phi ,
"Phi/F");
665 MyTree->Branch(
"Thickness" ,&tree_Thickness ,
"Thickness/F");
666 MyTree->Branch(
"FitMPV" ,&tree_FitMPV ,
"FitMPV/F");
667 MyTree->Branch(
"FitMPVErr" ,&tree_FitMPVErr ,
"FitMPVErr/F");
668 MyTree->Branch(
"FitWidth" ,&tree_FitWidth ,
"FitWidth/F");
669 MyTree->Branch(
"FitWidthErr" ,&tree_FitWidthErr,
"FitWidthErr/F");
670 MyTree->Branch(
"FitChi2NDF" ,&tree_FitChi2NDF ,
"FitChi2NDF/F");
671 MyTree->Branch(
"Gain" ,&tree_Gain ,
"Gain/D");
672 MyTree->Branch(
"PrevGain" ,&tree_PrevGain ,
"PrevGain/D");
673 MyTree->Branch(
"NEntries" ,&tree_NEntries ,
"NEntries/D");
674 MyTree->Branch(
"isMasked" ,&tree_isMasked ,
"isMasked/O");
677 FILE* Gains = stdout;
679 fprintf(Gains,
"NEvents = %i\n",
NEvent);
680 fprintf(Gains,
"NTracks = %i\n",
NTrack);
681 fprintf(Gains,
"NClusters = %i\n",
NCluster);
682 fprintf(Gains,
"Number of APVs = %lu\n",static_cast<unsigned long>(
APVsColl.size()));
683 fprintf(Gains,
"GoodFits = %i BadFits = %i ratio = %f\n",
GOOD,
BAD,(100.0*
GOOD)/(
GOOD+
BAD));
686 fprintf(Gains,
"NEvents = %i\n",
NEvent);
687 fprintf(Gains,
"NTracks = %i\n",
NTrack);
688 fprintf(Gains,
"NClusters = %i\n",
NCluster);
689 fprintf(Gains,
"Number of APVs = %lu\n",static_cast<unsigned long>(
APVsColl.size()));
690 fprintf(Gains,
"GoodFits = %i BadFits = %i ratio = %f\n",
GOOD,
BAD,(100.0*
GOOD)/(
GOOD+
BAD));
694 if(APV==
NULL)
continue;
698 tree_Index = APV->
Index;
700 tree_DetId = APV->
DetId;
701 tree_APVId = APV->
APVId;
702 tree_SubDet = APV->
SubDet;
710 tree_FitMPV = APV->
FitMPV;
714 tree_FitChi2NDF = APV->
FitChi2;
715 tree_Gain = APV->
Gain;
731 cout <<
"[SiStripGainFromCalibTree] produceTagFilter -> Return false: Statistics is too low : " <<
Charge_Vs_Index->
getTH2F()->Integral() << endl;
735 cout <<
"[SiStripGainFromCalibTree] produceTagFilter -> Return false: ratio of GOOD/TOTAL is too low: " << (1.0 *
GOOD) / (
GOOD+
BAD) << endl;
747 cout <<
"[SiStripGainFromCalibTree] getNewObject -> will not produce a paylaod because produceTagFilter returned false " << endl;
753 std::vector<float>* theSiStripVector =
NULL;
754 unsigned int PreviousDetId = 0;
758 if(APV==
NULL){ printf(
"Bug\n");
continue; }
759 if(APV->
DetId != PreviousDetId){
760 if(theSiStripVector!=
NULL){
762 if ( !obj->
put(PreviousDetId,range) ) printf(
"Bug to put detId = %i\n",PreviousDetId);
764 theSiStripVector =
new std::vector<float>;
765 PreviousDetId = APV->
DetId;
767 theSiStripVector->push_back(APV->
Gain);
769 if(theSiStripVector!=
NULL){
771 if ( !obj->
put(PreviousDetId,range) ) printf(
"Bug to put detId = %i\n",PreviousDetId);
785 TChain* t1 =
new TChain(
"SiStripCalib/APVGain");
788 unsigned int tree_DetId;
789 unsigned char tree_APVId;
792 t1->SetBranchAddress(
"DetId" ,&tree_DetId );
793 t1->SetBranchAddress(
"APVId" ,&tree_APVId );
794 t1->SetBranchAddress(
"Gain" ,&tree_Gain );
796 for (
unsigned int ientry = 0; ientry < t1->GetEntries(); ientry++) {
797 t1->GetEntry(ientry);
824 const Trajectory* traj = association->key.get();
836 vector<TrajectoryMeasurement> measurements = traj->
measurements();
837 for(vector<TrajectoryMeasurement>::const_iterator measurement_it = measurements.begin(); measurement_it!=measurements.end(); measurement_it++){
839 if( !trajState.
isValid() )
continue;
849 for(
unsigned int h=0;
h<2;
h++){
850 if(!sistripmatchedhit &&
h==1){
852 }
else if(sistripmatchedhit &&
h==0){
854 DetId = sistripmatchedhit->
monoId();
855 }
else if(sistripmatchedhit &&
h==1){
857 DetId = sistripmatchedhit->
stereoId();
858 }
else if(sistripsimplehit){
859 Cluster = (sistripsimplehit->
cluster()).
get();
861 }
else if(sistripsimple1dhit){
862 Cluster = (sistripsimple1dhit->
cluster()).
get();
869 double cosine = trackDirection.
z()/trackDirection.
mag();
872 int APVId = FirstStrip/128;
873 bool Saturation =
false;
874 bool Overlapping =
false;
876 double PrevGain = -1;
883 PrevGain = *(detGainRange.first + APVId);
886 for(
unsigned int a=0;
a<Ampls.size();
a++){
888 if(Ampls[
a] >=254)Saturation =
true;
891 if(FirstStrip==0 )Overlapping=
true;
892 if(FirstStrip==128 )Overlapping=
true;
893 if(FirstStrip==256 )Overlapping=
true;
894 if(FirstStrip==384 )Overlapping=
true;
895 if(FirstStrip==512 )Overlapping=
true;
896 if(FirstStrip==640 )Overlapping=
true;
898 if(FirstStrip<=127 && FirstStrip+Ampls.size()>127)Overlapping=
true;
899 if(FirstStrip<=255 && FirstStrip+Ampls.size()>255)Overlapping=
true;
900 if(FirstStrip<=383 && FirstStrip+Ampls.size()>383)Overlapping=
true;
901 if(FirstStrip<=511 && FirstStrip+Ampls.size()>511)Overlapping=
true;
902 if(FirstStrip<=639 && FirstStrip+Ampls.size()>639)Overlapping=
true;
904 if(FirstStrip+Ampls.size()==127 )Overlapping=
true;
905 if(FirstStrip+Ampls.size()==255 )Overlapping=
true;
906 if(FirstStrip+Ampls.size()==383 )Overlapping=
true;
907 if(FirstStrip+Ampls.size()==511 )Overlapping=
true;
908 if(FirstStrip+Ampls.size()==639 )Overlapping=
true;
909 if(FirstStrip+Ampls.size()==767 )Overlapping=
true;
913 if(Overlapping ==
true )
continue;
921 bool Saturation =
false;
922 for(
unsigned int s=0;
s<Ampls.size();
s++){
923 int StripCharge = Ampls[
s];
927 if(StripCharge>1024){
930 }
else if(StripCharge>254){
934 Charge += StripCharge;
943 double ClusterChargeOverPath = ( (double) Charge )/
Path ;
944 if(
Validation) {ClusterChargeOverPath/=PrevGain;}
961 }
else if(APV->
Eta>0){
983 if (dynamic_cast<const StripGeomDetUnit*>(it)==0 && dynamic_cast<const PixelGeomDetUnit*>(it)==0) {
984 std::cout <<
"this detID doesn't seem to belong to the Tracker" << std::endl;
989 const TrapezoidalPlaneBounds* trapezoidalBounds( dynamic_cast<const TrapezoidalPlaneBounds*>(&(plane.bounds())));
990 const RectangularPlaneBounds* rectangularBounds( dynamic_cast<const RectangularPlaneBounds*>(&(plane.bounds())));
992 double DistFromBorder = 1.0;
995 if(trapezoidalBounds)
997 std::array<const float, 4>
const &
parameters = (*trapezoidalBounds).parameters();
999 HalfLength = parameters[3];
1000 }
else if(rectangularBounds){
1002 }
else{
return false;}
1004 if (fabs(HitLocalPos.
y())+HitLocalError.
yy() >= (HalfLength - DistFromBorder) )
return false;
ClusterRef cluster() const
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
unsigned int stereoId() const
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)
SiStripCluster const & monoCluster() const
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
ClusterRef cluster() const
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.
SiStripCluster const & stereoCluster() const
MonitorElement * Charge_Vs_Index
double tagCondition_NClusters
unsigned int monoId() const
DetId geographicalId() const
volatile std::atomic< bool > shutdown_flag false
TH2F * getTH2F(void) const
MonitorElement * Charge_Vs_PathlengthTIDP
const std::vector< uint8_t > & amplitudes() const