Info in <TCanvas::MakeDefCanvas>: created default TCanvas with name c1

When I run my program, it automatically generates an extra canvas. How can I solve this problem?
I want to remove the extra canvas.Has anyone encountered a similar problem?

It is hard to tell without seeing your macro. Can you post it here ?

Thanks for your reply.This is my script.


void muonLife(TString filename = "data_C1_0327_wave.root"){

TFile* file = TFile::Open(filename);
if(file){
	cout <<"*************************************"<<endl;
	cout <<" File have been opend successfully!!"<<endl;
        cout <<"*************************************"<<endl;
}
TTree* tree = (TTree*)file->Get("tree");
if(!tree)cout<<" Cannot find the tree successfully!!"<<endl;

int nEntries = tree->GetEntriesFast();
cout << " Entries number is  "<<nEntries <<endl;
//--------------------------------------------------------
Bool_t cfdft;                    TBranch* b_cfdft;
UShort_t ltra = 0;                TBranch* b_ltra; 
UShort_t wave[5000] = {0};        TBranch* b_data;
UShort_t evte = 0;                TBranch* b_evte; 
UInt_t qs[8];                     TBranch* b_qs;
Bool_t pileup;                   TBranch* b_pileup; 


tree->SetBranchAddress("ltra",&ltra,&b_ltra);
tree->SetBranchAddress("cfdft",&cfdft,&b_cfdft);
tree->SetBranchAddress("data",&wave,&b_data);
tree->SetBranchAddress("evte",&evte,&b_evte);
tree->SetBranchAddress("qs",qs,&b_qs);
tree->SetBranchAddress("pileup",&pileup,&b_pileup);
//---------------------------------------------------------
Int_t npeaks = 30;
Int_t TwoPeakNum = 0;
Int_t sumNum,sumN;

//---------------------------------------------------------
 TGraph* gr = new TGraph(); TGraph* gr_copy = new TGraph();
 TGraph* singleWave = new TGraph();
 TH1F* waveHist = new TH1F("single wave","single wave",5000,0,5000);
 TH1F* histClone;
 TH1F* base = new TH1F("baseline","baseline",100,1600,1700);//baseline


 TCanvas* myc = new TCanvas("dynamic wave anayzed","",700,500);
//-------------------------------------------------------------------------
  for(int entry = 0;entry < nEntries;entry++){
         
         waveHist->Reset(); 
     
     tree->GetEntry(entry);   
     
     sumNum = sumN = 0; 
          
  if(pileup==1)continue;
    if(evte<=4000)continue;
  if(entry % 10000 ==0) cout <<entry << "  have been analyzed!!"<<endl;
  
       for(Int_t i=0; i<ltra; i++){
             gr->SetPoint(i,i,wave[i]);
             waveHist->SetBinContent(i+1,wave[i]);
         
         if(i<450){ 
             sumNum += wave[i];
             sumN++;
         }          
     } 
 double avg = sumNum/sumN;
  
  if(avg>1643+3*1.5 || avg>1643-3*1.5)continue; 
  
  if(entry == 52334){
  singleWave = (TGraph*)gr->Clone();
  histClone =  (TH1F*)waveHist->Clone();
  }
 
    TSpectrum *s = new TSpectrum(2*npeaks);
   Int_t nfound = s->Search(waveHist,2,"",0.01); 
                   //-------------------------------------// 
                   //            find peaks               // 
                   //-------------------------------------//
 if(nfound == 2) {
    
         for(int i=0;i<ltra;i++){
                 if(wave[i]<1700)wave[i]=0;
                    graph->SetPoint(i,i,wave[i]);                       
             }

  TwoPeakNum++;  
 } 
 #endif 
 
  }

TCanvas* canvas1 = new TCanvas();
canvas1->Divide(1,2);
canvas1->cd(1);
    singleWave->SetMarkerColor(kBlue);
    singleWave->SetMarkerStyle(8);
    singleWave->SetMarkerSize(0.5);
    singleWave->SetTitle("signle;time(ns);amplitude");
    singleWave->GetXaxis()->CenterTitle();
    singleWave->GetYaxis()->CenterTitle();
singleWave->Draw("AP");
canvas1->cd(2);
histClone->Draw("hist");
}

Two problems:

  1. I do not have you data file to run the macro
  2. graph at line 83 is undefined.

Sorry, responding so late.Here is my code and file. There is always one extra window appearing.
an.cpp (3.1 KB)

data.root (363.3 KB)

void an(TString filename = "data.root"){
   TFile* file = TFile::Open(filename);
   if (file) {
      cout <<"*************************************"<<endl;
      cout <<" File have been opend successfully!!"<<endl;
      cout <<"*************************************"<<endl;
   }
   TTree* tree = (TTree*)file->Get("ntuple");
   if (!tree) cout<<" Cannot find the tree successfully!!"<<endl;

   int nEntries = tree->GetEntriesFast();
   cout << " Entries number is  "<< nEntries <<endl;
   //--------------------------------------------------------
   Bool_t cfdft;                    TBranch* t_cfdft;
   Short_t ltra = 0;                TBranch* t_ltra;
   Short_t wave[5000] = {0};        TBranch* t_data;
   Short_t evte = 0;                TBranch* t_evte;
   Bool_t pileup;                   TBranch* t_pileup;

   tree->SetBranchAddress("ltra",&ltra,&t_ltra);
   tree->SetBranchAddress("cfdft",&cfdft,&t_cfdft);
   tree->SetBranchAddress("data",&wave,&t_data);
   tree->SetBranchAddress("evte",&evte,&t_evte);
   tree->SetBranchAddress("pileup",&pileup,&t_pileup);

   //---------------------------------------------------------
   int npeaks = 30;
   int TwoPeakNum = 0;
   int sumNum,sumN;

   //---------------------------------------------------------
   auto gr = new TGraph();
   auto gr_copy = new TGraph();
   auto singleWave = new TGraph();
   auto waveHist = new TH1F("single wave","single wave",5000,0,5000);
   auto base = new TH1F("baseline","baseline",100,1600,1700); //baseline

   for(int entry = 0; entry < nEntries; entry++){
      waveHist->Reset();
      tree->GetEntry(entry);
      sumNum = sumN = 0;

      if(pileup==1)continue;
      if(evte<=4000)continue;
      if(entry % 100 ==0) cout <<entry << "  have been analyzed!!"<<endl;

      for (int i=0; i<ltra; i++) {
         gr->SetPoint(i,i,wave[i]);
         waveHist->SetBinContent(i+1,wave[i]);
         if(entry == 6)singleWave->SetPoint(i,i,wave[i]);
      }

      auto *s = new TSpectrum(2*npeaks);
      int nfound = s->Search(waveHist,2,"goff",0.01);
      //-------------------------------------//
      //            find peaks               //
      //-------------------------------------//
      auto graph = new TGraph();
      if(nfound == 2) {
         for(int i=0;i<ltra;i++){
            if(wave[i]<1700)wave[i]=0;
            graph->SetPoint(i,i,wave[i]);
         }
         TwoPeakNum++;
      }
   }

   auto canvas1 = new TCanvas("canvas","canvas",700,500);
   singleWave->SetMarkerColor(kBlue);
   singleWave->SetMarkerStyle(8);
   singleWave->SetMarkerSize(0.5);
   singleWave->SetTitle("single;time(ns);amplitude");
   singleWave->GetXaxis()->CenterTitle();
   singleWave->GetYaxis()->CenterTitle();
   singleWave->Draw("AP");
}

Thank you! :grinning:

1 Like