19 #include "CLHEP/Random/RandFlat.h"
20 #include "CLHEP/Random/RandGauss.h"
41 edm::LogInfo(
"SiStripGainCosmicCalculator::SiStripGainCosmicCalculator");
56 edm::LogInfo(
"SiStripApvGainCalculator")<<
"The calibration for these DetIds will be set to a default value";
58 edm::LogInfo(
"SiStripApvGainCalculator")<<
"exclude detid = "<< *imod;
66 edm::LogInfo(
"SiStripGainCosmicCalculator::~SiStripGainCosmicCalculator");
75 std::cout<<
"SiStripGainCosmicCalculator::algoBeginJob called"<<std::endl;
79 HlistOtherHistos->Add(
new TH1F( Form(
"APVPairCorrections"), Form(
"APVPairCorrections"), 50,-1.,4.));
80 HlistOtherHistos->Add(
new TH1F(Form(
"APVPairCorrectionsTIB1mono"),Form(
"APVPairCorrectionsTIB1mono"),50,-1.,4.));
81 HlistOtherHistos->Add(
new TH1F(Form(
"APVPairCorrectionsTIB1stereo"),Form(
"APVPairCorrectionsTIB1stereo"),50,-1.,4.));
82 HlistOtherHistos->Add(
new TH1F(Form(
"APVPairCorrectionsTIB2"),Form(
"APVPairCorrectionsTIB2"),50,-1.,4.));
83 HlistOtherHistos->Add(
new TH1F(Form(
"APVPairCorrectionsTOB1"),Form(
"APVPairCorrectionsTOB1"),50,-1.,4.));
84 HlistOtherHistos->Add(
new TH1F(Form(
"APVPairCorrectionsTOB2"),Form(
"APVPairCorrectionsTOB2"),50,-1.,4.));
85 HlistOtherHistos->Add(
new TH1F(Form(
"LocalAngle"),Form(
"LocalAngle"),70,-0.1,3.4));
86 HlistOtherHistos->Add(
new TH1F(Form(
"LocalAngleAbsoluteCosine"),Form(
"LocalAngleAbsoluteCosine"),48,-0.1,1.1));
87 HlistOtherHistos->Add(
new TH1F(Form(
"LocalPosition_cm"),Form(
"LocalPosition_cm"),100,-5.,5.));
88 HlistOtherHistos->Add(
new TH1F(Form(
"LocalPosition_normalized"),Form(
"LocalPosition_normalized"),100,-1.1,1.1));
89 TH1F* local_histo =
new TH1F(Form(
"SiStripRecHitType"),Form(
"SiStripRecHitType"),2,0.5,2.5);
HlistOtherHistos->Add(local_histo);
90 local_histo->GetXaxis()->SetBinLabel(1,
"simple"); local_histo->GetXaxis()->SetBinLabel(2,
"matched");
94 std::vector<uint32_t> activeDets; activeDets.clear();
96 siStripDetCabling->addActiveDetectorsRawIds(activeDets);
104 for(TrackerGeometry::DetContainer::const_iterator it = tkGeom->dets().begin(); it != tkGeom->dets().end(); it++){
105 if( dynamic_cast<StripGeomDetUnit*>((*it))!=0){
106 uint32_t
detid=((*it)->geographicalId()).rawId();
108 double module_thickness = (*it)->surface().bounds().thickness();
109 thickness_map.insert(std::make_pair(detid,module_thickness));
111 bool is_active_detector =
false;
113 if( *iactive == detid ){
114 is_active_detector =
true;
119 bool exclude_this_detid =
false;
121 if(*imod == detid) exclude_this_detid =
true;
125 if(is_active_detector && (!exclude_this_detid)){
127 unsigned short NAPVPairs = p.
nstrips()/256;
128 if( NAPVPairs<2 || NAPVPairs>3 ) {
129 edm::LogError(
"SiStripGainCosmicCalculator")<<
"Problem with Number of strips in detector: "<<p.
nstrips()<<
" Exiting program";
132 for(
int iapp = 0; iapp<NAPVPairs; iapp++){
133 TString hid = Form(
"ChargeAPVPair_%i_%i",detid,iapp);
162 for(reco::TrackCollection::const_iterator itr =
tracks->begin(); itr !=
tracks->end(); itr++){
166 std::vector<std::pair<const TrackingRecHit *,float> >hitangle;
168 for(std::vector<std::pair<const TrackingRecHit *,float> >::const_iterator hitangle_iter=hitangle.begin();hitangle_iter!=hitangle.end();hitangle_iter++){
170 float local_angle=hitangle_iter->second;
172 const SiStripRecHit2D* sistripsimplehit=
dynamic_cast<const SiStripRecHit2D*
>(trechit);
173 const SiStripMatchedRecHit2D* sistripmatchedhit=
dynamic_cast<const SiStripMatchedRecHit2D*
>(trechit);
177 if(sistripsimplehit){
179 const SiStripRecHit2D::ClusterRef & cluster=sistripsimplehit->cluster();
180 const std::vector<uint8_t>& ampls = cluster->amplitudes();
182 uint32_t thedetid = cluster->geographicalId();
183 double module_width =
moduleWidth(thedetid, &iSetup);
185 ((TH1F*)
HlistOtherHistos->FindObject(
"LocalPosition_normalized"))->
Fill(local_position.
x()/module_width);
187 int ifirststrip= cluster->firstStrip();
188 int theapvpairid = int(
float(ifirststrip)/256.);
189 TH1F* histopointer = (TH1F*)
HlistAPVPairs->FindObject(Form(
"ChargeAPVPair_%i_%i",thedetid,theapvpairid));
192 for(
unsigned int iampl = 0; iampl<ampls.size(); iampl++){
193 cCharge += ampls[iampl];
195 double cluster_charge_over_path = ((double)cCharge) * fabs(
cos(local_angle)) / ( 10. * module_thickness);
196 histopointer->Fill(cluster_charge_over_path);
209 double adcs = -0.5;
double error = 0.;
double nr_of_entries = inputHisto->GetEntries();
211 return std::make_pair(adcs,error);
222 inputHisto->Fit(
"landau",
"0Q");
223 TF1 * fitfunction = (TF1*) inputHisto->GetListOfFunctions()->First();
224 adcs = fitfunction->GetParameter(
"MPV");
225 error = fitfunction->GetParError(1);
226 double chi2 = fitfunction->GetChisquare();
227 double ndf = fitfunction->GetNDF();
228 double chi2overndf = chi2 / ndf;
230 if(adcs< 2. || (error/adcs)>1.8 ){
231 inputHisto->Fit(
"landau",
"0Q",0,0.,400.);
232 TF1 * fitfunction2 = (TF1*) inputHisto->GetListOfFunctions()->First();
233 std::cout<<
"refitting landau for histogram "<<inputHisto->GetTitle()<<std::endl;
234 std::cout<<
"initial error/adcs ="<<error<<
" / "<<adcs<<std::endl;
235 std::cout<<
"new error/adcs ="<<fitfunction2->GetParError(1)<<
" / "<<fitfunction2->GetParameter(
"MPV")<<std::endl;
236 adcs = fitfunction2->GetParameter(
"MPV");
237 error = fitfunction2->GetParError(1);
238 chi2 = fitfunction2->GetChisquare();
239 ndf = fitfunction2->GetNDF();
240 chi2overndf = chi2 / ndf;
244 adcs = -0.5; error = 0.;
246 return std::make_pair(adcs,error);
253 double module_width=0.;
255 if (dynamic_cast<const StripGeomDetUnit*>(it)==0 && dynamic_cast<const PixelGeomDetUnit*>(it)==0) {
256 std::cout <<
"this detID doesn't seem to belong to the Tracker" << std::endl;
267 double module_thickness=0.;
269 if (dynamic_cast<const StripGeomDetUnit*>(it)==0 && dynamic_cast<const PixelGeomDetUnit*>(it)==0) {
270 std::cout <<
"this detID doesn't seem to belong to the Tracker" << std::endl;
274 return module_thickness;
279 std::cout<<
"SiStripGainCosmicCalculator::getNewObject called"<<std::endl;
283 TH1F *ChargeOfEachAPVPair =
new TH1F(
"ChargeOfEachAPVPair",
"ChargeOfEachAPVPair",1,0,1); ChargeOfEachAPVPair->SetBit(TH1::kCanRebin);
284 TH1F *EntriesApvPairs =
new TH1F(
"EntriesApvPairs",
"EntriesApvPairs",1,0,1); EntriesApvPairs->SetBit(TH1::kCanRebin);
285 TH1F * NrOfEntries =
new TH1F(
"NrOfEntries",
"NrOfEntries",351,-0.5,350.5);
286 TH1F * ModuleThickness =
new TH1F(
"ModuleThickness",
"ModuleThickness",2,0.5,2.5);
HlistOtherHistos->Add(ModuleThickness);
287 ModuleThickness->GetXaxis()->SetBinLabel(1,
"320mu"); ModuleThickness->GetXaxis()->SetBinLabel(2,
"500mu"); ModuleThickness->SetYTitle(
"Nr APVPairs");
288 TH1F * ModuleWidth =
new TH1F(
"ModuleWidth",
"ModuleWidth",5,0.5,5.5);
HlistOtherHistos->Add(ModuleWidth);
289 ModuleWidth->GetXaxis()->SetBinLabel(1,
"6.144cm"); ModuleWidth->GetXaxis()->SetBinLabel(2,
"7.14cm");
290 ModuleWidth->GetXaxis()->SetBinLabel(3,
"9.3696cm"); ModuleWidth->GetXaxis()->SetBinLabel(4,
"10.49cm");
291 ModuleWidth->GetXaxis()->SetBinLabel(5,
"12.03cm");
292 ModuleWidth->SetYTitle(
"Nr APVPairs");
296 double MeanCharge = 0.;
297 double NrOfApvPairs = 0.;
298 TH1F *MyHisto = (TH1F*)hiterator();
300 TString histo_title = MyHisto->GetTitle();
301 if(histo_title.Contains(
"ChargeAPVPair_")){
303 double local_nrofadcs = two_values.first;
304 double local_sigma = two_values.second;
305 ChargeOfEachAPVPair->Fill(histo_title, local_nrofadcs);
306 int ichbin = ChargeOfEachAPVPair->GetXaxis()->FindBin(histo_title.Data());
307 ChargeOfEachAPVPair->SetBinError(ichbin,local_sigma);
308 EntriesApvPairs->Fill(histo_title, MyHisto->GetEntries());
309 NrOfEntries->Fill(MyHisto->GetEntries());
310 if(local_nrofadcs > 0){
311 MeanCharge += local_nrofadcs;
315 MyHisto = (TH1F*)hiterator();
317 ChargeOfEachAPVPair->LabelsDeflate(
"X"); EntriesApvPairs->LabelsDeflate(
"X");
321 MeanCharge = MeanCharge / NrOfApvPairs;
323 TH1F* CorrectionOfEachAPVPair = (TH1F*) ChargeOfEachAPVPair->Clone(
"CorrectionOfEachAPVPair");
324 TH1F *ChargeOfEachAPVPairControlView =
new TH1F(
"ChargeOfEachAPVPairControlView",
"ChargeOfEachAPVPairControlView",1,0,1); ChargeOfEachAPVPairControlView->SetBit(TH1::kCanRebin);
325 TH1F *CorrectionOfEachAPVPairControlView =
new TH1F(
"CorrectionOfEachAPVPairControlView",
"CorrectionOfEachAPVPairControlView",1,0,1); CorrectionOfEachAPVPairControlView->SetBit(TH1::kCanRebin);
326 std::ofstream APVPairTextOutput(
"apvpair_corrections.txt");
327 APVPairTextOutput<<
"# MeanCharge = "<<MeanCharge<<std::endl;
328 APVPairTextOutput<<
"# Nr. of APVPairs = "<<NrOfApvPairs<<std::endl;
329 for(
int ibin=1; ibin <= ChargeOfEachAPVPair->GetNbinsX(); ibin++){
330 TString local_bin_label = ChargeOfEachAPVPair->GetXaxis()->GetBinLabel(ibin);
331 double local_charge_over_path = ChargeOfEachAPVPair->GetBinContent(ibin);
332 if(local_bin_label.Contains(
"ChargeAPVPair_") && local_charge_over_path > 0.0000001){
333 uint32_t extracted_detid; std::istringstream read_label((local_bin_label(14,9)).
Data()); read_label >> extracted_detid;
334 unsigned short extracted_apvpairid; std::istringstream read_apvpair((local_bin_label(24,1)).
Data()); read_apvpair >> extracted_apvpairid;
335 double local_error_of_charge = ChargeOfEachAPVPair->GetBinError(ibin);
336 double local_correction = -0.5;
337 double local_error_correction = 0.;
338 local_correction = MeanCharge / local_charge_over_path;
339 local_error_correction = local_correction * local_error_of_charge / local_charge_over_path;
340 if(local_error_correction>1.8){
341 std::cout<<
"too large error "<<local_error_correction<<
" for histogram "<<local_bin_label<<std::endl;
343 double nr_of_entries = EntriesApvPairs->GetBinContent(ibin);
344 APVPairTextOutput<<local_bin_label<<
" "<<local_correction<<
" "<<local_charge_over_path<<
" "<<nr_of_entries<<std::endl;
345 CorrectionOfEachAPVPair->SetBinContent(ibin, local_correction);
346 CorrectionOfEachAPVPair->SetBinError(ibin, local_error_correction);
349 unsigned int generalized_layer = 0;
359 generalized_layer = 10*thedetId.
subdetId();
362 generalized_layer += ptob.
layer();
365 if(generalized_layer==31){
368 if(generalized_layer==32){
371 if(generalized_layer==33){
374 if(generalized_layer==51){
377 if(generalized_layer==52){
382 const FedChannelConnection& fedchannelconnection = siStripDetCabling->getConnection( extracted_detid, extracted_apvpairid );
383 std::ostringstream local_key;
385 local_key<<
"fecCrate"<<fedchannelconnection.
fecCrate()<<
"_fecSlot"<<fedchannelconnection.
fecSlot()<<
"_fecRing"<<fedchannelconnection.
fecRing()<<
"_ccuAddr"<<fedchannelconnection.
ccuAddr()<<
"_ccuChan"<<fedchannelconnection.
ccuChan()<<
"_apvPair"<<extracted_apvpairid;
386 TString control_key = local_key.str();
387 ChargeOfEachAPVPairControlView->Fill(control_key,local_charge_over_path);
388 int ibin1 = ChargeOfEachAPVPairControlView->GetXaxis()->FindBin(control_key);
389 ChargeOfEachAPVPairControlView->SetBinError(ibin1,local_error_of_charge);
390 CorrectionOfEachAPVPairControlView->Fill(control_key, local_correction);
391 int ibin2 = CorrectionOfEachAPVPairControlView->GetXaxis()->FindBin(control_key);
392 CorrectionOfEachAPVPairControlView->SetBinError(ibin2, local_error_correction);
395 if( fabs(module_thickness - 0.032)<0.001 ) ModuleThickness->Fill(1);
396 if( fabs(module_thickness - 0.05)<0.001 ) ModuleThickness->Fill(2);
399 if(fabs(module_width-6.144)<0.01) ModuleWidth->Fill(1);
400 if(fabs(module_width-7.14)<0.01) ModuleWidth->Fill(2);
401 if(fabs(module_width-9.3696)<0.01) ModuleWidth->Fill(3);
402 if(fabs(module_width-10.49)<0.01) ModuleWidth->Fill(4);
403 if(fabs(module_width-12.03)<0.01) ModuleWidth->Fill(5);
407 ChargeOfEachAPVPairControlView->LabelsDeflate(
"X");
408 CorrectionOfEachAPVPairControlView->LabelsDeflate(
"X");
const uint16_t & fecSlot() const
T getParameter(std::string const &) const
TObjArray * HlistAPVPairs
virtual int nstrips() const =0
T getUntrackedParameter(std::string const &, T const &) const
uint32_t total_nr_of_events
std::vector< uint32_t > SelectedDetIds
double ExpectedChargeDeposition
const uint16_t & fecCrate() const
unsigned int layer() const
layer id
double moduleThickness(const uint32_t detid, const edm::EventSetup *iSetup)
TObjArray * HlistOtherHistos
const edm::EventSetup * eventSetupCopy_
std::vector< Track > TrackCollection
collection of Tracks
~SiStripGainCosmicCalculator()
bool outputHistogramsInRootFile
double moduleWidth(const uint32_t detid, const edm::EventSetup *iSetup)
std::pair< double, double > getPeakOfLandau(TH1F *inputHisto)
uint32_t rawId() const
get the raw id
virtual float thickness() const =0
unsigned int MinNrEntries
const uint16_t & fecRing() const
void getTOBDetectors(const std::vector< uint32_t > &inputDetRawIds, std::vector< uint32_t > &tobDetRawIds, uint32_t layer=0, uint32_t bkw_frw=0, uint32_t rod=0) const
Class containning control, module, detector and connection information, at the level of a FED channel...
void Fill(HcalDetId &id, double val, std::vector< TH2F > &depth)
void algoBeginJob(const edm::EventSetup &)
Cos< T >::type cos(const T &t)
const uint16_t & ccuChan() const
SiStripGainCosmicCalculator(const edm::ParameterSet &)
std::map< uint32_t, double > thickness_map
int subdetId() const
get the contents of the subdetector field (not cast into any detector's numbering enum) ...
const uint16_t & ccuAddr() const
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
SiStripApvGain * getNewObject()
const Bounds & bounds() const
std::vector< uint32_t > detModulesToBeExcluded
unsigned int layer() const
layer id
void algoAnalyze(const edm::Event &, const edm::EventSetup &)
const BoundPlane & surface() const
The nominal surface of the GeomDet.
void getTIBDetectors(const std::vector< uint32_t > &inputDetRawIds, std::vector< uint32_t > &tibDetRawIds, uint32_t layer=0, uint32_t bkw_frw=0, uint32_t int_ext=0, uint32_t string=0) const
virtual LocalPoint localPosition() const =0
virtual float width() const =0