201 {
202
203 std::cout << "ElectronCalibration: reading in file " << m_filename << std::endl;
204
205 TFile* infile = new TFile(m_filename);
206 TTree* track = (TTree*)infile->Get("track");
207
208
209
210
211
212 int kMaxEntries = 100;
213 double doca[kMaxEntries];
214 double enta[kMaxEntries];
215 double dedxhit[kMaxEntries];
217
218
219 int b2nhit;
220
221
222 double b3nhit;
223
224 track->SetBranchAddress("doca", doca);
225 track->SetBranchAddress("enta", enta);
226 track->SetBranchAddress("dedxhit", dedxhit);
227
228 if( m_type == 0 )
229 track->SetBranchAddress("numGoodHits", &b3nhit);
230 else if( m_type == 1 )
231 track->SetBranchAddress("numGoodLayerHits", &b2nhit);
232
233 double docastep = (m_upperdoca-m_lowerdoca)/m_docabins;
234 double entastep[m_entabins];
235
236
237
238
239
240
241 TH1F* totalenta = new TH1F("totalenta","",314,0,0.785);
242 for( unsigned int index = 0; index < track->GetEntries(); ++index ){
243 track->GetEvent(index);
244
245 if( m_type == 0 )
246 nhits = std::floor(b3nhit);
247 else if( m_type == 1 )
249 for(
int hit = 0; hit <
nhits; ++hit ){
250
251
252 if( enta[hit] > PI/4.0 ) enta[hit] -=
PI/2.0;
253 else if( enta[hit] < -1.0*PI/4.0 ) enta[hit] +=
PI/2.0;
254 totalenta->Fill(std::abs(enta[hit]));
255 }
256 }
257 entastep[0] = -0.785;
258 entastep[39] = 0.785;
259 int bin = 20;
int binmin = 1;
260 double total = totalenta->Integral()*2.0/(m_entabins+1);
261 for( int i = 2; i <= 314; ++i ){
262 if( totalenta->Integral(binmin,i) >= total ){
263 entastep[
bin] = 0.0025*i;
265 binmin = i+1;
266 }
267 }
268 for( int i = 0; i < m_entabins; ++i ){
269 if( i > 0 && i < 20 ) entastep[i] = -1.0*entastep[39-i];
270 }
271
272
273
274
275
276 TTree* outTree = new TTree("electrons","2D means and errors");
277
278 double satdoca;
279 double satenta;
280 double satdedx;
281 double saterror;
282
283 outTree->Branch("doca",&satdoca,"doca/D");
284 outTree->Branch("enta",&satenta,"enta/D");
285 outTree->Branch("dedx",&satdedx,"dedx/D");
286 outTree->Branch("error",&saterror,"error/D");
287
288 TH2F* twod = new TH2F("twod","",m_docabins,m_lowerdoca,m_upperdoca,m_entabins,m_lowerenta,m_upperenta);
289
290
291
292
293
294 if( m_fits == 0 ){
295
296 std::vector<double> docaent[m_docabins][m_entabins];
297 int docaenthits[m_docabins][m_entabins];
298 for( int i = 0; i < m_docabins; ++i ){
299 for( int j = 0; j < m_docabins; ++j ){
300 docaent[i][j].clear();
301 docaenthits[i][j] = 0;
302 }
303 }
304
305 for( unsigned int index = 0; index < track->GetEntries(); ++index ){
306 track->GetEvent(index);
307
308 if( m_type == 0 )
309 nhits = std::floor(b3nhit);
310 else if( m_type == 1 )
312 for(
int hit = 0; hit <
nhits; ++hit ){
313
314
315 if( enta[hit] > PI/4.0 ) enta[hit] -=
PI/2.0;
316 else if( enta[hit] < -1.0*PI/4.0 ) enta[hit] +=
PI/2.0;
317
318 if( doca[hit] > m_upperdoca || doca[hit] < m_lowerdoca ) continue;
319 if( enta[hit] > m_upperenta || enta[hit] < m_lowerenta ) continue;
320
321 int docaBin = (int)((doca[hit]-m_lowerdoca)/(m_upperdoca-m_lowerdoca) * m_docabins);
322 int entaBin = (int)((enta[hit]-m_lowerenta)/(m_upperenta-m_lowerenta) * m_entabins);
323
324 docaent[docaBin][entaBin].push_back(dedxhit[hit]);
325 docaenthits[docaBin][entaBin]++;
326 }
327 }
328
329 double tmean;
331 for( int i = 0; i < m_docabins; ++i ){
332 for( int j = 0; j < m_entabins; ++j ){
333
334
335 satdoca = m_lowerdoca+0.5*docastep+i*docastep;
336 satenta = m_lowerenta+0.5*entastep[j]+j*entastep[j];
337
338
339 ecor.
calculateMeans(tmean,satdedx,saterror,docaent[i][j],docaenthits[i][j]);
340 twod->SetBinContent(i,j,satdedx);
341
342 std::cout << i << "\t" << j << "\t" << satdedx << "\t" << saterror << std::endl;
343
344
345 outTree->Fill();
346 }
347 }
348 }
349
350
351
352
353
354 if( m_fits == 1 ){
355
356
357 TH1F* hdoca_enta[m_docabins][m_entabins];
358
359
360 for( int i = 0; i < m_docabins; ++i ){
361 for( int j = 0; j < m_entabins; ++j ){
362 char histname[100];
363 sprintf(histname,
"doca_%d_enta_%d",i,j);
364
365 hdoca_enta[i][j] = new TH1F(histname,histname,200,0,200);
366 }
367 }
368
369
370 for( unsigned int index = 0; index < track->GetEntries(); ++index ){
371 track->GetEvent(index);
372
373 if( m_type == 0 )
374 nhits = std::floor(b3nhit);
375 else if( m_type == 1 )
377 for(
int hit = 0; hit <
nhits; ++hit ){
378
379
380 if( enta[hit] > PI/4.0 ) enta[hit] -=
PI/2.0;
381 else if( enta[hit] < -1.0*PI/4.0 ) enta[hit] +=
PI/2.0;
382
383 if( doca[hit] > m_upperdoca || doca[hit] < m_lowerdoca ) continue;
384 if( enta[hit] > m_upperenta || enta[hit] < m_lowerenta ) continue;
385
386 int docaBin = (int)((doca[hit]-m_lowerdoca)/(m_upperdoca-m_lowerdoca) * m_docabins);
387 int entaBin = (int)((enta[hit]-m_lowerenta)/(m_upperenta-m_lowerenta) * m_entabins);
388
389 hdoca_enta[docaBin][entaBin]->Fill(dedxhit[hit]);
390 }
391 }
392
393
394
395
396
397
398
399 int fs1;
400 for( int i = 0; i < m_docabins; ++i ){
401 for( int j = 0; j < m_entabins; ++j ){
402
403
404 satdoca = m_lowerdoca+0.5*docastep+i*docastep;
405 satenta = m_lowerenta+0.5*entastep[j]+j*entastep[j];
406
408 fs = hdoca_enta[i][j]->Fit(
"landau",
"ql");
409 double mean = hdoca_enta[i][j]->GetFunction("landau")->GetParameter(1);
410 double width = hdoca_enta[i][j]->GetFunction("landau")->GetParameter(2);
411
412 fs = hdoca_enta[i][j]->Fit(
"landau",
"ql");
414
415 satdedx = hdoca_enta[i][j]->GetFunction("landau")->GetParameter(1);
416 saterror = hdoca_enta[i][j]->GetFunction("landau")->GetParError(1);
417
418 if(
fs == 0 ) twod->SetBinContent(i,j,satdedx);
419
420
421 outTree->Fill();
422 }
423 }
424 if( fs1 != 0 ) std::cout << "\t\t" <<"MEAN FITS FAILED: " << fs1 << std::endl;
425
426
427
428 TCanvas* ctmp = new TCanvas("tmp","tmp",900,900);
429 ctmp->Divide(3,3);
430 std::stringstream psname; psname << "plots/twoDFits.ps[";
431 ctmp->Print(psname.str().c_str());
432 psname.str(""); psname << "plots/twoDFits.ps";
433 for( int i = 0 ; i < m_docabins; ++i ){
434 for( int j = 0; j < m_entabins; ++j ){
435 ctmp->cd(j%9+1);
436 hdoca_enta[i][j]->Draw();
437 if((j+1)%9==0)
438 ctmp->Print(psname.str().c_str());
439 }
440 }
441 psname.str(""); psname << "plots/twoDFits.ps]";
442 ctmp->Print(psname.str().c_str());
443 delete ctmp;
444
445 for( int i = 0; i < m_docabins; ++i ){
446 for( int j = 0; j < m_entabins; ++j ){
447 delete hdoca_enta[i][j];
448 }
449 }
450 delete totalenta;
451 }
452
453
454 outfile->cd();
455 outTree->Write();
456 twod->Write();
457 infile->Close();
458}
sprintf(cut,"kal_costheta0_em>-0.93&&kal_costheta0_em<0.93&&kal_pxy0_em>=0.05+%d*0.1&&kal_pxy0_em<0.15+%d*0.1&&NGch>=2", j, j)
void calculateMeans(double &mean, double &truncatedMean, double &truncatedMeanErr, double dedx[], int size) const