Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-07-10 22:32:50

0001 // Original Author:  Loic QUERTENMONT
0002 //         Created:  Wed Feb  6 08:55:18 CET 2008
0003 
0004 // system includes
0005 #include <memory>
0006 #include <unordered_map>
0007 
0008 // user includes
0009 #include "CalibFormats/SiStripObjects/interface/SiStripDetCabling.h"
0010 #include "CalibFormats/SiStripObjects/interface/SiStripGain.h"
0011 #include "CalibTracker/Records/interface/SiStripDetCablingRcd.h"
0012 #include "CalibTracker/Records/interface/SiStripGainRcd.h"
0013 #include "CalibTracker/SiStripChannelGain/interface/APVGainStruct.h"
0014 #include "CommonTools/ConditionDBWriter/interface/ConditionDBWriter.h"
0015 #include "CondFormats/SiStripObjects/interface/SiStripApvGain.h"
0016 #include "DQMServices/Core/interface/DQMStore.h"
0017 #include "DataFormats/DetId/interface/DetId.h"
0018 #include "DataFormats/FEDRawData/interface/FEDNumbering.h"
0019 #include "DataFormats/GeometrySurface/interface/RectangularPlaneBounds.h"
0020 #include "DataFormats/GeometrySurface/interface/TrapezoidalPlaneBounds.h"
0021 #include "DataFormats/SiStripCluster/interface/SiStripClusterCollection.h"
0022 #include "DataFormats/SiStripDetId/interface/StripSubdetector.h"
0023 #include "DataFormats/TrackReco/interface/DeDxHit.h"
0024 #include "DataFormats/TrackReco/interface/Track.h"
0025 #include "DataFormats/TrackReco/interface/TrackDeDxHits.h"
0026 #include "DataFormats/TrackReco/interface/TrackFwd.h"
0027 #include "DataFormats/TrackerCommon/interface/TrackerTopology.h"
0028 #include "DataFormats/TrackerRecHit2D/interface/SiStripMatchedRecHit2D.h"
0029 #include "DataFormats/TrackerRecHit2D/interface/SiStripRecHit1D.h"
0030 #include "DataFormats/TrackerRecHit2D/interface/SiStripRecHit2D.h"
0031 #include "FWCore/Framework/interface/Event.h"
0032 #include "FWCore/Framework/interface/Frameworkfwd.h"
0033 #include "FWCore/Framework/interface/MakerMacros.h"
0034 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0035 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0036 #include "FWCore/Utilities/interface/Exception.h"
0037 #include "Geometry/CommonDetUnit/interface/GeomDet.h"
0038 #include "Geometry/CommonDetUnit/interface/GeomDetType.h"
0039 #include "Geometry/CommonDetUnit/interface/PixelGeomDetUnit.h"
0040 #include "Geometry/CommonDetUnit/interface/TrackingGeometry.h"
0041 #include "Geometry/CommonTopologies/interface/StripTopology.h"
0042 #include "Geometry/Records/interface/TrackerDigiGeometryRecord.h"
0043 #include "Geometry/Records/interface/TrackerTopologyRcd.h"
0044 #include "Geometry/TrackerGeometryBuilder/interface/StripGeomDetUnit.h"
0045 #include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
0046 #include "Geometry/TrackerNumberingBuilder/interface/GeometricDet.h"
0047 #include "TrackingTools/PatternTools/interface/TrajTrackAssociation.h"
0048 #include "TrackingTools/PatternTools/interface/Trajectory.h"
0049 
0050 // ROOT inclues
0051 #include "TFile.h"
0052 #include "TObjString.h"
0053 #include "TString.h"
0054 #include "TH1F.h"
0055 #include "TH2F.h"
0056 #include "TProfile.h"
0057 #include "TF1.h"
0058 #include "TROOT.h"
0059 
0060 using namespace edm;
0061 using namespace reco;
0062 using namespace std;
0063 
0064 class SiStripGainFromData : public ConditionDBWriter<SiStripApvGain> {
0065 public:
0066   typedef dqm::legacy::MonitorElement MonitorElement;
0067   typedef dqm::legacy::DQMStore DQMStore;
0068   explicit SiStripGainFromData(const edm::ParameterSet&);
0069   ~SiStripGainFromData() override = default;
0070 
0071 private:
0072   void algoBeginJob(const edm::EventSetup&) override;
0073   void algoEndJob() override;
0074   void algoBeginRun(const edm::Run&, const edm::EventSetup&) override;
0075   //      virtual void algoBeginRun(const edm::Event& iEvent, const edm::EventSetup& iSetup);
0076   void algoAnalyze(const edm::Event&, const edm::EventSetup&) override;
0077 
0078   std::unique_ptr<SiStripApvGain> getNewObject() override;
0079   DQMStore* dqmStore_;
0080 
0081   double ComputeChargeOverPath(const SiStripCluster* Cluster,
0082                                TrajectoryStateOnSurface trajState,
0083                                const edm::EventSetup* iSetup,
0084                                const Track* track,
0085                                double trajChi2OverN);
0086   bool IsFarFromBorder(TrajectoryStateOnSurface trajState, const uint32_t detid, const edm::EventSetup* iSetup);
0087 
0088   void getPeakOfLandau(TH1* InputHisto, double* FitResults, double LowRange = 0, double HighRange = 5400);
0089 
0090   const edm::EventSetup* iSetup_;
0091   const edm::Event* iEvent_;
0092 
0093   bool CheckLocalAngle;
0094   unsigned int MinNrEntries;
0095   double MaxMPVError;
0096   double MaxChi2OverNDF;
0097   double MinTrackMomentum;
0098   double MaxTrackMomentum;
0099   double MinTrackEta;
0100   double MaxTrackEta;
0101   unsigned int MaxNrStrips;
0102   unsigned int MinTrackHits;
0103   double MaxTrackChiOverNdf;
0104   bool AllowSaturation;
0105   bool FirstSetOfConstants;
0106   bool Validation;
0107   int CalibrationLevel;
0108   bool CheckIfFileExist;
0109 
0110   std::string AlgoMode;
0111   std::string OutputGains;
0112   std::string OutputHistos;
0113   std::string TrajToTrackProducer;
0114   std::string TrajToTrackLabel;
0115 
0116   vector<string> VInputFiles;
0117 
0118   MonitorElement* tmp;
0119 
0120   TH2F* Tracks_P_Vs_Eta;
0121   TH2F* Tracks_Pt_Vs_Eta;
0122 
0123   TH1F* NumberOfEntriesByAPV;
0124   TH1F* HChi2OverNDF;
0125   TH1F* HTrackChi2OverNDF;
0126   TH1F* HTrackHits;
0127 
0128   TH2F* MPV_Vs_EtaTIB;
0129   TH2F* MPV_Vs_EtaTID;
0130   TH2F* MPV_Vs_EtaTOB;
0131   TH2F* MPV_Vs_EtaTEC;
0132   TH2F* MPV_Vs_EtaTEC1;
0133   TH2F* MPV_Vs_EtaTEC2;
0134 
0135   TH2F* MPV_Vs_PhiTIB;
0136   TH2F* MPV_Vs_PhiTID;
0137   TH2F* MPV_Vs_PhiTOB;
0138   TH2F* MPV_Vs_PhiTEC;
0139   TH2F* MPV_Vs_PhiTEC1;
0140   TH2F* MPV_Vs_PhiTEC2;
0141 
0142   TH2F* Charge_Vs_PathTIB;
0143   TH2F* Charge_Vs_PathTID;
0144   TH2F* Charge_Vs_PathTOB;
0145   TH2F* Charge_Vs_PathTEC;
0146   TH2F* Charge_Vs_PathTEC1;
0147   TH2F* Charge_Vs_PathTEC2;
0148 
0149   TH1F* MPV_Vs_PathTIB;
0150   TH1F* MPV_Vs_PathTID;
0151   TH1F* MPV_Vs_PathTOB;
0152   TH1F* MPV_Vs_PathTEC;
0153   TH1F* MPV_Vs_PathTEC1;
0154   TH1F* MPV_Vs_PathTEC2;
0155 
0156   TH1F* Charge_TIB;
0157   TH1F* Charge_TID;
0158   TH1F* Charge_TIDP;
0159   TH1F* Charge_TIDM;
0160   TH1F* Charge_TOB;
0161   TH1F* Charge_TEC;
0162   TH1F* Charge_TEC1;
0163   TH1F* Charge_TEC2;
0164   TH1F* Charge_TECP;
0165   TH1F* Charge_TECM;
0166 
0167   TH2F* MPV_Vs_Phi;
0168   TH2F* MPV_Vs_Eta;
0169   TH2F* MPV_Vs_R;
0170 
0171   //      TH2F*        PD_Vs_Eta;
0172   //      TH2F*        PD_Vs_R;
0173 
0174   TH1F* APV_DetId;
0175   TH1F* APV_Id;
0176   TH1F* APV_Eta;
0177   TH1F* APV_R;
0178   TH1F* APV_SubDet;
0179   TH2F* APV_Momentum;
0180   TH2F* APV_Charge;
0181   TH2F* APV_PathLength;
0182   TH1F* APV_PathLengthM;
0183   TH1F* APV_MPV;
0184   TH1F* APV_Gain;
0185   TH1F* APV_CumulGain;
0186   TH1F* APV_PrevGain;
0187   TH1F* APV_Thickness;
0188 
0189   TH1F* MPVs;
0190   TH1F* MPVs320;
0191   TH1F* MPVs500;
0192 
0193   //      TH2F*        MPV_vs_10RplusEta;
0194 
0195   TH1F* NSatStripInCluster;
0196   TH1F* NHighStripInCluster;
0197   //      TH2F*        Charge_Vs_PathLength_CS1;
0198   //      TH2F*        Charge_Vs_PathLength_CS2;
0199   //      TH2F*        Charge_Vs_PathLength_CS3;
0200   //      TH2F*        Charge_Vs_PathLength_CS4;
0201   //      TH2F*        Charge_Vs_PathLength_CS5;
0202 
0203   //      TH1F*        MPV_Vs_PathLength_CS1;
0204   //      TH1F*        MPV_Vs_PathLength_CS2;
0205   //      TH1F*        MPV_Vs_PathLength_CS3;
0206   //      TH1F*        MPV_Vs_PathLength_CS4;
0207   //      TH1F*        MPV_Vs_PathLength_CS5;
0208 
0209   //      TH1F*        FWHM_Vs_PathLength_CS1;
0210   //      TH1F*        FWHM_Vs_PathLength_CS2;
0211   //      TH1F*        FWHM_Vs_PathLength_CS3;
0212   //      TH1F*        FWHM_Vs_PathLength_CS4;
0213   //      TH1F*        FWHM_Vs_PathLength_CS5;
0214 
0215   TH2F* Charge_Vs_PathLength;
0216   TH2F* Charge_Vs_PathLength320;
0217   TH2F* Charge_Vs_PathLength500;
0218 
0219   TH1F* MPV_Vs_PathLength;
0220   TH1F* MPV_Vs_PathLength320;
0221   TH1F* MPV_Vs_PathLength500;
0222 
0223   TH1F* FWHM_Vs_PathLength;
0224   TH1F* FWHM_Vs_PathLength320;
0225   TH1F* FWHM_Vs_PathLength500;
0226 
0227   TH2F* Charge_Vs_TransversAngle;
0228   TH1F* MPV_Vs_TransversAngle;
0229   TH2F* NStrips_Vs_TransversAngle;
0230 
0231   TH2F* Charge_Vs_Alpha;
0232   TH1F* MPV_Vs_Alpha;
0233   TH2F* NStrips_Vs_Alpha;
0234 
0235   TH2F* Charge_Vs_Beta;
0236   TH1F* MPV_Vs_Beta;
0237   TH2F* NStrips_Vs_Beta;
0238 
0239   TH2F* Error_Vs_MPV;
0240   TH2F* Error_Vs_Entries;
0241   TH2F* Error_Vs_Eta;
0242   TH2F* Error_Vs_Phi;
0243 
0244   TH2F* NoMPV_Vs_EtaPhi;
0245 
0246   TH2F* HitLocalPosition;
0247   TH2F* HitLocalPositionBefCut;
0248 
0249   TH1F* JobInfo;
0250 
0251   TH1F* HFirstStrip;
0252 
0253   unsigned int NEvent;
0254   unsigned int SRun;
0255   unsigned int SEvent;
0256   TimeValue_t STimestamp;
0257   unsigned int ERun;
0258   unsigned int EEvent;
0259   TimeValue_t ETimestamp;
0260 
0261 private:
0262   class isEqual {
0263   public:
0264     template <class T>
0265     bool operator()(const T& PseudoDetId1, const T& PseudoDetId2) {
0266       return PseudoDetId1 == PseudoDetId2;
0267     }
0268   };
0269 
0270   std::vector<stAPVGain*> APVsCollOrdered;
0271   std::unordered_map<unsigned int, stAPVGain*> APVsColl;
0272 
0273   edm::ESGetToken<TrackerTopology, TrackerTopologyRcd> tTopoToken_;
0274   edm::ESGetToken<TrackerGeometry, TrackerDigiGeometryRecord> tkGeomToken_;
0275   edm::ESGetToken<SiStripGain, SiStripGainRcd> gainToken_;
0276 };
0277 
0278 SiStripGainFromData::SiStripGainFromData(const edm::ParameterSet& iConfig)
0279     : ConditionDBWriter<SiStripApvGain>(iConfig) {
0280   AlgoMode = iConfig.getParameter<std::string>("AlgoMode");
0281 
0282   OutputGains = iConfig.getParameter<std::string>("OutputGains");
0283   OutputHistos = iConfig.getParameter<std::string>("OutputHistos");
0284 
0285   TrajToTrackProducer = iConfig.getParameter<std::string>("TrajToTrackProducer");
0286   TrajToTrackLabel = iConfig.getParameter<std::string>("TrajToTrackLabel");
0287 
0288   CheckLocalAngle = iConfig.getUntrackedParameter<bool>("checkLocalAngle", false);
0289   MinNrEntries = iConfig.getUntrackedParameter<unsigned>("minNrEntries", 20);
0290   MaxMPVError = iConfig.getUntrackedParameter<double>("maxMPVError", 500.0);
0291   MaxChi2OverNDF = iConfig.getUntrackedParameter<double>("maxChi2OverNDF", 5.0);
0292   MinTrackMomentum = iConfig.getUntrackedParameter<double>("minTrackMomentum", 3.0);
0293   MaxTrackMomentum = iConfig.getUntrackedParameter<double>("maxTrackMomentum", 99999.0);
0294   MinTrackEta = iConfig.getUntrackedParameter<double>("minTrackEta", -5.0);
0295   MaxTrackEta = iConfig.getUntrackedParameter<double>("maxTrackEta", 5.0);
0296   MaxNrStrips = iConfig.getUntrackedParameter<unsigned>("maxNrStrips", 2);
0297   MinTrackHits = iConfig.getUntrackedParameter<unsigned>("MinTrackHits", 8);
0298   MaxTrackChiOverNdf = iConfig.getUntrackedParameter<double>("MaxTrackChiOverNdf", 3);
0299   AllowSaturation = iConfig.getUntrackedParameter<bool>("AllowSaturation", false);
0300   FirstSetOfConstants = iConfig.getUntrackedParameter<bool>("FirstSetOfConstants", true);
0301   Validation = iConfig.getUntrackedParameter<bool>("Validation", false);
0302   CheckIfFileExist = iConfig.getUntrackedParameter<bool>("CheckIfFileExist", false);
0303 
0304   CalibrationLevel = iConfig.getUntrackedParameter<int>("CalibrationLevel", 0);
0305 
0306   if (strcmp(AlgoMode.c_str(), "WriteOnDB") == 0)
0307     VInputFiles = iConfig.getParameter<vector<string> >("VInputFiles");
0308 
0309   dqmStore_ = edm::Service<DQMStore>().operator->();
0310 
0311   tTopoToken_ = esConsumes<edm::Transition::BeginRun>();
0312   tkGeomToken_ = esConsumes<edm::Transition::BeginRun>();
0313   gainToken_ = esConsumes<edm::Transition::BeginRun>();
0314 
0315   //if( OutputHistos!="" )
0316   //  dqmStore_->open(OutputHistos.c_str(), true);
0317 }
0318 
0319 void SiStripGainFromData::algoBeginJob(const edm::EventSetup& iSetup) {
0320   const TrackerTopology* const tTopo = &iSetup.getData(tTopoToken_);
0321 
0322   iSetup_ = &iSetup;
0323 
0324   //   TH1::AddDirectory(kTRUE);
0325 
0326   tmp = dqmStore_->book1D("JobInfo", "JobInfo", 20, 0, 20);
0327   JobInfo = tmp->getTH1F();
0328 
0329   tmp = dqmStore_->book1D("APV_DetId", "APV_DetId", 72785, 0, 72784);
0330   APV_DetId = tmp->getTH1F();
0331   tmp = dqmStore_->book1D("APV_Id", "APV_Id", 72785, 0, 72784);
0332   APV_Id = tmp->getTH1F();
0333   tmp = dqmStore_->book1D("APV_Eta", "APV_Eta", 72785, 0, 72784);
0334   APV_Eta = tmp->getTH1F();
0335   tmp = dqmStore_->book1D("APV_R", "APV_R", 72785, 0, 72784);
0336   APV_R = tmp->getTH1F();
0337   tmp = dqmStore_->book1D("APV_SubDet", "APV_SubDet", 72785, 0, 72784);
0338   APV_SubDet = tmp->getTH1F();
0339   tmp = dqmStore_->book2D("APV_Momentum", "APV_Momentum", 72785, 0, 72784, 50, 0, 200);
0340   APV_Momentum = tmp->getTH2F();
0341   tmp = dqmStore_->book2D("APV_Charge", "APV_Charge", 72785, 0, 72784, 1000, 0, 2000);
0342   APV_Charge = tmp->getTH2F();
0343   tmp = dqmStore_->book2D("APV_PathLength", "APV_PathLength", 72785, 0, 72784, 100, 0.2, 1.4);
0344   APV_PathLength = tmp->getTH2F();
0345   tmp = dqmStore_->book1D("APV_PathLengthM", "APV_PathLengthM", 72785, 0, 72784);
0346   APV_PathLengthM = tmp->getTH1F();
0347   tmp = dqmStore_->book1D("APV_MPV", "APV_MPV", 72785, 0, 72784);
0348   APV_MPV = tmp->getTH1F();
0349   tmp = dqmStore_->book1D("APV_Gain", "APV_Gain", 72785, 0, 72784);
0350   APV_Gain = tmp->getTH1F();
0351   tmp = dqmStore_->book1D("APV_PrevGain", "APV_PrevGain", 72785, 0, 72784);
0352   APV_PrevGain = tmp->getTH1F();
0353   tmp = dqmStore_->book1D("APV_CumulGain", "APV_CumulGain", 72785, 0, 72784);
0354   APV_CumulGain = tmp->getTH1F();
0355   tmp = dqmStore_->book1D("APV_Thickness", "APV_Thicknes", 72785, 0, 72784);
0356   APV_Thickness = tmp->getTH1F();
0357 
0358   tmp = dqmStore_->book2D("Tracks_P_Vs_Eta", "Tracks_P_Vs_Eta", 30, 0, 3, 50, 0, 200);
0359   Tracks_P_Vs_Eta = tmp->getTH2F();
0360   tmp = dqmStore_->book2D("Tracks_Pt_Vs_Eta", "Tracks_Pt_Vs_Eta", 30, 0, 3, 50, 0, 200);
0361   Tracks_Pt_Vs_Eta = tmp->getTH2F();
0362 
0363   tmp = dqmStore_->book2D("Charge_Vs_PathTIB", "Charge_Vs_PathTIB", 100, 0.2, 1.4, 500, 0, 2000);
0364   Charge_Vs_PathTIB = tmp->getTH2F();
0365   tmp = dqmStore_->book2D("Charge_Vs_PathTID", "Charge_Vs_PathTID", 100, 0.2, 1.4, 500, 0, 2000);
0366   Charge_Vs_PathTID = tmp->getTH2F();
0367   tmp = dqmStore_->book2D("Charge_Vs_PathTOB", "Charge_Vs_PathTOB", 100, 0.2, 1.4, 500, 0, 2000);
0368   Charge_Vs_PathTOB = tmp->getTH2F();
0369   tmp = dqmStore_->book2D("Charge_Vs_PathTEC", "Charge_Vs_PathTEC", 100, 0.2, 1.4, 500, 0, 2000);
0370   Charge_Vs_PathTEC = tmp->getTH2F();
0371   tmp = dqmStore_->book2D("Charge_Vs_PathTEC1", "Charge_Vs_PathTEC1", 100, 0.2, 1.4, 500, 0, 2000);
0372   Charge_Vs_PathTEC1 = tmp->getTH2F();
0373   tmp = dqmStore_->book2D("Charge_Vs_PathTEC2", "Charge_Vs_PathTEC2", 100, 0.2, 1.4, 500, 0, 2000);
0374   Charge_Vs_PathTEC2 = tmp->getTH2F();
0375 
0376   tmp = dqmStore_->book1D("Charge_TIB", "Charge_TIB", 1000, 0, 2000);
0377   Charge_TIB = tmp->getTH1F();
0378   tmp = dqmStore_->book1D("Charge_TID", "Charge_TID", 1000, 0, 2000);
0379   Charge_TID = tmp->getTH1F();
0380   tmp = dqmStore_->book1D("Charge_TID+", "Charge_TID+", 1000, 0, 2000);
0381   Charge_TIDP = tmp->getTH1F();
0382   tmp = dqmStore_->book1D("Charge_TID-", "Charge_TID-", 1000, 0, 2000);
0383   Charge_TIDM = tmp->getTH1F();
0384   tmp = dqmStore_->book1D("Charge_TOB", "Charge_TOB", 1000, 0, 2000);
0385   Charge_TOB = tmp->getTH1F();
0386   tmp = dqmStore_->book1D("Charge_TEC", "Charge_TEC", 1000, 0, 2000);
0387   Charge_TEC = tmp->getTH1F();
0388   tmp = dqmStore_->book1D("Charge_TEC1", "Charge_TEC1", 1000, 0, 2000);
0389   Charge_TEC1 = tmp->getTH1F();
0390   tmp = dqmStore_->book1D("Charge_TEC2", "Charge_TEC2", 1000, 0, 2000);
0391   Charge_TEC2 = tmp->getTH1F();
0392   tmp = dqmStore_->book1D("Charge_TEC+", "Charge_TEC+", 1000, 0, 2000);
0393   Charge_TECP = tmp->getTH1F();
0394   tmp = dqmStore_->book1D("Charge_TEC-", "Charge_TEC-", 1000, 0, 2000);
0395   Charge_TECM = tmp->getTH1F();
0396 
0397   /*
0398    tmp  = dqmStore_->book2D ("Charge_Vs_PathLength_CS1", "Charge_Vs_PathLength_CS1"  , 250,0.2,1.4, 500,0,2000); Charge_Vs_PathLength_CS1 = tmp->getTH2F();
0399    tmp  = dqmStore_->book2D ("Charge_Vs_PathLength_CS2", "Charge_Vs_PathLength_CS2"  , 250,0.2,1.4, 500,0,2000); Charge_Vs_PathLength_CS2 = tmp->getTH2F();
0400    tmp  = dqmStore_->book2D ("Charge_Vs_PathLength_CS3", "Charge_Vs_PathLength_CS3"  , 250,0.2,1.4, 500,0,2000); Charge_Vs_PathLength_CS3 = tmp->getTH2F();
0401    tmp  = dqmStore_->book2D ("Charge_Vs_PathLength_CS4", "Charge_Vs_PathLength_CS4"  , 250,0.2,1.4, 500,0,2000); Charge_Vs_PathLength_CS4 = tmp->getTH2F();
0402    tmp  = dqmStore_->book2D ("Charge_Vs_PathLength_CS5", "Charge_Vs_PathLength_CS5"  , 250,0.2,1.4, 500,0,2000); Charge_Vs_PathLength_CS5 = tmp->getTH2F();
0403 */
0404   tmp = dqmStore_->book2D("Charge_Vs_PathLength", "Charge_Vs_PathLength", 100, 0.2, 1.4, 500, 0, 2000);
0405   Charge_Vs_PathLength = tmp->getTH2F();
0406   tmp = dqmStore_->book2D("Charge_Vs_PathLength320", "Charge_Vs_PathLength", 100, 0.2, 1.4, 500, 0, 2000);
0407   Charge_Vs_PathLength320 = tmp->getTH2F();
0408   tmp = dqmStore_->book2D("Charge_Vs_PathLength500", "Charge_Vs_PathLength", 100, 0.2, 1.4, 500, 0, 2000);
0409   Charge_Vs_PathLength500 = tmp->getTH2F();
0410 
0411   tmp = dqmStore_->book2D("Charge_Vs_TransversAngle", "Charge_Vs_TransversAngle", 220, -20, 200, 500, 0, 2000);
0412   Charge_Vs_TransversAngle = tmp->getTH2F();
0413   tmp = dqmStore_->book2D("Charge_Vs_Alpha", "Charge_Vs_Alpha", 220, -20, 200, 500, 0, 2000);
0414   Charge_Vs_Alpha = tmp->getTH2F();
0415   tmp = dqmStore_->book2D("Charge_Vs_Beta", "Charge_Vs_Beta", 220, -20, 200, 500, 0, 2000);
0416   Charge_Vs_Beta = tmp->getTH2F();
0417 
0418   tmp = dqmStore_->book2D("NStrips_Vs_TransversAngle", "NStrips_Vs_TransversAngle", 220, -20, 200, 10, 0, 10);
0419   NStrips_Vs_TransversAngle = tmp->getTH2F();
0420   tmp = dqmStore_->book2D("NStrips_Vs_Alpha", "NStrips_Vs_Alpha", 220, -20, 200, 10, 0, 10);
0421   NStrips_Vs_Alpha = tmp->getTH2F();
0422   tmp = dqmStore_->book2D("NStrips_Vs_Beta", "NStrips_Vs_Beta", 220, -20, 200, 10, 0, 10);
0423   NStrips_Vs_Beta = tmp->getTH2F();
0424   tmp = dqmStore_->book1D("NHighStripInCluster", "NHighStripInCluster", 15, 0, 14);
0425   NHighStripInCluster = tmp->getTH1F();
0426   tmp = dqmStore_->book1D("NSatStripInCluster", "NSatStripInCluster", 50, 0, 50);
0427   NSatStripInCluster = tmp->getTH1F();
0428 
0429   tmp = dqmStore_->book1D("TrackChi2OverNDF", "TrackChi2OverNDF", 500, 0, 10);
0430   HTrackChi2OverNDF = tmp->getTH1F();
0431   tmp = dqmStore_->book1D("TrackHits", "TrackHits", 40, 0, 40);
0432   HTrackHits = tmp->getTH1F();
0433 
0434   tmp = dqmStore_->book1D("FirstStrip", "FirstStrip", 800, 0, 800);
0435   HFirstStrip = tmp->getTH1F();
0436 
0437   if (strcmp(AlgoMode.c_str(), "MultiJob") != 0) {
0438     tmp = dqmStore_->book2D("MPV_Vs_EtaTIB", "MPV_Vs_EtaTIB", 50, -3.0, 3.0, 300, 0, 600);
0439     MPV_Vs_EtaTIB = tmp->getTH2F();
0440     tmp = dqmStore_->book2D("MPV_Vs_EtaTID", "MPV_Vs_EtaTID", 50, -3.0, 3.0, 300, 0, 600);
0441     MPV_Vs_EtaTID = tmp->getTH2F();
0442     tmp = dqmStore_->book2D("MPV_Vs_EtaTOB", "MPV_Vs_EtaTOB", 50, -3.0, 3.0, 300, 0, 600);
0443     MPV_Vs_EtaTOB = tmp->getTH2F();
0444     tmp = dqmStore_->book2D("MPV_Vs_EtaTEC", "MPV_Vs_EtaTEC", 50, -3.0, 3.0, 300, 0, 600);
0445     MPV_Vs_EtaTEC = tmp->getTH2F();
0446     tmp = dqmStore_->book2D("MPV_Vs_EtaTEC1", "MPV_Vs_EtaTEC1", 50, -3.0, 3.0, 300, 0, 600);
0447     MPV_Vs_EtaTEC1 = tmp->getTH2F();
0448     tmp = dqmStore_->book2D("MPV_Vs_EtaTEC2", "MPV_Vs_EtaTEC2", 50, -3.0, 3.0, 300, 0, 600);
0449     MPV_Vs_EtaTEC2 = tmp->getTH2F();
0450 
0451     tmp = dqmStore_->book2D("MPV_Vs_PhiTIB", "MPV_Vs_PhiTIB", 50, -3.2, 3.2, 300, 0, 600);
0452     MPV_Vs_PhiTIB = tmp->getTH2F();
0453     tmp = dqmStore_->book2D("MPV_Vs_PhiTID", "MPV_Vs_PhiTID", 50, -3.2, 3.2, 300, 0, 600);
0454     MPV_Vs_PhiTID = tmp->getTH2F();
0455     tmp = dqmStore_->book2D("MPV_Vs_PhiTOB", "MPV_Vs_PhiTOB", 50, -3.2, 3.2, 300, 0, 600);
0456     MPV_Vs_PhiTOB = tmp->getTH2F();
0457     tmp = dqmStore_->book2D("MPV_Vs_PhiTEC", "MPV_Vs_PhiTEC", 50, -3.2, 3.2, 300, 0, 600);
0458     MPV_Vs_PhiTEC = tmp->getTH2F();
0459     tmp = dqmStore_->book2D("MPV_Vs_PhiTEC1", "MPV_Vs_PhiTEC1", 50, -3.2, 3.2, 300, 0, 600);
0460     MPV_Vs_PhiTEC1 = tmp->getTH2F();
0461     tmp = dqmStore_->book2D("MPV_Vs_PhiTEC2", "MPV_Vs_PhiTEC2", 50, -3.2, 3.2, 300, 0, 600);
0462     MPV_Vs_PhiTEC2 = tmp->getTH2F();
0463 
0464     tmp = dqmStore_->book1D("MPV_Vs_PathTIB", "MPV_Vs_PathTIB", 100, 0.2, 1.4);
0465     MPV_Vs_PathTIB = tmp->getTH1F();
0466     tmp = dqmStore_->book1D("MPV_Vs_PathTID", "MPV_Vs_PathTID", 100, 0.2, 1.4);
0467     MPV_Vs_PathTID = tmp->getTH1F();
0468     tmp = dqmStore_->book1D("MPV_Vs_PathTOB", "MPV_Vs_PathTOB", 100, 0.2, 1.4);
0469     MPV_Vs_PathTOB = tmp->getTH1F();
0470     tmp = dqmStore_->book1D("MPV_Vs_PathTEC", "MPV_Vs_PathTEC", 100, 0.2, 1.4);
0471     MPV_Vs_PathTEC = tmp->getTH1F();
0472     tmp = dqmStore_->book1D("MPV_Vs_PathTEC1", "MPV_Vs_PathTEC1", 100, 0.2, 1.4);
0473     MPV_Vs_PathTEC1 = tmp->getTH1F();
0474     tmp = dqmStore_->book1D("MPV_Vs_PathTEC2", "MPV_Vs_PathTEC2", 100, 0.2, 1.4);
0475     MPV_Vs_PathTEC2 = tmp->getTH1F();
0476 
0477     tmp = dqmStore_->book2D("MPV_Vs_Phi", "MPV_Vs_Phi", 50, -3.2, 3.2, 300, 0, 600);
0478     MPV_Vs_Phi = tmp->getTH2F();
0479     tmp = dqmStore_->book2D("MPV_Vs_Eta", "MPV_Vs_Eta", 50, -3.0, 3.0, 300, 0, 600);
0480     MPV_Vs_Eta = tmp->getTH2F();
0481     tmp = dqmStore_->book2D("MPV_Vs_R", "MPV_Vs_R", 150, 0.0, 150.0, 300, 0, 600);
0482     MPV_Vs_R = tmp->getTH2F();
0483     /*   
0484       tmp  = dqmStore_->book1D ("MPV_Vs_PathLength_CS1"   , "MPV_Vs_PathLength_CS1" , 250, 0.2, 1.4); MPV_Vs_PathLength_CS1 = tmp->getTH1F();
0485       tmp  = dqmStore_->book1D ("MPV_Vs_PathLength_CS2"   , "MPV_Vs_PathLength_CS2" , 250, 0.2, 1.4); MPV_Vs_PathLength_CS2 = tmp->getTH1F();
0486       tmp  = dqmStore_->book1D ("MPV_Vs_PathLength_CS3"   , "MPV_Vs_PathLength_CS3" , 250, 0.2, 1.4); MPV_Vs_PathLength_CS3 = tmp->getTH1F();
0487       tmp  = dqmStore_->book1D ("MPV_Vs_PathLength_CS4"   , "MPV_Vs_PathLength_CS4" , 250, 0.2, 1.4); MPV_Vs_PathLength_CS4 = tmp->getTH1F();
0488       tmp  = dqmStore_->book1D ("MPV_Vs_PathLength_CS5"   , "MPV_Vs_PathLength_CS5" , 250, 0.2, 1.4); MPV_Vs_PathLength_CS5 = tmp->getTH1F();
0489 
0490       tmp  = dqmStore_->book1D ("FWHM_Vs_PathLength_CS1"  , "FWHM_Vs_PathLength_CS1", 250, 0.2, 1.4); FWHM_Vs_PathLength_CS1 = tmp->getTH1F();
0491       tmp  = dqmStore_->book1D ("FWHM_Vs_PathLength_CS2"  , "FWHM_Vs_PathLength_CS2", 250, 0.2, 1.4); FWHM_Vs_PathLength_CS2 = tmp->getTH1F();
0492       tmp  = dqmStore_->book1D ("FWHM_Vs_PathLength_CS3"  , "FWHM_Vs_PathLength_CS3", 250, 0.2, 1.4); FWHM_Vs_PathLength_CS3 = tmp->getTH1F();
0493       tmp  = dqmStore_->book1D ("FWHM_Vs_PathLength_CS4"  , "FWHM_Vs_PathLength_CS4", 250, 0.2, 1.4); FWHM_Vs_PathLength_CS4 = tmp->getTH1F();
0494       tmp  = dqmStore_->book1D ("FWHM_Vs_PathLength_CS5"  , "FWHM_Vs_PathLength_CS5", 250, 0.2, 1.4); FWHM_Vs_PathLength_CS5 = tmp->getTH1F();
0495 */
0496     tmp = dqmStore_->book1D("MPV_Vs_PathLength", "MPV_Vs_PathLength", 100, 0.2, 1.4);
0497     MPV_Vs_PathLength = tmp->getTH1F();
0498     tmp = dqmStore_->book1D("MPV_Vs_PathLength320", "MPV_Vs_PathLength", 100, 0.2, 1.4);
0499     MPV_Vs_PathLength320 = tmp->getTH1F();
0500     tmp = dqmStore_->book1D("MPV_Vs_PathLength500", "MPV_Vs_PathLength", 100, 0.2, 1.4);
0501     MPV_Vs_PathLength500 = tmp->getTH1F();
0502 
0503     tmp = dqmStore_->book1D("FWHM_Vs_PathLength", "FWHM_Vs_PathLength", 100, 0.2, 1.4);
0504     FWHM_Vs_PathLength = tmp->getTH1F();
0505     tmp = dqmStore_->book1D("FWHM_Vs_PathLength320", "FWHM_Vs_PathLength", 100, 0.2, 1.4);
0506     FWHM_Vs_PathLength320 = tmp->getTH1F();
0507     tmp = dqmStore_->book1D("FWHM_Vs_PathLength500", "FWHM_Vs_PathLength", 100, 0.2, 1.4);
0508     FWHM_Vs_PathLength500 = tmp->getTH1F();
0509 
0510     tmp = dqmStore_->book1D("MPV_Vs_TransversAngle", "MPV_Vs_TransversAngle", 220, -20, 200);
0511     MPV_Vs_TransversAngle = tmp->getTH1F();
0512     tmp = dqmStore_->book1D("MPV_Vs_Alpha", "MPV_Vs_Alpha", 220, -20, 200);
0513     MPV_Vs_Alpha = tmp->getTH1F();
0514     tmp = dqmStore_->book1D("MPV_Vs_Beta", "MPV_Vs_Beta", 220, -20, 200);
0515     MPV_Vs_Beta = tmp->getTH1F();
0516 
0517     tmp = dqmStore_->book2D("Error_Vs_MPV", "Error_Vs_MPV", 600, 0, 600, 100, 0, 50);
0518     Error_Vs_MPV = tmp->getTH2F();
0519     tmp = dqmStore_->book2D("Error_Vs_Entries", "Error_Vs_Entries", 500, 0, 10000, 100, 0, 50);
0520     Error_Vs_Entries = tmp->getTH2F();
0521     tmp = dqmStore_->book2D("Error_Vs_Eta", "Error_Vs_Eta", 50, -3.0, 3.0, 100, 0, 50);
0522     Error_Vs_Eta = tmp->getTH2F();
0523     tmp = dqmStore_->book2D("Error_Vs_Phi", "Error_Vs_Phi", 50, -3.2, 3.2, 100, 0, 50);
0524     Error_Vs_Phi = tmp->getTH2F();
0525 
0526     tmp = dqmStore_->book2D("NoMPV_Vs_EtaPhi", "NoMPV_Vs_EtaPhi", 50, -3.0, 3.0, 50, -3.2, 3.2);
0527     NoMPV_Vs_EtaPhi = tmp->getTH2F();
0528 
0529     tmp = dqmStore_->book1D("NumberOfEntriesByAPV", "NumberOfEntriesByAPV", 1000, 0, 10000);
0530     NumberOfEntriesByAPV = tmp->getTH1F();
0531     tmp = dqmStore_->book1D("Chi2OverNDF", "Chi2OverNDF", 500, 0, 25);
0532     HChi2OverNDF = tmp->getTH1F();
0533 
0534     tmp = dqmStore_->book1D("MPVs", "MPVs", 600, 0, 600);
0535     MPVs = tmp->getTH1F();
0536     tmp = dqmStore_->book1D("MPVs320", "MPVs320", 600, 0, 600);
0537     MPVs320 = tmp->getTH1F();
0538     tmp = dqmStore_->book1D("MPVs500", "MPVs500", 600, 0, 600);
0539     MPVs500 = tmp->getTH1F();
0540 
0541     //      MPV_vs_10RplusEta          tmp  = dqmStore_->book2D ("MPV_vs_10RplusEta","MPV_vs_10RplusEta", 48000,0,2400, 800,100,500);
0542   }
0543 
0544   gROOT->cd();
0545 
0546   auto const& Det = iSetup.getData(tkGeomToken_).dets();
0547 
0548   //   if(strcmp(AlgoMode.c_str(),"MultiJob")!=0 && !FirstSetOfConstants){
0549   if (!iSetup.getHandle(gainToken_)) {
0550     printf("\n#####################\n\nERROR --> gainHandle is not valid\n\n#####################\n\n");
0551     exit(0);
0552   }
0553   //   }
0554 
0555   unsigned int Id = 0;
0556   for (unsigned int i = 0; i < Det.size(); i++) {
0557     DetId Detid = Det[i]->geographicalId();
0558     int SubDet = Detid.subdetId();
0559 
0560     if (SubDet == StripSubdetector::TIB || SubDet == StripSubdetector::TID || SubDet == StripSubdetector::TOB ||
0561         SubDet == StripSubdetector::TEC) {
0562       auto DetUnit = dynamic_cast<StripGeomDetUnit const*>(Det[i]);
0563       if (!DetUnit)
0564         continue;
0565 
0566       const StripTopology& Topo = DetUnit->specificTopology();
0567       unsigned int NAPV = Topo.nstrips() / 128;
0568 
0569       double Phi = DetUnit->position().basicVector().phi();
0570       double Eta = DetUnit->position().basicVector().eta();
0571       double R = DetUnit->position().basicVector().transverse();
0572       double Thick = DetUnit->surface().bounds().thickness();
0573 
0574       for (unsigned int j = 0; j < NAPV; j++) {
0575         stAPVGain* APV = new stAPVGain;
0576         APV->Index = Id;
0577         APV->DetId = Detid.rawId();
0578         APV->APVId = j;
0579         APV->SubDet = SubDet;
0580         APV->FitMPV = -1;
0581         APV->Gain = -1;
0582         APV->PreviousGain = 1;
0583         APV->Eta = Eta;
0584         APV->Phi = Phi;
0585         APV->R = R;
0586         APV->Thickness = Thick;
0587         APV->Side = 0;
0588 
0589         if (SubDet == StripSubdetector::TID) {
0590           APV->Side = tTopo->tidSide(Detid);
0591         } else if (SubDet == StripSubdetector::TEC) {
0592           APV->Side = tTopo->tecSide(Detid);
0593         }
0594 
0595         APVsCollOrdered.push_back(APV);
0596         APVsColl[(APV->DetId << 3) | APV->APVId] = APV;
0597         Id++;
0598 
0599         APV_DetId->Fill(Id, APV->DetId);
0600         APV_Id->Fill(Id, APV->APVId);
0601         APV_Eta->Fill(Id, APV->Eta);
0602         APV_R->Fill(Id, APV->R);
0603         APV_SubDet->Fill(Id, APV->SubDet);
0604         APV_Thickness->Fill(Id, APV->Thickness);
0605       }
0606     }
0607   }
0608 
0609   NEvent = 0;
0610   SRun = 0;
0611   SEvent = 0;
0612   STimestamp = 0;
0613   ERun = 0;
0614   EEvent = 0;
0615   ETimestamp = 0;
0616 }
0617 
0618 void SiStripGainFromData::algoEndJob() {
0619   unsigned int I = 0;
0620 
0621   if (strcmp(AlgoMode.c_str(), "WriteOnDB") == 0 || strcmp(AlgoMode.c_str(), "Merge") == 0) {
0622     TH1::AddDirectory(kTRUE);
0623 
0624     TFile* file = nullptr;
0625     for (unsigned int f = 0; f < VInputFiles.size(); f++) {
0626       printf("Loading New Input File : %s\n", VInputFiles[f].c_str());
0627       if (CheckIfFileExist) {
0628         FILE* doesFileExist = fopen(VInputFiles[f].c_str(), "r");
0629         if (!doesFileExist) {
0630           printf("File %s doesn't exist\n", VInputFiles[f].c_str());
0631           continue;
0632         } else {
0633           fclose(doesFileExist);
0634         }
0635       }
0636       file = new TFile(VInputFiles[f].c_str());
0637       if (!file || file->IsZombie()) {
0638         printf("### Bug With File %s\n### File will be skipped \n", VInputFiles[f].c_str());
0639         continue;
0640       }
0641       APV_Charge->Add((TH1*)file->FindObjectAny("APV_Charge"), 1);
0642       APV_Momentum->Add((TH1*)file->FindObjectAny("APV_Momentum"), 1);
0643       APV_PathLength->Add((TH1*)file->FindObjectAny("APV_PathLength"), 1);
0644 
0645       Tracks_P_Vs_Eta->Add((TH1*)file->FindObjectAny("Tracks_P_Vs_Eta"), 1);
0646       Tracks_Pt_Vs_Eta->Add((TH1*)file->FindObjectAny("Tracks_Pt_Vs_Eta"), 1);
0647 
0648       Charge_Vs_PathTIB->Add((TH1*)file->FindObjectAny("Charge_Vs_PathTIB"), 1);
0649       Charge_Vs_PathTID->Add((TH1*)file->FindObjectAny("Charge_Vs_PathTID"), 1);
0650       Charge_Vs_PathTOB->Add((TH1*)file->FindObjectAny("Charge_Vs_PathTOB"), 1);
0651       Charge_Vs_PathTEC->Add((TH1*)file->FindObjectAny("Charge_Vs_PathTEC"), 1);
0652       Charge_Vs_PathTEC1->Add((TH1*)file->FindObjectAny("Charge_Vs_PathTEC1"), 1);
0653       Charge_Vs_PathTEC2->Add((TH1*)file->FindObjectAny("Charge_Vs_PathTEC2"), 1);
0654 
0655       HTrackChi2OverNDF->Add((TH1*)file->FindObjectAny("TrackChi2OverNDF"), 1);
0656       HTrackHits->Add((TH1*)file->FindObjectAny("TrackHits"), 1);
0657 
0658       NHighStripInCluster->Add((TH1*)file->FindObjectAny("NHighStripInCluster"), 1);
0659       NSatStripInCluster->Add((TH1*)file->FindObjectAny("NSatStripInCluster"), 1);
0660       Charge_Vs_PathLength->Add((TH1*)file->FindObjectAny("Charge_Vs_PathLength"), 1);
0661       Charge_Vs_PathLength320->Add((TH1*)file->FindObjectAny("Charge_Vs_PathLength320"), 1);
0662       Charge_Vs_PathLength500->Add((TH1*)file->FindObjectAny("Charge_Vs_PathLength500"), 1);
0663       Charge_Vs_TransversAngle->Add((TH1*)file->FindObjectAny("Charge_Vs_TransversAngle"), 1);
0664       NStrips_Vs_TransversAngle->Add((TH1*)file->FindObjectAny("NStrips_Vs_TransversAngle"), 1);
0665       Charge_Vs_Alpha->Add((TH1*)file->FindObjectAny("Charge_Vs_Alpha"), 1);
0666       NStrips_Vs_Alpha->Add((TH1*)file->FindObjectAny("NStrips_Vs_Alpha"), 1);
0667       HFirstStrip->Add((TH1*)file->FindObjectAny("FirstStrip"), 1);
0668 
0669       TH1F* JobInfo_tmp = (TH1F*)file->FindObjectAny("JobInfo");
0670       NEvent += (unsigned int)JobInfo_tmp->GetBinContent(JobInfo_tmp->GetXaxis()->FindBin(1));
0671       unsigned int tmp_SRun = (unsigned int)JobInfo_tmp->GetBinContent(JobInfo_tmp->GetXaxis()->FindBin(3));
0672       unsigned int tmp_SEvent = (unsigned int)JobInfo_tmp->GetBinContent(JobInfo_tmp->GetXaxis()->FindBin(4));
0673       unsigned int tmp_ERun = (unsigned int)JobInfo_tmp->GetBinContent(JobInfo_tmp->GetXaxis()->FindBin(6));
0674       unsigned int tmp_EEvent = (unsigned int)JobInfo_tmp->GetBinContent(JobInfo_tmp->GetXaxis()->FindBin(7));
0675 
0676       if (SRun == 0)
0677         SRun = tmp_SRun;
0678 
0679       if (tmp_SRun < SRun) {
0680         SRun = tmp_SRun;
0681         SEvent = tmp_SEvent;
0682       } else if (tmp_SRun == SRun && tmp_SEvent < SEvent) {
0683         SEvent = tmp_SEvent;
0684       }
0685 
0686       if (tmp_ERun > ERun) {
0687         ERun = tmp_ERun;
0688         EEvent = tmp_EEvent;
0689       } else if (tmp_ERun == ERun && tmp_EEvent > EEvent) {
0690         EEvent = tmp_EEvent;
0691       }
0692 
0693       printf("Deleting Current Input File\n");
0694       file->Close();
0695       delete file;
0696     }
0697   }
0698 
0699   JobInfo->Fill(1, NEvent);
0700   JobInfo->Fill(3, SRun);
0701   JobInfo->Fill(4, SEvent);
0702   JobInfo->Fill(6, ERun);
0703   JobInfo->Fill(7, EEvent);
0704 
0705   if (strcmp(AlgoMode.c_str(), "MultiJob") != 0) {
0706     TH1D* Proj = nullptr;
0707     double FitResults[5];
0708     I = 0;
0709     for (auto it = APVsColl.begin(); it != APVsColl.end(); it++) {
0710       if (I % 3650 == 0)
0711         printf("Fitting Histograms \t %6.2f%%\n", (100.0 * I) / APVsColl.size());
0712       I++;
0713       stAPVGain* APV = it->second;
0714 
0715       int bin = APV_Charge->GetXaxis()->FindBin(APV->Index);
0716       Proj = APV_Charge->ProjectionY(" ", bin, bin, "e");
0717       Proj = (TH1D*)Proj->Clone();
0718       if (Proj == nullptr)
0719         continue;
0720 
0721       // ADD PROJECTTIONS COMMING FROM THE SECOND APV IN THE PAIR
0722       if (CalibrationLevel == 1) {
0723         int SecondAPVId = APV->APVId;
0724         if (SecondAPVId % 2 == 0) {
0725           SecondAPVId = SecondAPVId + 1;
0726         } else {
0727           SecondAPVId = SecondAPVId - 1;
0728         }
0729         stAPVGain* APV2 = APVsColl[(APV->DetId << 3) | SecondAPVId];
0730 
0731         int bin2 = APV_Charge->GetXaxis()->FindBin(APV2->Index);
0732         TH1D* Proj2 = APV_Charge->ProjectionY(" ", bin2, bin2, "e");
0733         if (Proj2 != nullptr) {
0734           Proj->Add(Proj2, 1);
0735         }
0736       } else if (CalibrationLevel > 1) {
0737         //       printf("%8i %i--> %4.0f + %4.0f\n",APV->DetId, APV->APVId, 0.0, Proj->GetEntries());
0738         for (auto it2 = APVsColl.begin(); it2 != APVsColl.end(); it2++) {
0739           stAPVGain* APV2 = it2->second;
0740 
0741           if (APV2->DetId != APV->DetId)
0742             continue;
0743           if (APV2->APVId == APV->APVId)
0744             continue;
0745 
0746           int bin2 = APV_Charge->GetXaxis()->FindBin(APV2->Index);
0747           TH1D* Proj2 = APV_Charge->ProjectionY(" ", bin2, bin2, "e");
0748           if (Proj2 != nullptr) {
0749             //                   printf("%8i %i--> %4.0f + %4.0f\n",APV2->DetId, APV2->APVId, Proj->GetEntries(), Proj2->GetEntries());
0750             Proj->Add(Proj2, 1);
0751           }
0752         }
0753         //             printf("%8i %i--> %4.0f Full\n",APV->DetId, APV->APVId, Proj->GetEntries());
0754       }
0755 
0756       //std::cout << "Proj->GetEntries(): " << Proj->GetEntries() << ", Proj->GetMean(): " << Proj->GetMean() << std::endl;
0757 
0758       getPeakOfLandau(Proj, FitResults);
0759       APV->FitMPV = FitResults[0];
0760       //         printf("MPV = %f - %f\n",FitResults[0], FitResults[1]);
0761       if (FitResults[0] != -0.5 && FitResults[1] < MaxMPVError) {
0762         APV_MPV->Fill(APV->Index, APV->FitMPV);
0763         MPVs->Fill(APV->FitMPV);
0764         if (APV->Thickness < 0.04)
0765           MPVs320->Fill(APV->FitMPV);
0766         if (APV->Thickness > 0.04)
0767           MPVs500->Fill(APV->FitMPV);
0768 
0769         MPV_Vs_R->Fill(APV->R, APV->FitMPV);
0770         MPV_Vs_Eta->Fill(APV->Eta, APV->FitMPV);
0771         if (APV->SubDet == StripSubdetector::TIB)
0772           MPV_Vs_EtaTIB->Fill(APV->Eta, APV->FitMPV);
0773         if (APV->SubDet == StripSubdetector::TID)
0774           MPV_Vs_EtaTID->Fill(APV->Eta, APV->FitMPV);
0775         if (APV->SubDet == StripSubdetector::TOB)
0776           MPV_Vs_EtaTOB->Fill(APV->Eta, APV->FitMPV);
0777         if (APV->SubDet == StripSubdetector::TEC) {
0778           MPV_Vs_EtaTEC->Fill(APV->Eta, APV->FitMPV);
0779           if (APV->Thickness < 0.04)
0780             MPV_Vs_EtaTEC1->Fill(APV->Eta, APV->FitMPV);
0781           if (APV->Thickness > 0.04)
0782             MPV_Vs_EtaTEC2->Fill(APV->Eta, APV->FitMPV);
0783         }
0784         MPV_Vs_Phi->Fill(APV->Phi, APV->FitMPV);
0785         if (APV->SubDet == StripSubdetector::TIB)
0786           MPV_Vs_PhiTIB->Fill(APV->Phi, APV->FitMPV);
0787         if (APV->SubDet == StripSubdetector::TID)
0788           MPV_Vs_PhiTID->Fill(APV->Phi, APV->FitMPV);
0789         if (APV->SubDet == StripSubdetector::TOB)
0790           MPV_Vs_PhiTOB->Fill(APV->Phi, APV->FitMPV);
0791         if (APV->SubDet == StripSubdetector::TEC) {
0792           MPV_Vs_PhiTEC->Fill(APV->Phi, APV->FitMPV);
0793           if (APV->Thickness < 0.04)
0794             MPV_Vs_PhiTEC1->Fill(APV->Phi, APV->FitMPV);
0795           if (APV->Thickness > 0.04)
0796             MPV_Vs_PhiTEC2->Fill(APV->Phi, APV->FitMPV);
0797         }
0798 
0799         if (APV->SubDet == StripSubdetector::TIB)
0800           Charge_TIB->Add(Proj, 1);
0801         if (APV->SubDet == StripSubdetector::TID) {
0802           Charge_TID->Add(Proj, 1);
0803           if (APV->Side == 1)
0804             Charge_TIDM->Add(Proj, 1);
0805           if (APV->Side == 2)
0806             Charge_TIDP->Add(Proj, 1);
0807         }
0808         if (APV->SubDet == StripSubdetector::TOB)
0809           Charge_TOB->Add(Proj, 1);
0810         if (APV->SubDet == StripSubdetector::TEC) {
0811           Charge_TEC->Add(Proj, 1);
0812           if (APV->Thickness < 0.04)
0813             Charge_TEC1->Add(Proj, 1);
0814           if (APV->Thickness > 0.04)
0815             Charge_TEC2->Add(Proj, 1);
0816           if (APV->Side == 1)
0817             Charge_TECM->Add(Proj, 1);
0818           if (APV->Side == 2)
0819             Charge_TECP->Add(Proj, 1);
0820         }
0821       }
0822 
0823       if (APV->SubDet == StripSubdetector::TIB)
0824         Charge_TIB->Add(Proj, 1);
0825       if (APV->SubDet == StripSubdetector::TID) {
0826         Charge_TID->Add(Proj, 1);
0827         if (APV->Side == 1)
0828           Charge_TIDM->Add(Proj, 1);
0829         if (APV->Side == 2)
0830           Charge_TIDP->Add(Proj, 1);
0831       }
0832       if (APV->SubDet == StripSubdetector::TOB)
0833         Charge_TOB->Add(Proj, 1);
0834       if (APV->SubDet == StripSubdetector::TEC) {
0835         Charge_TEC->Add(Proj, 1);
0836         if (APV->Thickness < 0.04)
0837           Charge_TEC1->Add(Proj, 1);
0838         if (APV->Thickness > 0.04)
0839           Charge_TEC2->Add(Proj, 1);
0840         if (APV->Side == 1)
0841           Charge_TECM->Add(Proj, 1);
0842         if (APV->Side == 2)
0843           Charge_TECP->Add(Proj, 1);
0844       }
0845 
0846       if (FitResults[0] != -0.5) {
0847         HChi2OverNDF->Fill(FitResults[4]);
0848         Error_Vs_MPV->Fill(FitResults[0], FitResults[1]);
0849         Error_Vs_Entries->Fill(Proj->GetEntries(), FitResults[1]);
0850         Error_Vs_Eta->Fill(APV->Eta, FitResults[1]);
0851         Error_Vs_Phi->Fill(APV->Phi, FitResults[1]);
0852       }
0853       NumberOfEntriesByAPV->Fill(Proj->GetEntries());
0854       delete Proj;
0855 
0856       Proj = APV_PathLength->ProjectionY(" ", bin, bin, "e");
0857       if (Proj == nullptr)
0858         continue;
0859 
0860       APV_PathLengthM->SetBinContent(APV->Index, Proj->GetMean(1));
0861       APV_PathLengthM->SetBinError(APV->Index, Proj->GetMeanError(1));
0862       //         delete Proj;
0863     }
0864 
0865     unsigned int GOOD = 0;
0866     unsigned int BAD = 0;
0867     double MPVmean = 300.;  //MPVs->GetMean();
0868     for (auto it = APVsColl.begin(); it != APVsColl.end(); it++) {
0869       stAPVGain* APV = it->second;
0870       if (APV->FitMPV > 0) {
0871         APV->Gain = APV->FitMPV / MPVmean;  // APV->FitMPV;
0872         GOOD++;
0873       } else {
0874         NoMPV_Vs_EtaPhi->Fill(APV->Eta, APV->Phi);
0875         APV->Gain = 1;
0876         BAD++;
0877       }
0878       if (APV->Gain <= 0)
0879         APV->Gain = 1;
0880       APV_Gain->Fill(APV->Index, APV->Gain);
0881 
0882       if (!FirstSetOfConstants)
0883         APV->Gain *= APV->PreviousGain;
0884       APV_CumulGain->Fill(APV->Index, APV->Gain);
0885     }
0886 
0887     for (int j = 0; j < Charge_Vs_PathLength->GetXaxis()->GetNbins(); j++) {
0888       Proj = Charge_Vs_PathLength->ProjectionY(" ", j, j, "e");
0889       getPeakOfLandau(Proj, FitResults);
0890       if (FitResults[0] == -0.5)
0891         continue;
0892       MPV_Vs_PathLength->SetBinContent(j, FitResults[0] / Charge_Vs_PathLength->GetXaxis()->GetBinCenter(j));
0893       MPV_Vs_PathLength->SetBinError(j, FitResults[1] / Charge_Vs_PathLength->GetXaxis()->GetBinCenter(j));
0894       FWHM_Vs_PathLength->SetBinContent(
0895           j, FitResults[2] / (FitResults[0] / Charge_Vs_PathLength->GetXaxis()->GetBinCenter(j)));
0896       FWHM_Vs_PathLength->SetBinError(
0897           j, FitResults[3] / (FitResults[0] / Charge_Vs_PathLength->GetXaxis()->GetBinCenter(j)));
0898       delete Proj;
0899     }
0900 
0901     for (int j = 0; j < Charge_Vs_PathLength320->GetXaxis()->GetNbins(); j++) {
0902       Proj = Charge_Vs_PathLength320->ProjectionY(" ", j, j, "e");
0903       getPeakOfLandau(Proj, FitResults);
0904       if (FitResults[0] == -0.5)
0905         continue;
0906       MPV_Vs_PathLength320->SetBinContent(j, FitResults[0] / Charge_Vs_PathLength320->GetXaxis()->GetBinCenter(j));
0907       MPV_Vs_PathLength320->SetBinError(j, FitResults[1] / Charge_Vs_PathLength320->GetXaxis()->GetBinCenter(j));
0908       FWHM_Vs_PathLength320->SetBinContent(
0909           j, FitResults[2] / (FitResults[0] / Charge_Vs_PathLength320->GetXaxis()->GetBinCenter(j)));
0910       FWHM_Vs_PathLength320->SetBinError(
0911           j, FitResults[3] / (FitResults[0] / Charge_Vs_PathLength320->GetXaxis()->GetBinCenter(j)));
0912       delete Proj;
0913     }
0914 
0915     for (int j = 0; j < Charge_Vs_PathLength500->GetXaxis()->GetNbins(); j++) {
0916       Proj = Charge_Vs_PathLength500->ProjectionY(" ", j, j, "e");
0917       getPeakOfLandau(Proj, FitResults);
0918       if (FitResults[0] == -0.5)
0919         continue;
0920       MPV_Vs_PathLength500->SetBinContent(j, FitResults[0] / Charge_Vs_PathLength500->GetXaxis()->GetBinCenter(j));
0921       MPV_Vs_PathLength500->SetBinError(j, FitResults[1] / Charge_Vs_PathLength500->GetXaxis()->GetBinCenter(j));
0922       FWHM_Vs_PathLength500->SetBinContent(
0923           j, FitResults[2] / (FitResults[0] / Charge_Vs_PathLength500->GetXaxis()->GetBinCenter(j)));
0924       FWHM_Vs_PathLength500->SetBinError(
0925           j, FitResults[3] / (FitResults[0] / Charge_Vs_PathLength500->GetXaxis()->GetBinCenter(j)));
0926       delete Proj;
0927     }
0928     /*
0929       for(int j=0;j<Charge_Vs_PathLength_CS1->GetXaxis()->GetNbins();j++){
0930          Proj      = Charge_Vs_PathLength_CS1->ProjectionY(" ",j,j,"e");
0931          getPeakOfLandau(Proj,FitResults); if(FitResults[0] ==-0.5)continue;
0932          MPV_Vs_PathLength_CS1->SetBinContent (j, FitResults[0]/Charge_Vs_PathLength_CS1->GetXaxis()->GetBinCenter(j));
0933          MPV_Vs_PathLength_CS1->SetBinError   (j, FitResults[1]/Charge_Vs_PathLength_CS1->GetXaxis()->GetBinCenter(j));
0934          FWHM_Vs_PathLength_CS1->SetBinContent(j, FitResults[2]/(FitResults[0]/Charge_Vs_PathLength_CS1->GetXaxis()->GetBinCenter(j) ));
0935          FWHM_Vs_PathLength_CS1->SetBinError  (j, FitResults[3]/(FitResults[0]/Charge_Vs_PathLength_CS1->GetXaxis()->GetBinCenter(j) ));
0936          delete Proj;
0937       }
0938 
0939       for(int j=0;j<Charge_Vs_PathLength_CS2->GetXaxis()->GetNbins();j++){
0940          Proj      = Charge_Vs_PathLength_CS2->ProjectionY(" ",j,j,"e");
0941          getPeakOfLandau(Proj,FitResults); if(FitResults[0] ==-0.5)continue;
0942          MPV_Vs_PathLength_CS2->SetBinContent (j, FitResults[0]/Charge_Vs_PathLength_CS2->GetXaxis()->GetBinCenter(j));
0943          MPV_Vs_PathLength_CS2->SetBinError   (j, FitResults[1]/Charge_Vs_PathLength_CS2->GetXaxis()->GetBinCenter(j));
0944          FWHM_Vs_PathLength_CS2->SetBinContent(j, FitResults[2]/(FitResults[0]/Charge_Vs_PathLength_CS2->GetXaxis()->GetBinCenter(j) ));
0945          FWHM_Vs_PathLength_CS2->SetBinError  (j, FitResults[3]/(FitResults[0]/Charge_Vs_PathLength_CS2->GetXaxis()->GetBinCenter(j) ));
0946          delete Proj;
0947       }
0948 
0949       for(int j=0;j<Charge_Vs_PathLength_CS3->GetXaxis()->GetNbins();j++){
0950          Proj      = Charge_Vs_PathLength_CS3->ProjectionY(" ",j,j,"e");
0951          getPeakOfLandau(Proj,FitResults); if(FitResults[0] ==-0.5)continue;
0952          MPV_Vs_PathLength_CS3->SetBinContent (j, FitResults[0]/Charge_Vs_PathLength_CS3->GetXaxis()->GetBinCenter(j));
0953          MPV_Vs_PathLength_CS3->SetBinError   (j, FitResults[1]/Charge_Vs_PathLength_CS3->GetXaxis()->GetBinCenter(j));
0954          FWHM_Vs_PathLength_CS3->SetBinContent(j, FitResults[2]/(FitResults[0]/Charge_Vs_PathLength_CS3->GetXaxis()->GetBinCenter(j) ));
0955          FWHM_Vs_PathLength_CS3->SetBinError  (j, FitResults[3]/(FitResults[0]/Charge_Vs_PathLength_CS3->GetXaxis()->GetBinCenter(j) ));
0956          delete Proj;
0957       }
0958 
0959       for(int j=0;j<Charge_Vs_PathLength_CS4->GetXaxis()->GetNbins();j++){
0960          Proj      = Charge_Vs_PathLength_CS4->ProjectionY(" ",j,j,"e");
0961          getPeakOfLandau(Proj,FitResults); if(FitResults[0] ==-0.5)continue;
0962          MPV_Vs_PathLength_CS4->SetBinContent (j, FitResults[0]/Charge_Vs_PathLength_CS4->GetXaxis()->GetBinCenter(j));
0963          MPV_Vs_PathLength_CS4->SetBinError   (j, FitResults[1]/Charge_Vs_PathLength_CS4->GetXaxis()->GetBinCenter(j));
0964          FWHM_Vs_PathLength_CS4->SetBinContent(j, FitResults[2]/(FitResults[0]/Charge_Vs_PathLength_CS4->GetXaxis()->GetBinCenter(j) ));
0965          FWHM_Vs_PathLength_CS4->SetBinError  (j, FitResults[3]/(FitResults[0]/Charge_Vs_PathLength_CS4->GetXaxis()->GetBinCenter(j) ));
0966          delete Proj;
0967       }
0968 
0969       for(int j=0;j<Charge_Vs_PathLength_CS5->GetXaxis()->GetNbins();j++){
0970          Proj      = Charge_Vs_PathLength_CS5->ProjectionY(" ",j,j,"e");
0971          getPeakOfLandau(Proj,FitResults); if(FitResults[0] ==-0.5)continue;
0972          MPV_Vs_PathLength_CS5->SetBinContent (j, FitResults[0]/Charge_Vs_PathLength_CS5->GetXaxis()->GetBinCenter(j));
0973          MPV_Vs_PathLength_CS5->SetBinError   (j, FitResults[1]/Charge_Vs_PathLength_CS5->GetXaxis()->GetBinCenter(j));
0974          FWHM_Vs_PathLength_CS5->SetBinContent(j, FitResults[2]/(FitResults[0]/Charge_Vs_PathLength_CS5->GetXaxis()->GetBinCenter(j) ));
0975          FWHM_Vs_PathLength_CS5->SetBinError  (j, FitResults[3]/(FitResults[0]/Charge_Vs_PathLength_CS5->GetXaxis()->GetBinCenter(j) ));
0976          delete Proj;
0977       }
0978 */
0979 
0980     for (int j = 0; j < Charge_Vs_PathTIB->GetXaxis()->GetNbins(); j++) {
0981       Proj = Charge_Vs_PathTIB->ProjectionY(" ", j, j, "e");
0982       getPeakOfLandau(Proj, FitResults);
0983       if (FitResults[0] == -0.5)
0984         continue;
0985       MPV_Vs_PathTIB->SetBinContent(j, FitResults[0] / Charge_Vs_PathTIB->GetXaxis()->GetBinCenter(j));
0986       MPV_Vs_PathTIB->SetBinError(j, FitResults[1] / Charge_Vs_PathTIB->GetXaxis()->GetBinCenter(j));
0987       delete Proj;
0988     }
0989 
0990     for (int j = 0; j < Charge_Vs_PathTID->GetXaxis()->GetNbins(); j++) {
0991       Proj = Charge_Vs_PathTID->ProjectionY(" ", j, j, "e");
0992       getPeakOfLandau(Proj, FitResults);
0993       if (FitResults[0] == -0.5)
0994         continue;
0995       MPV_Vs_PathTID->SetBinContent(j, FitResults[0] / Charge_Vs_PathTID->GetXaxis()->GetBinCenter(j));
0996       MPV_Vs_PathTID->SetBinError(j, FitResults[1] / Charge_Vs_PathTID->GetXaxis()->GetBinCenter(j));
0997       delete Proj;
0998     }
0999 
1000     for (int j = 0; j < Charge_Vs_PathTOB->GetXaxis()->GetNbins(); j++) {
1001       Proj = Charge_Vs_PathTOB->ProjectionY(" ", j, j, "e");
1002       getPeakOfLandau(Proj, FitResults);
1003       if (FitResults[0] == -0.5)
1004         continue;
1005       MPV_Vs_PathTOB->SetBinContent(j, FitResults[0] / Charge_Vs_PathTOB->GetXaxis()->GetBinCenter(j));
1006       MPV_Vs_PathTOB->SetBinError(j, FitResults[1] / Charge_Vs_PathTOB->GetXaxis()->GetBinCenter(j));
1007       delete Proj;
1008     }
1009 
1010     for (int j = 0; j < Charge_Vs_PathTEC->GetXaxis()->GetNbins(); j++) {
1011       Proj = Charge_Vs_PathTEC->ProjectionY(" ", j, j, "e");
1012       getPeakOfLandau(Proj, FitResults);
1013       if (FitResults[0] == -0.5)
1014         continue;
1015       MPV_Vs_PathTEC->SetBinContent(j, FitResults[0] / Charge_Vs_PathTEC->GetXaxis()->GetBinCenter(j));
1016       MPV_Vs_PathTEC->SetBinError(j, FitResults[1] / Charge_Vs_PathTEC->GetXaxis()->GetBinCenter(j));
1017       delete Proj;
1018     }
1019 
1020     for (int j = 0; j < Charge_Vs_PathTEC1->GetXaxis()->GetNbins(); j++) {
1021       Proj = Charge_Vs_PathTEC1->ProjectionY(" ", j, j, "e");
1022       getPeakOfLandau(Proj, FitResults);
1023       if (FitResults[0] == -0.5)
1024         continue;
1025       MPV_Vs_PathTEC1->SetBinContent(j, FitResults[0] / Charge_Vs_PathTEC1->GetXaxis()->GetBinCenter(j));
1026       MPV_Vs_PathTEC1->SetBinError(j, FitResults[1] / Charge_Vs_PathTEC1->GetXaxis()->GetBinCenter(j));
1027       delete Proj;
1028     }
1029 
1030     for (int j = 0; j < Charge_Vs_PathTEC2->GetXaxis()->GetNbins(); j++) {
1031       Proj = Charge_Vs_PathTEC2->ProjectionY(" ", j, j, "e");
1032       getPeakOfLandau(Proj, FitResults);
1033       if (FitResults[0] == -0.5)
1034         continue;
1035       MPV_Vs_PathTEC2->SetBinContent(j, FitResults[0] / Charge_Vs_PathTEC2->GetXaxis()->GetBinCenter(j));
1036       MPV_Vs_PathTEC2->SetBinError(j, FitResults[1] / Charge_Vs_PathTEC2->GetXaxis()->GetBinCenter(j));
1037       delete Proj;
1038     }
1039 
1040     for (int j = 1; j < Charge_Vs_TransversAngle->GetXaxis()->GetNbins(); j++) {
1041       Proj = Charge_Vs_TransversAngle->ProjectionY(" ", j, j, "e");
1042       getPeakOfLandau(Proj, FitResults);
1043       if (FitResults[0] == -0.5)
1044         continue;
1045       MPV_Vs_TransversAngle->SetBinContent(j, FitResults[0]);
1046       MPV_Vs_TransversAngle->SetBinError(j, FitResults[1]);
1047       delete Proj;
1048     }
1049 
1050     for (int j = 1; j < Charge_Vs_Alpha->GetXaxis()->GetNbins(); j++) {
1051       Proj = Charge_Vs_Alpha->ProjectionY(" ", j, j, "e");
1052       getPeakOfLandau(Proj, FitResults);
1053       if (FitResults[0] == -0.5)
1054         continue;
1055       MPV_Vs_Alpha->SetBinContent(j, FitResults[0]);
1056       MPV_Vs_Alpha->SetBinError(j, FitResults[1]);
1057       delete Proj;
1058     }
1059 
1060     for (int j = 1; j < Charge_Vs_Beta->GetXaxis()->GetNbins(); j++) {
1061       Proj = Charge_Vs_Beta->ProjectionY(" ", j, j, "e");
1062       getPeakOfLandau(Proj, FitResults);
1063       if (FitResults[0] == -0.5)
1064         continue;
1065       MPV_Vs_Beta->SetBinContent(j, FitResults[0]);
1066       MPV_Vs_Beta->SetBinError(j, FitResults[1]);
1067       delete Proj;
1068     }
1069 
1070     FILE* Gains = fopen(OutputGains.c_str(), "w");
1071     fprintf(Gains, "NEvents = %i\n", NEvent);
1072     fprintf(Gains, "Number of APVs = %lu\n", static_cast<unsigned long>(APVsColl.size()));
1073     fprintf(Gains, "GoodFits = %i BadFits = %i ratio = %f\n", GOOD, BAD, (100.0 * GOOD) / (GOOD + BAD));
1074     for (std::vector<stAPVGain*>::iterator it = APVsCollOrdered.begin(); it != APVsCollOrdered.end(); it++) {
1075       stAPVGain* APV = *it;
1076       fprintf(Gains,
1077               "%i | %i | PreviousGain = %7.5f NewGain = %7.5f\n",
1078               APV->DetId,
1079               APV->APVId,
1080               APV->PreviousGain,
1081               APV->Gain);
1082     }
1083 
1084     std::vector<unsigned int> DetIdOfBuggedAPV;
1085     fprintf(Gains, "----------------------------------------------------------------------\n");
1086     for (std::vector<stAPVGain*>::iterator it = APVsCollOrdered.begin(); it != APVsCollOrdered.end(); it++) {
1087       stAPVGain* APV = *it;
1088       if (APV->FitMPV > 0 && APV->FitMPV < 200) {
1089         bool tmpBug = false;
1090         for (unsigned int b = 0; b < DetIdOfBuggedAPV.size() && !tmpBug; b++) {
1091           if (DetIdOfBuggedAPV[b] == APV->DetId)
1092             tmpBug = true;
1093         }
1094         if (!tmpBug) {
1095           fprintf(Gains, "%i,\n", APV->DetId);
1096           DetIdOfBuggedAPV.push_back(APV->DetId);
1097         }
1098       }
1099     }
1100 
1101     fclose(Gains);
1102 
1103     //      delete [] FitResults;
1104     //      delete Proj;
1105   }
1106 
1107   dqmStore_->cd();
1108   dqmStore_->save(OutputHistos);
1109 }
1110 
1111 void SiStripGainFromData::algoBeginRun(const edm::Run&, const edm::EventSetup& iSetup) {
1112   if ((strcmp(AlgoMode.c_str(), "MultiJob") != 0 && !FirstSetOfConstants) || Validation) {
1113     const auto gainHandle = iSetup.getHandle(gainToken_);
1114     if (!gainHandle.isValid()) {
1115       printf("\n#####################\n\nERROR --> gainHandle is not valid\n\n#####################\n\n");
1116       exit(0);
1117     }
1118 
1119     for (std::vector<stAPVGain*>::iterator it = APVsCollOrdered.begin(); it != APVsCollOrdered.end(); it++) {
1120       stAPVGain* APV = *it;
1121 
1122       if (gainHandle.isValid()) {
1123         SiStripApvGain::Range detGainRange = gainHandle->getRange(APV->DetId);
1124         APV->PreviousGain = *(detGainRange.first + APV->APVId);
1125         //             APV_PrevGain->Fill(APV->Index,APV->PreviousGain);
1126         APV_PrevGain->SetBinContent(APV_PrevGain->GetXaxis()->FindBin(APV->Index), APV->PreviousGain);
1127         if (APV->PreviousGain < 0)
1128           APV->PreviousGain = 1;
1129       } else {
1130         printf("GAIN HANDLE IS NOT VALID\n");
1131       }
1132     }
1133   }
1134 }
1135 
1136 void SiStripGainFromData::algoAnalyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
1137   if (strcmp(AlgoMode.c_str(), "WriteOnDB") == 0)
1138     return;
1139 
1140   if (NEvent == 0) {
1141     SRun = iEvent.id().run();
1142     SEvent = iEvent.id().event();
1143     STimestamp = iEvent.time().value();
1144   }
1145   ERun = iEvent.id().run();
1146   EEvent = iEvent.id().event();
1147   ETimestamp = iEvent.time().value();
1148   NEvent++;
1149 
1150   iEvent_ = &iEvent;
1151 
1152   Handle<TrajTrackAssociationCollection> trajTrackAssociationHandle;
1153   iEvent.getByLabel(TrajToTrackProducer, TrajToTrackLabel, trajTrackAssociationHandle);
1154   const TrajTrackAssociationCollection TrajToTrackMap = *trajTrackAssociationHandle.product();
1155 
1156   for (TrajTrackAssociationCollection::const_iterator it = TrajToTrackMap.begin(); it != TrajToTrackMap.end(); ++it) {
1157     const Track track = *it->val;
1158     const Trajectory traj = *it->key;
1159 
1160     if (track.p() < MinTrackMomentum || track.p() > MaxTrackMomentum || track.eta() < MinTrackEta ||
1161         track.eta() > MaxTrackEta)
1162       continue;
1163 
1164     Tracks_Pt_Vs_Eta->Fill(fabs(track.eta()), track.pt());
1165     Tracks_P_Vs_Eta->Fill(fabs(track.eta()), track.p());
1166 
1167     //BEGIN TO COMPUTE NDOF FOR TRACKS NO IMPLEMENTED BEFORE 200pre3
1168     int ndof = 0;
1169     const Trajectory::RecHitContainer transRecHits = traj.recHits();
1170 
1171     for (Trajectory::RecHitContainer::const_iterator rechit = transRecHits.begin(); rechit != transRecHits.end();
1172          ++rechit)
1173       if ((*rechit)->isValid())
1174         ndof += (*rechit)->dimension();
1175     ndof -= 5;
1176     //END TO COMPUTE NDOF FOR TRACKS NO IMPLEMENTED BEFORE 200pre3
1177 
1178     HTrackChi2OverNDF->Fill(traj.chiSquared() / ndof);
1179     if (traj.chiSquared() / ndof > MaxTrackChiOverNdf)
1180       continue;
1181 
1182     vector<TrajectoryMeasurement> measurements = traj.measurements();
1183     HTrackHits->Fill(traj.foundHits());
1184     if (traj.foundHits() < (int)MinTrackHits)
1185       continue;
1186     /*
1187       //BEGIN TO COMPUTE #MATCHEDRECHIT IN THE TRACK
1188       int NMatchedHit = 0;
1189       for(vector<TrajectoryMeasurement>::const_iterator measurement_it = measurements.begin(); measurement_it!=measurements.end(); measurement_it++){
1190          TrajectoryStateOnSurface trajState = measurement_it->updatedState();
1191          if( !trajState.isValid() ) continue;
1192          const TrackingRecHit*         hit               = (*measurement_it->recHit()).hit();
1193          const SiStripMatchedRecHit2D* sistripmatchedhit = dynamic_cast<const SiStripMatchedRecHit2D*>(hit);
1194      if(sistripmatchedhit)NMatchedHit++;
1195 //       NMatchedHit++;
1196 
1197       }
1198       //END TO COMPUTE #MATCHEDRECHIT IN THE TRACK
1199 
1200       if(NMatchedHit<2){
1201      printf("NOT ENOUGH MATCHED RECHITS : %i\n",NMatchedHit);
1202      continue;
1203       }
1204 */
1205 
1206     for (vector<TrajectoryMeasurement>::const_iterator measurement_it = measurements.begin();
1207          measurement_it != measurements.end();
1208          measurement_it++) {
1209       TrajectoryStateOnSurface trajState = measurement_it->updatedState();
1210       if (!trajState.isValid())
1211         continue;
1212 
1213       const TrackingRecHit* hit = (*measurement_it->recHit()).hit();
1214       const SiStripRecHit1D* sistripsimple1dhit = dynamic_cast<const SiStripRecHit1D*>(hit);
1215       const SiStripRecHit2D* sistripsimplehit = dynamic_cast<const SiStripRecHit2D*>(hit);
1216       const SiStripMatchedRecHit2D* sistripmatchedhit = dynamic_cast<const SiStripMatchedRecHit2D*>(hit);
1217 
1218       if (sistripsimplehit) {
1219         ComputeChargeOverPath(
1220             (sistripsimplehit->cluster()).get(), trajState, &iSetup, &track, traj.chiSquared() / ndof);
1221       } else if (sistripmatchedhit) {
1222         ComputeChargeOverPath(&sistripmatchedhit->monoCluster(), trajState, &iSetup, &track, traj.chiSquared() / ndof);
1223         ComputeChargeOverPath(
1224             &sistripmatchedhit->stereoCluster(), trajState, &iSetup, &track, traj.chiSquared() / ndof);
1225       } else if (sistripsimple1dhit) {
1226         ComputeChargeOverPath(
1227             (sistripsimple1dhit->cluster()).get(), trajState, &iSetup, &track, traj.chiSquared() / ndof);
1228       } else {
1229       }
1230     }
1231   }
1232 }
1233 
1234 double SiStripGainFromData::ComputeChargeOverPath(const SiStripCluster* Cluster,
1235                                                   TrajectoryStateOnSurface trajState,
1236                                                   const edm::EventSetup* iSetup,
1237                                                   const Track* track,
1238                                                   double trajChi2OverN) {
1239   LocalVector trackDirection = trajState.localDirection();
1240   double cosine = trackDirection.z() / trackDirection.mag();
1241   auto const& Ampls = Cluster->amplitudes();
1242   uint32_t DetId = 0;  // is 0 since long time Cluster->geographicalId();
1243   int FirstStrip = Cluster->firstStrip();
1244   int APVId = FirstStrip / 128;
1245   stAPVGain* APV = APVsColl[(DetId << 3) | APVId];
1246   int Saturation = 0;
1247   bool Overlaping = false;
1248   int Charge = 0;
1249   unsigned int NHighStrip = 0;
1250 
1251   if (!IsFarFromBorder(trajState, DetId, iSetup))
1252     return -1;
1253 
1254   if (FirstStrip == 0)
1255     Overlaping = true;
1256   if (FirstStrip == 128)
1257     Overlaping = true;
1258   if (FirstStrip == 256)
1259     Overlaping = true;
1260   if (FirstStrip == 384)
1261     Overlaping = true;
1262   if (FirstStrip == 512)
1263     Overlaping = true;
1264   if (FirstStrip == 640)
1265     Overlaping = true;
1266 
1267   if (FirstStrip <= 127 && FirstStrip + Ampls.size() > 127)
1268     Overlaping = true;
1269   if (FirstStrip <= 255 && FirstStrip + Ampls.size() > 255)
1270     Overlaping = true;
1271   if (FirstStrip <= 383 && FirstStrip + Ampls.size() > 383)
1272     Overlaping = true;
1273   if (FirstStrip <= 511 && FirstStrip + Ampls.size() > 511)
1274     Overlaping = true;
1275   if (FirstStrip <= 639 && FirstStrip + Ampls.size() > 639)
1276     Overlaping = true;
1277 
1278   if (FirstStrip + Ampls.size() == 127)
1279     Overlaping = true;
1280   if (FirstStrip + Ampls.size() == 255)
1281     Overlaping = true;
1282   if (FirstStrip + Ampls.size() == 383)
1283     Overlaping = true;
1284   if (FirstStrip + Ampls.size() == 511)
1285     Overlaping = true;
1286   if (FirstStrip + Ampls.size() == 639)
1287     Overlaping = true;
1288   if (FirstStrip + Ampls.size() == 767)
1289     Overlaping = true;
1290   if (Overlaping)
1291     return -1;
1292 
1293   /*
1294    if(FirstStrip==0                                 )Overlaping=true;
1295    if(FirstStrip<=255 && FirstStrip+Ampls.size()>255)Overlaping=true;
1296    if(FirstStrip<=511 && FirstStrip+Ampls.size()>511)Overlaping=true;
1297    if(FirstStrip+Ampls.size()==511                  )Overlaping=true;
1298    if(FirstStrip+Ampls.size()==767                  )Overlaping=true;
1299    if(Overlaping)return -1;
1300 */
1301 
1302   for (unsigned int a = 0; a < Ampls.size(); a++) {
1303     Charge += Ampls[a];
1304     if (Ampls[a] >= 254)
1305       Saturation++;
1306     if (Ampls[a] >= 20)
1307       NHighStrip++;
1308   }
1309   double path = (10.0 * APV->Thickness) / fabs(cosine);
1310   double ClusterChargeOverPath = (double)Charge / path;
1311 
1312   NSatStripInCluster->Fill(Saturation);
1313 
1314   if (Ampls.size() > MaxNrStrips)
1315     return -1;
1316   if (Saturation > 0 && !AllowSaturation)
1317     return -1;
1318   Charge_Vs_PathLength->Fill(path, Charge);
1319   if (APV->Thickness < 0.04)
1320     Charge_Vs_PathLength320->Fill(path, Charge);
1321   if (APV->Thickness > 0.04)
1322     Charge_Vs_PathLength500->Fill(path, Charge);
1323   if (APV->SubDet == StripSubdetector::TIB)
1324     Charge_Vs_PathTIB->Fill(path, Charge);
1325   if (APV->SubDet == StripSubdetector::TID)
1326     Charge_Vs_PathTID->Fill(path, Charge);
1327   if (APV->SubDet == StripSubdetector::TOB)
1328     Charge_Vs_PathTOB->Fill(path, Charge);
1329   if (APV->SubDet == StripSubdetector::TEC) {
1330     Charge_Vs_PathTEC->Fill(path, Charge);
1331     if (APV->Thickness < 0.04)
1332       Charge_Vs_PathTEC1->Fill(path, Charge);
1333     if (APV->Thickness > 0.04)
1334       Charge_Vs_PathTEC2->Fill(path, Charge);
1335   }
1336 
1337   double trans = atan2(trackDirection.y(), trackDirection.x()) * (180 / 3.14159265);
1338   double alpha =
1339       acos(trackDirection.x() / sqrt(pow(trackDirection.x(), 2) + pow(trackDirection.z(), 2))) * (180 / 3.14159265);
1340   double beta =
1341       acos(trackDirection.y() / sqrt(pow(trackDirection.x(), 2) + pow(trackDirection.z(), 2))) * (180 / 3.14159265);
1342 
1343   if (path > 0.4 && path < 0.45) {
1344     Charge_Vs_TransversAngle->Fill(trans, Charge / path);
1345     Charge_Vs_Alpha->Fill(alpha, Charge / path);
1346     Charge_Vs_Beta->Fill(beta, Charge / path);
1347   }
1348 
1349   NStrips_Vs_TransversAngle->Fill(trans, Ampls.size());
1350   NStrips_Vs_Alpha->Fill(alpha, Ampls.size());
1351   NStrips_Vs_Beta->Fill(beta, Ampls.size());
1352 
1353   NHighStripInCluster->Fill(NHighStrip);
1354   //   if(NHighStrip==1)   Charge_Vs_PathLength_CS1->Fill(path, Charge );
1355   //   if(NHighStrip==2)   Charge_Vs_PathLength_CS2->Fill(path, Charge );
1356   //   if(NHighStrip==3)   Charge_Vs_PathLength_CS3->Fill(path, Charge );
1357   //   if(NHighStrip==4)   Charge_Vs_PathLength_CS4->Fill(path, Charge );
1358   //   if(NHighStrip==5)   Charge_Vs_PathLength_CS5->Fill(path, Charge );
1359 
1360   HFirstStrip->Fill(FirstStrip);
1361 
1362   if (Validation) {
1363     ClusterChargeOverPath = ClusterChargeOverPath / APV->PreviousGain;
1364   }
1365 
1366   APV_Charge->Fill(APV->Index, ClusterChargeOverPath);
1367   APV_Momentum->Fill(APV->Index, trajState.globalMomentum().mag());
1368   APV_PathLength->Fill(APV->Index, path);
1369 
1370   return ClusterChargeOverPath;
1371 }
1372 
1373 bool SiStripGainFromData::IsFarFromBorder(TrajectoryStateOnSurface trajState,
1374                                           const uint32_t detid,
1375                                           const edm::EventSetup* iSetup) {
1376   const auto& tkGeom = iSetup->getData(tkGeomToken_);
1377 
1378   LocalPoint HitLocalPos = trajState.localPosition();
1379   LocalError HitLocalError = trajState.localError().positionError();
1380 
1381   const GeomDetUnit* it = tkGeom.idToDetUnit(DetId(detid));
1382   if (dynamic_cast<const StripGeomDetUnit*>(it) == nullptr && dynamic_cast<const PixelGeomDetUnit*>(it) == nullptr) {
1383     std::cout << "this detID doesn't seem to belong to the Tracker" << std::endl;
1384     return false;
1385   }
1386 
1387   const BoundPlane plane = it->surface();
1388   const TrapezoidalPlaneBounds* trapezoidalBounds(dynamic_cast<const TrapezoidalPlaneBounds*>(&(plane.bounds())));
1389   const RectangularPlaneBounds* rectangularBounds(dynamic_cast<const RectangularPlaneBounds*>(&(plane.bounds())));
1390 
1391   double DistFromBorder = 1.0;
1392   //double HalfWidth      = it->surface().bounds().width()  /2.0;
1393   double HalfLength;
1394 
1395   if (trapezoidalBounds) {
1396     std::array<const float, 4> const& parameters = (*trapezoidalBounds).parameters();
1397     HalfLength = parameters[3];
1398     //double t       = (HalfLength + HitLocalPos.y()) / (2*HalfLength) ;
1399     //HalfWidth      = parameters[0] + (parameters[1]-parameters[0]) * t;
1400   } else if (rectangularBounds) {
1401     //HalfWidth      = it->surface().bounds().width()  /2.0;
1402     HalfLength = it->surface().bounds().length() / 2.0;
1403   } else {
1404     return false;
1405   }
1406 
1407   //  if (fabs(HitLocalPos.x())+HitLocalError.xx() >= (HalfWidth  - DistFromBorder) ) return false;//Don't think is really necessary
1408   if (fabs(HitLocalPos.y()) + HitLocalError.yy() >= (HalfLength - DistFromBorder))
1409     return false;
1410 
1411   return true;
1412 }
1413 
1414 void SiStripGainFromData::getPeakOfLandau(TH1* InputHisto, double* FitResults, double LowRange, double HighRange) {
1415   double adcs = -0.5;
1416   double adcs_err = 0.;
1417   double width = -0.5;
1418   double width_err = 0;
1419   double chi2overndf = -0.5;
1420 
1421   double nr_of_entries = InputHisto->GetEntries();
1422 
1423   if ((unsigned int)nr_of_entries < MinNrEntries) {
1424     FitResults[0] = adcs;
1425     FitResults[1] = adcs_err;
1426     FitResults[2] = width;
1427     FitResults[3] = width_err;
1428     FitResults[4] = chi2overndf;
1429     return;
1430   }
1431 
1432   // perform fit with standard landau
1433   TF1* MyLandau = new TF1("MyLandau", "landau", LowRange, HighRange);
1434   MyLandau->SetParameter("MPV", 300);
1435 
1436   InputHisto->Fit(MyLandau, "QR WW");
1437 
1438   // MPV is parameter 1 (0=constant, 1=MPV, 2=Sigma)
1439   adcs = MyLandau->GetParameter("MPV");
1440   adcs_err = MyLandau->GetParError(1);
1441   width = MyLandau->GetParameter(2);
1442   width_err = MyLandau->GetParError(2);
1443   chi2overndf = MyLandau->GetChisquare() / MyLandau->GetNDF();
1444 
1445   // if still wrong, give up
1446   if (adcs < 2. || chi2overndf > MaxChi2OverNDF) {
1447     adcs = -0.5;
1448     adcs_err = 0.;
1449     width = -0.5;
1450     width_err = 0;
1451     chi2overndf = -0.5;
1452   }
1453 
1454   FitResults[0] = adcs;
1455   FitResults[1] = adcs_err;
1456   FitResults[2] = width;
1457   FitResults[3] = width_err;
1458   FitResults[4] = chi2overndf;
1459 
1460   delete MyLandau;
1461 }
1462 
1463 std::unique_ptr<SiStripApvGain> SiStripGainFromData::getNewObject() {
1464   cout << "START getNewObject\n";
1465 
1466   //  if( !(strcmp(AlgoMode.c_str(),"WriteOnDB")==0 || strcmp(AlgoMode.c_str(),"SingleJob")==0) )return NULL;
1467   if (!(strcmp(AlgoMode.c_str(), "WriteOnDB") == 0 || strcmp(AlgoMode.c_str(), "SingleJob") == 0))
1468     return std::make_unique<SiStripApvGain>();
1469 
1470   auto obj = std::make_unique<SiStripApvGain>();
1471   std::vector<float>* theSiStripVector = nullptr;
1472   unsigned int PreviousDetId = -1;
1473   for (unsigned int a = 0; a < APVsCollOrdered.size(); a++) {
1474     stAPVGain* APV = APVsCollOrdered[a];
1475     if (APV == nullptr) {
1476       printf("Bug\n");
1477       continue;
1478     }
1479     if (APV->DetId != PreviousDetId) {
1480       if (theSiStripVector != nullptr) {
1481         SiStripApvGain::Range range(theSiStripVector->begin(), theSiStripVector->end());
1482         if (!obj->put(PreviousDetId, range))
1483           printf("Bug to put detId = %i\n", PreviousDetId);
1484       }
1485 
1486       theSiStripVector = new std::vector<float>;
1487       PreviousDetId = APV->DetId;
1488     }
1489     printf("%i | %i | PreviousGain = %7.5f NewGain = %7.5f\n", APV->DetId, APV->APVId, APV->PreviousGain, APV->Gain);
1490     if (theSiStripVector != nullptr) {
1491       theSiStripVector->push_back(APV->Gain);
1492     }
1493     //      theSiStripVector->push_back(APV->Gain);
1494   }
1495 
1496   if (theSiStripVector != nullptr) {
1497     SiStripApvGain::Range range(theSiStripVector->begin(), theSiStripVector->end());
1498     if (!obj->put(PreviousDetId, range))
1499       printf("Bug to put detId = %i\n", PreviousDetId);
1500   }
1501 
1502   cout << "END getNewObject\n";
1503   return obj;
1504 }
1505 
1506 DEFINE_FWK_MODULE(SiStripGainFromData);