423{
424
425 TString htitle,
hname,textfilename,rootfilename;
426
427
429
430
431
432
433
434
435
437 printf("WARNING: Received instruction to select recurring bad cells only in SetSelectRecurringBadCells(bool flag)\n");
438 printf("-------> Only one algorithm selected for cell selection criteria in SetAlgo(int algoindex)\n");
439 printf("-------> Recurring cell selection overides algorithm specification\n");
440 std::abort();
441 }
442
444 printf("ERROR: Partition not specified in SetPartition(bool set_part, std::string partname)\n");
445 std::abort();
448 }
449
450
451 int selectionFlag = -1;
452 const double GeV = 1/1000.;
459 int nskipped=0;
460 int nBadCells=0;
461 int noEvdata=0;
462 unsigned int onlid = 0;
463 int ne=0;
464 int emin=0;
465 int emax=0;
466 int nq=0;
467 int qmin=0;
468 int qmax=0;
469 int nlb=0;
470 int nlb_corr=0;
474 int nEvents_E_gt_ecut=0;
475 double EventEnergySum=0.;
476 double EventEnergyMean=0.;
477
478 int nBadLBs=0;
479 double MeanHits=0, rmsHits=0;
480 int iMeanCellHitsSelection=0,iEnergyThresholdSelection=0,iOR_selection=0,iAND_selection=0;
481 int CellCounter=0;
482
483
484
485
486 vector<int, std::allocator<int> > DQLBList;
487 vector<int, std::allocator<int> > BadLBList;
489 printf("Reading in DQ Defect LB List...\n");
491 printf("...Done\n");
492 }
494 printf("Getting bad LB list...\n");
495 BadLBList=
GetBadLBList(inputfile,lbmin,lbmax,nsigma,nlb,DQLBList);
496 printf("...Done.\n");
497 nBadLBs = BadLBList.size();
498 }
500 BadLBList = std::move(DQLBList);
501 nBadLBs = BadLBList.size();
502 }
503
504
505 printf("Getting histogram limits... ");
506 GetLimits_EqLB(inputfile,lbmin,lbmax,emin,emax,qmin,qmax,runNumber,BadLBList);
510 }
511
512
514 ne=emax-emin;
515 nq=qmax-qmin;
516 printf("Done.\n LBmin: %d LBmax: %d Emin: %d Emax: %d Qmin: %d Qmax: %d\n",lbmin,lbmax,emin,emax,qmin,qmax);
517
518
521 textfilename.Form("Output/BadCellSelection_run%d.txt",runNumber);
523 if (!outputFile){
524 std::cout<<("Cannot open output selection summary file " + textfilename)<< "\n";
525 std::abort();}
526 } else {
529 if (!outputFile){
530 std::cout<<("Cannot open output selection summary file " + textfilename)<< "\n";
531 std::abort();}
532 }
533
534
535
536 FILE * pFile=nullptr;
539 textfilename.Form("Output/BadCellList_run%d.txt",runNumber);
540 pFile = fopen (textfilename , "w");
541 if (!pFile){
542 printf("Cannot open output text file\n");
543 std::abort();}
544 } else {
547 pFile = fopen (textfilename , "w");
548 if (!pFile){
549 printf("Cannot open output text file\n");
550 std::abort();}
551 }
552 }
553 }
554
555
556
557
558printf("Determining mean cell hits...\n ");
559 GetMeanCellHits(inputfile, nlb, lbmin, lbmax, nsigmaHits, BadLBList, MeanHits, rmsHits, nlb_corr);
560printf("Done.\n");
561printf("Set threshold at %4.3f counts per cell for LB range. \n",(MeanHits+(nsigmaHits*rmsHits))*((double)nlb_corr-(double)nBadLBs));
562
563
565 fprintf(pFile, "onlid // part // FT // SL // CH // Events (E>%d sigma) // Events E>1GeV // Mean Energy [GeV] // # LBs // fraction events Q>%d // Flagged in LB // selectionFlag\n",(int)nsigmaHits,qthreshold);
566 }
567
568 int EventCount=0., qcount=0.;
569
570
571
574 } else {
575 rootfilename.Form("Output/BadCells_run%d.root",runNumber);
576 }
577
578 auto fout = std::make_unique<TFile>(rootfilename,
"RECREATE");
579 Int_t larid = 0;
580 Int_t t_run =0;
581 Int_t n_ensig = 0;
582 Int_t n_ecut = 0;
583 Float_t fr_LB = 0;
584 Float_t nbrLB = 0;
586 Float_t meanECell = 0;
587 Float_t fr_q4k = 0;
588 TH1F* h1_lb =
nullptr;
589 TH1F* h1_e =
nullptr;
590 TH1F* h1_q =
nullptr;
591 TH2F* h2_elb =
nullptr;
592 TH2D* h2_pulse = nullptr;
593 TH2D* h2_t_LB = nullptr;
595 TH1F* h1_ADCmax =
nullptr;
596 std::unique_ptr<TTree> tree_cells;
598 tree_cells.reset(new TTree("BadTree","LAr Tree ordered by OnlID"));
599 tree_cells->Branch("Energy",&h1_e);
600 tree_cells->Branch("Quality",&h1_q);
601 tree_cells->Branch("LB",&h1_lb);
602 tree_cells->Branch("larid",&larid);
603 tree_cells->Branch("onlid",&onlid);
604 tree_cells->Branch("Run",&t_run);
605 tree_cells->Branch("selectionFlag",&selectionFlag);
606 tree_cells->Branch("nhits_ensig",&n_ensig);
607 tree_cells->Branch("nhits_e1GeV",&n_ecut);
608 tree_cells->Branch("Quality_4k",&fr_q4k);
609 tree_cells->Branch("LBfraction",&fr_LB);
610 tree_cells->Branch("EnergyVsLB",&h2_elb);
611 tree_cells->Branch("PulseShape",&h2_pulse);
612 tree_cells->Branch("PulsePeakTimeVsLB",&h2_t_LB);
613 tree_cells->Branch("noise",&noise);
614 tree_cells->Branch("MeanCellEnergy",&meanECell);
615 tree_cells->Branch("PulseShapeTProf",&TProf_pulse);
616 tree_cells->Branch("ADCmax",&h1_ADCmax);
617 }
618
620 Double_t xw=0.1;
621 TH2D NormPulse("Normalised_pulse","",100,-200,200,400,-10,10);
622 NormPulse.SetXTitle("Time [ns]");
623 NormPulse.SetYTitle("Value [ADC counts] / ADCmax");
624
625
632
633 TH2F E_LB(
"E_LB",
"",nlb,lbmin,lbmax,ne,emin,emax);
634 E_LB.SetXTitle("LB");
635 E_LB.SetYTitle("Energy [GeV]");
636
637 TH2D t_LB("t_LB","",nlb,lbmin,lbmax,400,-200,200);
638 t_LB.GetXaxis()->SetTitle("LB");
639 t_LB.GetYaxis()->SetTitle("Time(maxSample) + ofcTime [ns]");
640
641 TH1F CF_LB(
"CF_LB",
"",nlb,lbmin,lbmax);
642 CF_LB.GetXaxis()->SetTitle("LB");
643 CF_LB.GetYaxis()->SetTitle("Number of cells flagged");
644
645 TH1F ME_EV(
"ME_EV",
"",1000,0,1000);
646 ME_EV.GetXaxis()->SetTitle("Nbr events >1 GeV");
647 ME_EV.GetYaxis()->SetTitle("Mean Energy [GeV]");
648
649
650
652 for (int ii=0;ii<npl;ii++){
654 Cellmaps[ii]->GetXaxis()->SetTitle("#eta"); Cellmaps[ii]->GetYaxis()->SetTitle("#Phi");
656 Pulsemaps[ii].reset((TH2D*)NormPulse.Clone(hname));
658 E_LBmaps[ii].reset((TH2F*)E_LB.Clone(hname));
660 t_LBmaps[ii].reset((TH2D*)t_LB.Clone(hname));
662 CellsFlagged_LB_part[ii].reset((TH1F*)CF_LB.Clone(hname));
663
665 Emean_NbrEvents_part[ii].reset((TH1F*)ME_EV.Clone(hname));
666
667 }
668 }
670 Eeta.GetXaxis()->SetTitle("#eta");
671 Eeta.GetYaxis()->SetTitle("Energy [GeV]");
672
673 TH1F CellEnergy(
"CellEnergy",
"",ne,emin,emax);
674 CellEnergy.GetXaxis()->SetTitle("Cell Mean Energy [GeV]");
675 CellEnergy.GetYaxis()->SetTitle("Cells");
676
677 TH1F LBfrac(
"LBfrac",
"",100,0.,1.);
678 LBfrac.GetXaxis()->SetTitle("LB fraction");
679 LBfrac.GetYaxis()->SetTitle("Cells");
680
681 TH1F Qfrac(
"Qfrac",
"",100.,0.,1.);
682 Qfrac.GetXaxis()->SetTitle("Fraction of Events Q>4000");
683 Qfrac.GetYaxis()->SetTitle("Cells");
684
685 TH1F CellsFlagged_LB(
"CellsFlagged_LB",
"",nlb,lbmin,lbmax);
686 CellsFlagged_LB.GetXaxis()->SetTitle("LB");
687 CellsFlagged_LB.GetYaxis()->SetTitle("Number of cells flagged");
688
689 TH1F Emean_NbrEvents(
"Emean_NEvents_1GeV",
"",1000,0,1000);
690 Emean_NbrEvents.GetXaxis()->SetTitle("N events > 1 GeV");
691 Emean_NbrEvents.GetYaxis()->SetTitle("Mean Energy [GeV]");
692
693
694
695
696 printf("Creating histograms (nEbins = %d, nQbins = %d, nLBbins = %d)... ",ne,nq,nlb);
697 TH1F hene =
TH1F(
"hEne",
"",ne,emin,emax); hene.SetXTitle(
"Energy [GeV]");
698 hene.SetYTitle("Events");
699 TH1F hqua =
TH1F(
"hQua",
"",nq/100.,qmin,qmax); hqua.SetXTitle(
"Quality");
700 hqua.SetYTitle("Events");
701 TH1F hlb =
TH1F(
"hLB",
"",nlb,lbmin,lbmax); hlb.SetXTitle(
"LB");
702 hlb.SetYTitle("Events");
703 TH2F henelb =
TH2F(
"hEnelb",
"",nlb,lbmin,lbmax,ne,emin,emax); henelb.SetXTitle(
"LB");
704 henelb.SetYTitle("Energy [GeV]");
705 TH2D hpulse = TH2D("hPulse","",100,-200,200,400, -10,10); hpulse.SetXTitle("Time [ns]");
706 hpulse.SetYTitle("Value [ADC counts] / ADCmax");
707 TProfile TProfpulse =
TProfile(
"",
"",5, 0, 5,
"s"); TProfpulse.SetXTitle(
"Sample Number");
708 TProfpulse.SetYTitle("Value [ADC counts]");
709 TH2D ht_LB= TH2D("ht_LB","",nlb,lbmin,lbmax,400,-200,200); ht_LB.GetXaxis()->SetTitle("LB");
710 ht_LB.GetYaxis()->SetTitle("Time(maxSample) + ofcTime [ns]");
711 TH1F hADCmax =
TH1F(
"",
"",110,-200,2000); hADCmax.SetXTitle(
"ADCmax [ADC counts]"); hADCmax.SetYTitle(
"Events");
712
713 printf("Done.\n");
714
715
716 LArSamples::Interface* tuple = (LArSamples::Interface*)
Interface::open(inputfile);
718 unsigned int nchannels = tuple->
nChannels();
719
720 fprintf(outputFile,"Onlid \t MeanCellHitBased \t EnergyThresholdBased \t OR \t AND\n");
721
722
723 printf("Begin looping over cells...\n");
724 for(
unsigned int ichan = 0;
ichan < nchannels;
ichan++){
725
726 if(tuple->
historySize(ichan)==0){ nskipped++;
continue; }
727
728
729
731 const LArSamples::CellInfo* cellInfo =
hist->cellInfo();
732 double eta = cellInfo->
eta();
733 double phi = cellInfo->
phi();
734
735
736
738 onlid = cellInfo->
onlid();
739
740
743 }
744
745
746 unsigned int ndigits =
hist->nData();
747 if(ndigits<(unsigned int)threshold){ nskipped++; continue; }
748
749
750
752
753 hname.Form(
"h0x%x_Energy",onlid);
754 h1_e = (
TH1F*)hene.Clone(hname);
755 hname.Form(
"h0x%x_Quality",onlid);
756 h1_q = (
TH1F*)hqua.Clone(hname);
757 hname.Form(
"h0x%x_LB",onlid);
758 h1_lb = (
TH1F*)hlb.Clone(hname);
759 hname.Form(
"h0x%x_Energy_LB",onlid);
760 h2_elb = (
TH2F*)henelb.Clone(hname);
761 hname.Form(
"h0x%x_Pulse",onlid);
762 h2_pulse = (TH2D*)hpulse.Clone(hname);
763 hname.Form(
"TProf0x%x_Pulse",onlid);
764 TProf_pulse = (
TProfile*)TProfpulse.Clone(hname);
765 hname.Form(
"h0x%x_ADCmax",onlid);
766 h1_ADCmax = (
TH1F*)hADCmax.Clone(hname);
767 hname.Form(
"h0x%x_t_LB",onlid);
768 h2_t_LB = (TH2D*)ht_LB.Clone(hname);
769 }
770
771 bool CellInList = false;
772 int LBFlaggedIn = -1;
773
774
775 for(unsigned int idigit = 0; idigit < ndigits; idigit++){
776 const LArSamples::Data*
data =
hist->data(idigit);
778 const LArSamples::EventData* Evdata =
data->eventData();
779 if(!Evdata){ noEvdata++; continue; }
782
783
784
786
787
788 if (isBadLB) continue;
789
790
792 if (ProcessEvent){
793
795 h1_e->Fill(
data->energy()*
GeV);
796 h1_q->Fill(
data->quality());
797 h1_lb->Fill(lumiBlock);
798 h2_elb->Fill(lumiBlock,
data->energy()*
GeV);
799 h1_ADCmax->Fill(
data->adcMax());
801 h2_t_LB->Fill(lumiBlock,time);
802 for(
unsigned int isample=0;isample<
data->nSamples();isample++) {
803 TProf_pulse->Fill(isample,
data->value(isample));
804 h2_pulse->Fill(
data->time(isample)-time,
data->value(isample)/
data->adcMax());
805 }
806 }
807
808
809 if (
data->quality() > qthreshold){
810 qcount = qcount+1;
811 }
812
813
814 EventCount++;
815
816
817 if (
data->energy()*
GeV > ecut) {
818 nEvents_E_gt_ecut++;
819 EventEnergySum+=(
data->energy()*
GeV);
820 }
821
822
823 if(!CellInList){
824 selectionFlag=
CheckCellSelectionCriteria(EventCount,nsigmaHits,MeanHits,rmsHits,nEvents_E_gt_ecut,EventEnergySum,nBadLBs,nlb_corr);
825 if(selectionFlag==2){
827 CellInList = true;
828 }
829 else if(selectionFlag==1){
832 CellInList = true;
833 }
836 CellInList = true;
837 }
838 }
839 else if(selectionFlag==0){
842 CellInList = true;
843 }
846 CellInList = true;
847 }
848 }
849 }
850
851 }
852
853 }
854
855 CellCounter++;
856 if ( CellCounter%1000 == 0 ) {
857 printf("Scanning Cell No. = %d\n",CellCounter);
858 }
859 selectionFlag=
CheckCellSelectionCriteria(EventCount,nsigmaHits,MeanHits,rmsHits,nEvents_E_gt_ecut,EventEnergySum,nBadLBs,nlb_corr);
860
861
862 if (selectionFlag >= 0){
863 bool ListOutput = true;
864 int MeanCellHitsSelection = 0;
865 int EnergyThresholdSelection = 0;
866 int OR_selection = 0;
867 int AND_selection = 0;
868
869 if (selectionFlag == 2){
870 MeanCellHitsSelection = 1;
871 EnergyThresholdSelection = 1;
872 OR_selection = 1;
873 AND_selection = 1;
874 }
875
876
877 else if (selectionFlag == 1){
878 if (
m_Algo == 1) ListOutput =
false;
879 MeanCellHitsSelection = 1;
880 EnergyThresholdSelection = 0;
881 OR_selection = 1;
882 AND_selection = 0;
883 }
884
885 else if (selectionFlag == 0){
886 if (
m_Algo == 0) ListOutput =
false;
887 MeanCellHitsSelection = 0;
888 EnergyThresholdSelection = 1;
889 OR_selection = 1;
890 AND_selection = 0;
891 }
892
893
894 iMeanCellHitsSelection += MeanCellHitsSelection;
895 iEnergyThresholdSelection += EnergyThresholdSelection;
896 iOR_selection += OR_selection;
897 iAND_selection += AND_selection;
898
899
902
903 if (ListOutput){
904 fprintf(outputFile,"0x%x \t %d \t \t \t %d \t \t %d \t %d\n",onlid,MeanCellHitsSelection,EnergyThresholdSelection,OR_selection,AND_selection);
905
906
907
908 nBadCells++;
911 for (
int ilb=1;
ilb<=nlb;
ilb++){
912 if (h1_lb->GetBinContent(ilb) > 0.)
nLB++;
913
914 }
915 fr_LB = (
double)nLB/(
double)(nlb_corr-nBadLBs);
917 meanECell = h1_e->GetMean();
918 n_ensig = EventCount;
919 if (EventCount!=0){
920 fr_q4k = (
double)qcount/(
double)EventCount;
921 }
922 n_ecut = nEvents_E_gt_ecut;
923 }
924
927 for (
int ilb=1;
ilb<=nlb;
ilb++){
928 if (h1_lb->GetBinContent(ilb) > 0.)
nLB++;
929 }
930 fr_LB = (
double)nLB/(
double)(nlb_corr-nBadLBs);
932 meanECell = h1_e->GetMean();
933 n_ensig = EventCount;
934 if (EventCount!=0){
935 fr_q4k = (
double)qcount/(
double)EventCount;
936 }
937 n_ecut = nEvents_E_gt_ecut;
939 Eeta.Fill(
eta,meanECell);
940 CellEnergy.Fill(meanECell);
941 LBfrac.Fill(fr_LB);
942 if (n_ecut>0) {
943 Emean_NbrEvents.Fill(n_ecut,EventEnergySum/n_ecut);
944 Emean_NbrEvents_part[
index]->Fill(n_ecut,EventEnergySum/n_ecut);
945 }
946
947 Qfrac.Fill(fr_q4k);
948 Pulsemaps[
index]->Add(Pulsemaps[index].
get(),h2_pulse);
949 E_LBmaps[
index]->Add(E_LBmaps[index].
get(),h2_elb);
950 t_LBmaps[
index]->Add(t_LBmaps[index].
get(),h2_t_LB);
951 CellsFlagged_LB_part[
index]->Fill(LBFlaggedIn);
952 CellsFlagged_LB.Fill(LBFlaggedIn);
953 }
954
956
957
958
960 if (nEvents_E_gt_ecut>0) { EventEnergyMean = EventEnergySum/nEvents_E_gt_ecut; }
961 fprintf(pFile,
"0x%x \t %7s \t %d \t %d \t %d \t %d \t %d \t %f \t %4.0f \t %4.3f \t %d \t %d\n",onlid,
m_LarIdTranslator->GetPartitonLayerName(index),cellInfo->
feedThrough(),cellInfo->
slot(),cellInfo->
channel(),EventCount,nEvents_E_gt_ecut,EventEnergyMean,nbrLB,fr_q4k,LBFlaggedIn,selectionFlag);
962
963 }
964
965 }
966 }
967
968 qcount = 0.;
969 EventCount = 0.;
970 nEvents_E_gt_ecut = 0.;
971 EventEnergySum = 0.;
972 EventEnergyMean = 0.;
973 }
974 printf("Cells selected: MeanCellHitsCut \t EnergyThreshodCut \t Either List \t Both Lists\n");
975 printf("\t \t %d \t \t \t \t %d \t \t %d \t %d\n",iMeanCellHitsSelection,iEnergyThresholdSelection,iOR_selection,iAND_selection);
977 fclose (pFile);
978 }
979 fclose (outputFile);
980
982 printf("Skipped %d cells. Cells scanned = %d\n",nskipped,nbad);
983 printf("Cells put in Bad Cell List = %d\n",nBadCells);
984 if (noEvdata>0) printf("No Evdata pointer: %d\n",noEvdata);
985
988 for(
int i=0;
i<npl;
i++){
989 if(Cellmaps[i]->
GetEntries()>0) Cellmaps[
i]->Write();
990 if(E_LBmaps[i]->
GetEntries()>0) E_LBmaps[
i]->Write();
991 if(t_LBmaps[i]->
GetEntries()>0) t_LBmaps[
i]->Write();
992 if(Pulsemaps[i]->
GetEntries()>0) Pulsemaps[
i]->Write();
993 if(CellsFlagged_LB_part[i]->
GetEntries()>0) CellsFlagged_LB_part[
i]->Write();
994 if( Emean_NbrEvents_part[i]->
GetEntries()>0) Emean_NbrEvents_part[
i]->Write();
995 }
996 CellsFlagged_LB.Write();
997 Eeta.Write();
998 CellEnergy.Write();
999 Qfrac.Write();
1000 Emean_NbrEvents.Write();
1001 LBfrac.Write();
1002 }
1004 delete h1_lb;
1005 delete h1_e;
1006 delete h1_q;
1007 delete h2_elb;
1008 delete h2_pulse;
1009 delete h2_t_LB;
1010 delete TProf_pulse;
1011 delete h1_ADCmax;
1012 return;
1013
1014}
Scalar eta() const
pseudorapidity method
Scalar phi() const
phi method
std::vector< std::unique_ptr< T > > THVec
TGraphErrors * GetEntries(TH2F *histo)
short feedThrough() const
bool CheckPartition(int index) const
void GetLimits_EqLB(const char *inputfile, int &lbmin, int &lbmax, int &emin, int &emax, int &qmin, int &qmax, int &runNumber, const std::vector< int, std::allocator< int > > &BadLBList)
std::vector< int, std::allocator< int > > GetBadLBList(const char *inputfile, int lbmin, int lbmax, double nsigma, int nlb, const std::vector< int, std::allocator< int > > &DQLBList)
int CheckCellSelectionCriteria(int EventCount, int nsigmaHits, double MeanHits, double rmsHits, int nEvents_E_gt_ecut, double EventEnergySum, int nBadLBs, int nlb) const
void GetMeanCellHits(const char *inputfile, int nlb, int lbmin, int lbmax, int nsigma, const std::vector< int, std::allocator< int > > &BadLBList, double &MeanHits, double &rmsHits, int &nlb_corr)
std::vector< int, std::allocator< int > > ReadBadLBList(const TString &LBfile)
T * get(TKey *tobj)
get a TObject* from a TKey* (why can't a TObject be a TKey?)
time(flags, cells_name, *args, **kw)
TH2F(name, title, nxbins, bins_par2, bins_par3, bins_par4, bins_par5=None, bins_par6=None, path='', **kwargs)
TProfile(*args, **kwargs)
TH1F(name, title, nxbins, bins_par2, bins_par3=None, path='', **kwargs)