Example #1
0
void ExpManager::DrawMap(TString NameTitle, double xmin, double xmax, double ymin, double ymax, double zmin, double zmax) {

    TMultiGraph *mg = new TMultiGraph();
    
    //Draw a cross
    TGraphErrors *frame = new TGraphErrors(); //= new TGraph2DErrors(np, x_array, y_array, bz_array, ex, ey, ez);
    frame->SetPoint(0,+0,+100);
    frame->SetPoint(1,+0,-100);
    frame->SetPoint(2,0,0); 
    frame->SetPoint(3,-100,0); 
    frame->SetPoint(4,+100,0);
    frame->SetPoint(5,+0,+0); 
    frame->SetPoint(6,+0,-100);
    frame->SetMarkerColor(kWhite);
    frame->SetDrawOption("ap");
    
    //Draw the map 
    TGraphErrors *fGraph = new TGraphErrors(); //= new TGraph2DErrors(np, x_array, y_array, bz_array, ex, ey, ez);
    fGraph->SetMarkerSize(1.2);
    fGraph->SetMarkerStyle(20);
    fGraph->SetMarkerColor(kBlue);
    fGraph->SetLineColor(kBlue);
    fGraph->SetLineWidth(1);
    //fGraph->SetDrawOption("ap");
    
    int graph_counter = 0 ; 
   for (unsigned i=0; i< fExpY.size(); i++)   {
        if( (fExpX.at(i) >= xmin && fExpX.at(i) <= xmax) && (fExpY.at(i) >= ymin && fExpY.at(i) <= ymax) && (fExpZ.at(i) >= zmin && fExpZ.at(i) <= zmax) ) {
            fGraph->SetPoint(graph_counter,fExpX.at(i),fExpY.at(i));    
            fGraph->SetPointError(graph_counter,fExpXErr.at(i),fExpYErr.at(i)); 
            graph_counter++;
        } 
   }

    //fGraph->Write();
    mg->SetTitle(NameTitle+Form(" Map : %.2f < X < %.2f mm    %.2f < Y < %.2f mm    %.2f < Z < %.2f mm;X (mm);Y (mm)",xmin,xmax,ymin,ymax,zmin,zmax));
    mg->SetName(NameTitle+Form("_Map__X_%.2f_%.2fmm__Y_%.2f_%.2fmm__Z_%.2f_%.2fmm",xmin,xmax,ymin,ymax,zmin,zmax));
    mg->Add(frame); 
    mg->Add(fGraph);
    mg->Write();
}
Example #2
0
vector<float> getSlopeRatio(char slice, const float MAX1, const float MAX2, const float STEP, float xmin, float xmax, float ymin, float ymax){
  
//   TString fitFilename;
//   
//   fitFilename.Form("goodFits/Fit_%c_%g_%g_%g_%g.root", slice, xmin, xmax, ymin, ymax);
//   TFile *goodFits = new TFile(fitFilename.Data(), "RECREATE");
//   
//   fitFilename.Form("badFits/Fit_%c_%g_%g_%g_%g.root", slice, xmin, xmax, ymin, ymax);
//   TFile *badFits = new TFile(fitFilename.Data(), "RECREATE");
//   
//   fitFilename.Form("allFits/Fit_%c_%g_%g_%g_%g.root", slice, xmin, xmax, ymin, ymax);
//   TFile *allFits = new TFile(fitFilename.Data(), "RECREATE");
  
  float meanAmp1[5] = {0}, meanAmp2[5] = {0}, eX1[5] = {0}, eX2[5] = {0}, eY1[5] = {0}, eY2[5] = {0};
  float energy[5] = {20, 50, 100, 150, 200}; //Different beam energies
  int runs[5] = {5651, 5654, 5655, 5659, 5661}; //Runs used at different gains
  
  vector<float> point(2); //Vector to be returned

  point.at(0) = 0;
  point.at(1) = 0;
  
  TFile *file = new TFile("myfile.root", "RECREATE");
  
  TH1F *hist1 = new TH1F("hist1", "hist1", 10000, -3000, 3000); //Histograms for APDs 1 and 2
  TH1F *hist2 = new TH1F("hist2", "hist2", 10000, -3000, 3000);
  
  TFile *f;
  TTree *h4;
  TF1 *g1, *g2, *f1, *f2;
  TString filename, plot1, plot2, cut, histname, newName;
  
  newName.Form("Resolution%c/%g_%g_%g/%c_%g_%g_%g_%g.root", slice, MAX1, MAX2, STEP, slice, xmin, xmax, ymin, ymax);
  
  TFile *newFile = new TFile(newName.Data(), "RECREATE"); //File that stores the two histograms for each slice
  
  for(int i = 0; i < 5; i++){
    
    filename.Form("./ntuples_v1/analysis_%d.root", runs[i]);
    f = TFile::Open(filename.Data());
    h4 = (TTree*) f->Get("h4");
    
    file->cd(); //Need this so the histograms have a place to "live"
    
    float xOffset = 2.5; //Difference between X[0] and X[1]. Determined by Draw("X[0]:X[1]"). There is no Y offset
    
    //Plot the fit_ampl of all the runs
    plot1.Form("fit_ampl[XTAL_C0_APD1] >> hist1");
    plot2.Form("fit_ampl[XTAL_C0_APD2] >> hist2"); //tag
    
    //Cut to look only within the specified slice 
    cut.Form("(X[0] > %f) && (X[1] > %f) && (X[0] < %f) && (X[1] < %f) && (Y[0] > %f) && (Y[1] > %f) && (Y[0] < %f) && (Y[1] < %f)", xmin, xmin + xOffset, xmax, xmax + xOffset, ymin, ymin, ymax, ymax);
    
    h4->Draw(plot1.Data(), cut.Data());
    h4->Draw(plot2.Data(), cut.Data());
    
    allFits->cd();
    hist1->Write("hist1");
    hist2->Write("hist2");
    
    float cutoff = hist1->GetMean() - 3*hist1->GetRMS(); //Designate a cutoff value for fit_ampl based on the previous plot
    cut.Form("(X[0] > %f) && (X[1] > %f) && (X[0] < %f) && (X[1] < %f) && (Y[0] > %f) && (Y[1] > %f) && (Y[0] < %f) && (Y[1] < %f) && (fit_ampl[XTAL_C0_APD1] > %f)", xmin, xmin + xOffset, xmax, xmax + xOffset, ymin, ymin, ymax, ymax, cutoff);
    long int entries1 = h4->Draw(plot1.Data(), cut.Data()); //Replot with fit_ampl restriction
    
    //Fit the plot with a Gaussian
    g1 = new TF1("g1", "gaus", hist1->GetMean() - 2*hist1->GetRMS(), hist1->GetMean() + 2*hist1->GetRMS());
    g1->SetParameters(1, hist1->GetMean(), hist1->GetRMS());
    g1->SetParLimits(1, hist1->GetMean() - hist1->GetRMS()/2, hist1->GetMean() + hist1->GetRMS()/2);
    g1->SetParLimits(2, hist1->GetRMS()/10, hist1->GetRMS()/2);
    hist1->Fit(g1, "RQME");
    
    //Fit the plot with a crystalball based on the Gaussian
    f1 = new TF1("f1","crystalball", hist1->GetMean() - 2*hist1->GetRMS(), hist1->GetMean() + 2*hist1->GetRMS());
    f1->SetParameters(1, g1->GetParameter(1), g1->GetParameter(2)/3, 1, .5);
    f1->SetParLimits(1, g1->GetParameter(1) - g1->GetParameter(2), g1->GetParameter(1) + g1->GetParameter(2));
    f1->SetParLimits(4, 0.1, 4);
    f1->SetParLimits(3, 0.1, 10);
    f1->SetParLimits(2, (g1->GetParameter(2))/10, g1->GetParameter(2)/2);
    hist1->Fit(f1, "RQME");
    
    //Attempt to refit if the first failed for some reason, such as a bad Gaussian fit
    if((f1->GetChisquare()/entries1 - 0.2132) > 1*0.03559){ //Using values from chi2 distribution on 6 6 2 run
      
      f1->SetParameter(1, hist1->GetMean());
      f1->SetParameter(2, hist1->GetRMS()/3);
      f1->SetParLimits(1, hist1->GetMean() - hist1->GetRMS()/2, hist1->GetMean() + hist1->GetRMS()/2);
      f1->SetParLimits(2, hist1->GetRMS()/10, hist1->GetRMS()/2);
      hist1->Fit(f1, "RQME");
      
      if((f1->GetChisquare()/entries1 - 0.2132) > 1*0.03559){
	badFits->cd();
	histname.Form("APD1_%d_%g_%g_%g_%g", i, xmin, xmax, ymin, ymax); //tag
	hist1->Write(histname.Data());
      }
      else{
      goodFits->cd();
      histname.Form("APD1_%d_%g_%g_%g_%g", i, xmin, xmax, ymin, ymax); //tag
      hist1->Write(histname.Data());
      }
    }
    
    allFits->cd();
    histname.Form("APD1_%d_%g_%g_%g_%g", i, xmin, xmax, ymin, ymax); //tag
    hist1->Write(histname.Data());
    
    chi2->Fill(f1->GetChisquare()/entries1);
    
    //Save histogram
    newFile->cd();
    histname.Form("APD1_%d", i); //tag
    hist1->Write(histname.Data());
    file->cd();
    
    //Repeat of everything with APD2
    cutoff = hist2->GetMean() - 3*hist2->GetRMS();
    cut.Form("(X[0] > %f) && (X[1] > %f) && (X[0] < %f) && (X[1] < %f) && (Y[0] > %f) && (Y[1] > %f) && (Y[0] < %f) && (Y[1] < %f) && (fit_ampl[XTAL_C0_APD2] > %f)", xmin, xmin + xOffset, xmax, xmax + xOffset, ymin, ymin, ymax, ymax, cutoff);
    long int entries2 = h4->Draw(plot2.Data(), cut.Data());
    
    g2 = new TF1("g2", "gaus", hist2->GetMean() - 2*hist2->GetRMS(), hist2->GetMean() + 2*hist2->GetRMS());
    g2->SetParameters(1, hist2->GetMean(), hist2->GetRMS());
    g2->SetParLimits(1, hist2->GetMean() - hist2->GetRMS()/2, hist2->GetMean() + hist2->GetRMS()/2);
    g2->SetParLimits(2, hist2->GetRMS()/10, hist2->GetRMS()/2);
    hist2->Fit(g2, "RQME");
    
    f2 = new TF1("f2","crystalball", hist2->GetMean() - 2*hist2->GetRMS(), hist2->GetMean() + 2*hist2->GetRMS());
    f2->SetParameters(1, g2->GetParameter(1), g2->GetParameter(2)/3, 1, .5);
    f2->SetParLimits(1, g2->GetParameter(1) - g2->GetParameter(2), g2->GetParameter(1) + g2->GetParameter(2));
    f2->SetParLimits(4, 0.1, 4);
    f2->SetParLimits(3, 0.1, 10);
    f2->SetParLimits(2, (g2->GetParameter(2))/10, g2->GetParameter(2)/2);
    hist2->Fit(f2, "RQME");
    
    if((f2->GetChisquare()/entries2 - 0.2132) > 1*0.03559){ //Using values from chi2 distribution on 6 6 2 run
      
      f2->SetParameter(1, hist2->GetMean());
      f2->SetParameter(2, hist2->GetRMS()/3);
      f2->SetParLimits(1, hist2->GetMean() - hist2->GetRMS()/2, hist2->GetMean() + hist2->GetRMS()/2);
      f2->SetParLimits(2, hist2->GetRMS()/10, hist2->GetRMS()/2);
      hist2->Fit(f2, "RQME");
      
      if((f2->GetChisquare()/entries2 - 0.2132) > 1*0.03559){
	badFits->cd();
	histname.Form("APD2_%d_%g_%g_%g_%g", i, xmin, xmax, ymin, ymax); //tag
	hist2->Write(histname.Data());
      }
      else{
      goodFits->cd();
      histname.Form("APD2_%d_%g_%g_%g_%g", i, xmin, xmax, ymin, ymax); //tag
      hist2->Write(histname.Data());
      }
    }
    
    allFits->cd();
    histname.Form("APD2_%d_%g_%g_%g_%g", i, xmin, xmax, ymin, ymax); //tag
    hist1->Write(histname.Data());
    
    chi2->Fill(f2->GetChisquare()/entries2);
      
    newFile->cd();
    histname.Form("APD2_%d", i); //tag
    hist2->Write(histname.Data());
    
    //Use the fit parameters as values to fill the arrays
    meanAmp1[i] = f1->GetParameter(1);
    eY1[i] = f1->GetParError(1);
    hist1->Reset();
    
    meanAmp2[i] = f2->GetParameter(1);
    eY2[i] = f2->GetParError(1);
    hist2->Reset();
  }

  //Make graphs from arrays
  TGraphErrors *gr1 = new TGraphErrors(5, energy, meanAmp1, eX1, eY1);
  TGraphErrors *gr2 = new TGraphErrors(5, energy, meanAmp2, eX2, eY2);
  
  //Making the graphs nice
  gr1->GetXaxis()->SetTitle("Beam Energy");
  gr1->GetYaxis()->SetTitle("Mean Fit Amplitude");
  
  gr1->SetMarkerStyle(21);
  gr2->SetMarkerStyle(20);
  gr2->SetMarkerColor(kBlue);
  gr2->SetLineColor(kBlue);
  
  //Linear fit of both graphs
  TFitResultPtr r1 = gr1->Fit("pol1", "S MEQ");
  TFitResultPtr r2 = gr2->Fit("pol1", "S MEQ");
  
  //Getting the slope and error of each fit
  float slope1 = r1->Value(1);
  float slope2 = r2->Value(1);
  
  float err1 = r1->ParError(1);
  float err2 = r2->ParError(1);
  
  float chi1 = r1->Chi2();
  float chi2 = r2->Chi2();
  
  //cout << chi1 << " " << chi2 << endl;
  
  //Plotting both graphs together
  TMultiGraph *mg = new TMultiGraph("mg", "Energy Resolution Calibration");

  mg->Add(gr1);
  mg->Add(gr2);
  
  mg->Draw("apl");
  mg->GetXaxis()->SetTitle("Beam Energy (GeV)");
  mg->GetYaxis()->SetTitle("Mean Fit Amplitude");
  
  TLegend* leg = new TLegend(0.1,0.7,0.5,0.9);
  leg->AddEntry(gr1,"APD1","lp");
  leg->AddEntry(gr2,"APD2","lp");
  leg->Draw();
  
  mg->Write();
  
//   point.at(0)= slope2+slope1;
//   point.at(1) = sqrt((err1*err1) + (err2*err2));

  //Filling the vector with the relevant values
  point.at(0)= slope2/slope1;
  point.at(1) = point.at(0)*sqrt((err1*err1)/(slope1*slope1) + (err2*err2)/(slope2*slope2)); //Error propagation
  
  return point;
}
Example #3
0
int main(int argc, char *argv[])
{
  setenv("TZ","UTC",1);
  tzset();
  gROOT->SetStyle("Plain");


  printf("[email protected] (build %s %s)\n", __DATE__, __TIME__);  

  
  char *RunFileName="$HOME/analyisis/default_00.root";
  char *OutputFileName="$HOME/analysis/RunFlag_out.root";
  bool debug = false;

  
  
  int c;
  opterr = 0;

  while ((c = getopt (argc, argv, "hf:i:")) != -1) //: after var ->Waits for input
    switch (c)
      {
      case 'f':
	OutputFileName = optarg ;
	break;
      case 'i':
	RunFileName = optarg ;
	break;
      case '?':
        if (isprint (optopt))
          fprintf (stderr, "Unknown option `-%c'.\nTry '-h'\n", optopt);
        else
          fprintf (stderr,
                   "Unknown option character `\\x%x'.\nTry '-h'\n",
                   optopt);
        return 1;
      case 'h':
        cout << endl;
	cout << " Overview: optimal window size\n";
	cout << "===============================\n\n";
	cout << " -h	HELP -- you already tried :-)\n";
	cout << " -f	output file name (default: " << OutputFileName << ")\n";
	cout << " -i	input file name in STAR format (default: " << RunFileName << ")\n";
	cout << "\n\n\nmailto: [email protected]\n";
	exit(1);
        break;      
      default:
	abort ();
      }

  

  TChain *Tevent = new TChain("Tevent");
  CreateTChain(Tevent, RunFileName);
  
  TChain *Theader = new TChain("Theader");
  Theader->Add(RunFileName);

  TChain *Tchannel_profile = new TChain("Tchannel_profile");
  Tchannel_profile->Add(RunFileName);

  struct event event;
  struct header header;
  struct channel_profiles channel_profiles;
  
  TFile *output = new TFile (OutputFileName,"Recreate","RunFlagOut");
  
  CreateTevent(Tevent, &event);
  CreateTheader(Theader, &header);
  CreateTchannel_profiles(Tchannel_profile, &channel_profiles);

  Tevent->GetEntry(0);
  Theader->GetEntry(0);
  

  //no of channels per event in this run
  int NoChannels = Tchannel_profile->GetEntries();
  if(debug) cout << "NoChannels = " << NoChannels << endl;
  
///////////////////////////////   
  int START = 0;
 
  unsigned int event_id_start = 1553544;
   for(int w=0; w<Tevent->GetEntries(); w++){
     Tevent->GetEntry(w);
     START = w;
     if(event.event_id == event_id_start) break;
     if(event.event_id > event_id_start) break;
   }
   Tevent->GetEntry(0);
   if(debug) cout << START << " START" << endl;

 int STOP =  START+NoChannels*1; //Tevent->GetEntries();
///////////////////////////////
  
  if(STOP> Tevent->GetEntries()){
   cerr << "entry doesn't exist" << endl;
   STOP = Tevent->GetEntries();
   cerr << "changed end number." <<  endl;
  // exit(1);
  }
  
  if(debug) cout << "STOP " << STOP << endl;
  
  /***************************************/
  int stepsize = int(1*1024);
  int startbin = 8*1024;



  /***************************************/
  int tmp_start=0;
  int tmp_stop=0;
  
  //calculate how many window sizes to prozess
  Tevent->GetEntry(0);
  int kwindow_size = event.window_size / stepsize;
    
  //ZeroPadding
  int NoZeros = 7;
  int New_window_size = (NoZeros+1)*event.window_size;
  float *TraceZP[(STOP-START)*(kwindow_size+1)];
  for(int i=0; i<(STOP-START)*(kwindow_size+1); i++) TraceZP[i] = new float [New_window_size];
  float TimeFFTZP[New_window_size];
  for(int i=0; i<New_window_size; i++) TimeFFTZP[i]=(float)i/(NoZeros+1);

  //spectra
  float spectrum[(STOP-START)*(kwindow_size+1)][event.window_size/2];
  float freq[event.window_size/2];
  for(unsigned int i=0; i<event.window_size/2; i++)
    freq[i]=40.0+40.0/(event.window_size/2)*2+(40.0/(event.window_size/2.0))*(i);
  float phase[event.window_size/2], rawfft[2*event.window_size];
  
  //graph data
  float d_ws[kwindow_size+1];
  float d_mean[NoChannels][kwindow_size+1];
  float d_rms[NoChannels][kwindow_size+1];
  float d_RMS_with[NoChannels][kwindow_size+1];
  float d_RMS_without[NoChannels][kwindow_size+1];
  float tmp1_mean, tmp2_mean, tmp1_rms, tmp2_rms;
  unsigned int min_channel_id=999;
  
  //stopwatch
  TStopwatch timer[NoChannels][kwindow_size+1];
  float real_time[NoChannels][kwindow_size+1];
  float  cpu_time[NoChannels][kwindow_size+1];

 
  //define output TGraphs and canvases
  TGraph **gWin = new TGraph *[(STOP-START)*(kwindow_size+1)];
  TCanvas **canWin = new TCanvas *[(STOP-START)*(kwindow_size+1)];
  TGraph **gSpec = new TGraph *[(STOP-START)*(kwindow_size+1)];
  TCanvas **canSpec = new TCanvas *[(STOP-START)*(kwindow_size+1)];
  TGraph **gd_mean = new TGraph *[NoChannels];
  TGraph **gd_rms = new TGraph *[NoChannels];
  TGraph **gd_RMS_with = new TGraph *[NoChannels];
  TGraph **gd_RMS_without = new TGraph *[NoChannels];  
  TGraph **gtime_cpu = new TGraph *[NoChannels];
  TGraph **gtime_real = new TGraph *[NoChannels];
  
  float tmpTrace[event.window_size];
  float tmpTraceSpec[event.window_size];
  char name[1024];
  char title[1024];
  int ChId = 0;
  int window_size = 0;
  float tmpTraceStat[New_window_size];
  
  
  for(int i=0; i<Tchannel_profile->GetEntries(); i++){
     Tchannel_profile->GetEntry(i);
     if(channel_profiles.ch_id < min_channel_id)
       min_channel_id = channel_profiles.ch_id;
  } 
  cout << "min channel id = " << min_channel_id << endl;
   
  for(int i=0; i<(kwindow_size+1); i++){
   printf("\nwithout RFISuppression\t\t\t\t\twith RFISuppression\n");
   printf("Channel\t\twindow_size\tmean\trms\t\tChannel\t\twindow_size\tmean\trms\n");
   if(i==1) window_size = event.window_size+stepsize;
   if(i!=0){
     window_size -= stepsize;
     New_window_size = (NoZeros+1)*window_size;
     for(int c=0; c<New_window_size; c++) TimeFFTZP[c]=(float)c/(NoZeros+1);
     for(int c=0; c<window_size/2; c++)
       freq[c]=40.0+40.0/(window_size/2)*2+(40.0/(window_size/2.0))*(c);

   }
   else  window_size = event.window_size;
   
   d_ws[i] = window_size;
   
   if(debug){
     cout << "kwindow_size = " << kwindow_size << " - i = " << i << endl;
     cout << "window_size = " << window_size << endl;
     cout << "New_window_size = " << New_window_size << endl;
   }
   
   for(int a=START;a<STOP; a++){
    Tevent->GetEntry(a);
    
    ChId=0;
    Tchannel_profile->GetEntry(ChId);
    while( event.channel_profile_id != channel_profiles.ch_id ){
      ChId++;
      if(Tchannel_profile->GetEntry(ChId)==0){
        cerr << "error - Tchannel_profile->GetEntry()" << endl;
        exit(0);
      }
    }
    if(i!=0)sprintf(title,"D%i_%s_%s, %s, run=%i, event_id=%i, ws=%i",header.DAQ_id, channel_profiles.antenna_pos, 
   		channel_profiles.polarisation, event.timestamp, header.run_id, event.event_id, window_size);
    else sprintf(title,"D%i_%s_%s, %s, run=%i, event_id=%i, ws=%i without RFI suppression",header.DAQ_id, channel_profiles.antenna_pos, 
   		channel_profiles.polarisation, event.timestamp, header.run_id, event.event_id, window_size);

    if(debug) cout << title <<  " - " << a-START+(i)*NoChannels << " " << a << "  " << i << endl;
    
    //define readout range
    tmp_start = startbin-window_size/2;
    if( (tmp_start)<0 ) tmp_start = 0;
    tmp_stop = tmp_start + window_size;
    if((unsigned int) tmp_stop > event.window_size) tmp_stop = event.window_size;
    
    for(int j=tmp_start; j<tmp_stop; j++) tmpTrace[j-tmp_start] = (event.trace[j] & 0x0fff);
    
    
    if(debug) cout << "tmp: start:stop  = " << tmp_start << ":" << tmp_stop << " - ws=" << tmp_stop-tmp_start << endl; 
    
    for(int v=0; v<2; v++){
      if(v==1)RFISuppressionMedian(window_size, tmpTrace, tmpTrace);
      ZeroPaddingFFT(window_size, tmpTrace, NoZeros, TraceZP[a-START+(i)*NoChannels]);
    
      for(int w=0; w<window_size; w++) tmpTraceStat[w] = Abs(TraceZP[a-START+(i)*NoChannels][w]);
      if(v==0){ tmp1_mean = Mean(window_size,tmpTraceStat); tmp1_rms = RMS(window_size,tmpTraceStat);}
      if(v==1){ tmp2_mean = Mean(window_size,tmpTraceStat); tmp2_rms = RMS(window_size,tmpTraceStat);}
      printf("%s:%s\t%d\t\t%.2f\t%.2f",channel_profiles.antenna_pos, channel_profiles.polarisation, window_size,
       Mean(window_size,tmpTraceStat), RMS(window_size,tmpTraceStat));
      if(v==0) cout << "\t\t";
    }
    cout << endl;
    
    d_mean[event.channel_profile_id-min_channel_id][i] = (tmp1_mean-tmp2_mean)/tmp1_mean;
     d_rms[event.channel_profile_id-min_channel_id][i] = (tmp1_rms-tmp2_rms)/tmp1_rms;
        d_RMS_with[event.channel_profile_id-min_channel_id][i] = tmp2_rms;
     d_RMS_without[event.channel_profile_id-min_channel_id][i] = tmp1_rms;

    //be sure to have the original data
    for(int j=tmp_start; j<tmp_stop; j++) tmpTrace[j-tmp_start] = (event.trace[j] & 0x0fff);
    
    timer[event.channel_profile_id-min_channel_id][i].Start();

    if(i!=0)RFISuppressionMedian(window_size, tmpTrace, tmpTrace);
    for(int j=0; j<window_size; j++) tmpTraceSpec[j] = tmpTrace[j];    
    TraceFFT(window_size , tmpTraceSpec , spectrum[a-START+(i)*NoChannels] , phase, rawfft);
    ZeroPaddingFFT(window_size, tmpTrace, NoZeros, TraceZP[a-START+(i)*NoChannels]);

    timer[event.channel_profile_id-min_channel_id][i].Stop();
    
    sprintf(name,"can%03i_ws%i",a-START+(i)*NoChannels,window_size);
    gWin[a-START+(i)*NoChannels] = new TGraph(New_window_size, TimeFFTZP, TraceZP[a-START+(i)*NoChannels]);
    gWin[a-START+(i)*NoChannels]->SetTitle(title);
    gWin[a-START+(i)*NoChannels]->GetXaxis()->SetTitle(" time #upoint 12.5 / ns");
    gWin[a-START+(i)*NoChannels]->GetYaxis()->SetTitle(" ADC value ");
    canWin[a-START+(i)*NoChannels] = new TCanvas();
    canWin[a-START+(i)*NoChannels]->SetTitle(name);
    canWin[a-START+(i)*NoChannels]->SetName(name);

    sprintf(name,"spec%03i_ws%i",a-START+(i)*NoChannels,window_size);
    gSpec[a-START+(i)*NoChannels] = new TGraph(window_size/2, freq, spectrum[a-START+(i)*NoChannels]);
    gSpec[a-START+(i)*NoChannels]->SetTitle(title);
    gSpec[a-START+(i)*NoChannels]->GetXaxis()->SetTitle(" frequency / MHz ");
    gSpec[a-START+(i)*NoChannels]->GetYaxis()->SetTitle(" Amp ");
    canSpec[a-START+(i)*NoChannels] = new TCanvas();
    canSpec[a-START+(i)*NoChannels]->SetTitle(name);
    canSpec[a-START+(i)*NoChannels]->SetName(name);
    
   } //end over START STOP
  }//end for over kwindwo_size

  if(debug) cout << "end of loop over kwindow_size" << endl;
  
  TMultiGraph *mg = new TMultiGraph();
  mg->SetTitle("optimal window size");
  mg->SetName("mg");
  TLegend leg = TLegend(0.87,0.7,0.99,0.99);

  TMultiGraph *mg3 = new TMultiGraph();
  mg3->SetTitle("optimal window size");
  mg3->SetName("mg3");
  TLegend leg3 = TLegend(0.655779, 0.807692, 0.981156, 0.972028);
  
  for(int i=0; i<NoChannels; i++){
   sprintf(name,"gd_mean%02i",i+min_channel_id);
   gd_mean[i] = new TGraph(kwindow_size+1, d_ws, d_mean[i]);
   gd_mean[i]->SetTitle(name);
   gd_mean[i]->SetName(name);
   gd_mean[i]->SetLineColor(i+1);   
   gd_mean[i]->SetLineWidth(2);
   sprintf(title,"mean: channel %02i",i+min_channel_id);
   mg->Add(gd_mean[i]);
   leg.AddEntry(gd_mean[i],title,"L");
   gd_mean[i]->Write();
   
   sprintf(name,"gd_rms%02i",i+min_channel_id);
   gd_rms[i] = new TGraph(kwindow_size+1, d_ws, d_rms[i]);
   gd_rms[i]->SetTitle(name);
   gd_rms[i]->SetName(name);
   gd_rms[i]->SetLineColor(i+1);
   gd_rms[i]->SetLineStyle(2);
   gd_rms[i]->SetLineWidth(4);
   sprintf(title,"rms: channel %02i",i+min_channel_id);
   mg->Add(gd_rms[i]);
   leg.AddEntry(gd_rms[i],title,"L");
   gd_rms[i]->Write();
   
   sprintf(name,"gd_rms_wiht%02i",i+min_channel_id);
   gd_RMS_with[i] = new TGraph(kwindow_size+1, d_ws, d_RMS_with[i]);
   gd_RMS_with[i]->SetTitle(name);
   gd_RMS_with[i]->SetName(name);
   gd_RMS_with[i]->SetLineColor(i+1);
   gd_RMS_with[i]->SetLineStyle(2);
   gd_RMS_with[i]->SetLineWidth(4);
   sprintf(title,"RMS with RFISupp: channel %02i",i+min_channel_id);
   mg3->Add(gd_RMS_with[i]);
//   leg3.AddEntry(gd_RMS_with[i],title,"L");
   gd_RMS_with[i]->Write();

   sprintf(name,"gd_rms_wiht%02i",i+min_channel_id);
   gd_RMS_without[i] = new TGraph(kwindow_size+1, d_ws, d_RMS_without[i]);
   gd_RMS_without[i]->SetTitle(name);
   gd_RMS_without[i]->SetName(name);
   gd_RMS_without[i]->SetLineColor(i+1);
   gd_RMS_without[i]->SetLineWidth(2);
   sprintf(title,"RMS without RFISupp: channel %02i",i+min_channel_id);
   mg3->Add(gd_RMS_without[i]);
//   leg3.AddEntry(gd_RMS_without[i],title,"L");
   gd_RMS_without[i]->Write();
   
   if(i==0){
     sprintf(title,"without RFI suppression");
     leg3.AddEntry(gd_RMS_without[0],title,"L");
     sprintf(title,"with RFI suppression");
     leg3.AddEntry(gd_RMS_with[0],title,"L");
   }

 }
 
 mg->Write();
 mg3->Write();

 TCanvas *gdcan = new TCanvas();
 gdcan->SetName("canmg"); 
 gdcan->SetTitle("canmg");
 gdcan->cd();
 mg->Draw("AL");
 leg.Draw("SAME");
 gdcan->Write();

 TCanvas *gdcan3 = new TCanvas();
 gdcan3->SetName("canmg3"); 
 gdcan3->SetTitle("canmg3");
 gdcan3->cd();
 mg3->Draw("AL");
 mg3->GetXaxis()->SetTitle("window size");
 mg3->GetYaxis()->SetTitle("RMS / ADC unit");
 leg3.Draw("SAME");
 gdcan3->Write();
 
 
 //stopwatch
  for(int j=0; j<NoChannels; j++)for(int i=0; i<kwindow_size+1; i++) {
    real_time[j][i] = timer[j][i].RealTime();
    cpu_time[j][i] = timer[j][i].CpuTime();    
//    cout << "WS = " << i << " - no channel = " << j  << endl;
//    cout << "Real = " <<  timer[j[i].RealTime();
//    cout << "CPU = " << timer[j][i].CpuTime() << endl;
  }
  TMultiGraph *mg2 = new TMultiGraph();
  mg2->SetTitle("processing time");
  mg2->SetName("mg2");
  TLegend leg2 = TLegend(0.655779, 0.807692, 0.981156, 0.972028);
  
  for(int i=0; i<NoChannels; i++){
   cpu_time[i][0] = cpu_time[i][1];
   sprintf(name,"gtime_cpu%02i",i+min_channel_id);
   gtime_cpu[i] = new TGraph(kwindow_size+1, d_ws, cpu_time[i]);
   gtime_cpu[i]->SetTitle(name);
   gtime_cpu[i]->SetName(name);
   gtime_cpu[i]->SetLineColor(i+1);
   gtime_cpu[i]->SetLineWidth(4);
   gtime_cpu[i]->SetLineStyle(2);
   sprintf(title,"cpu time: channel %02i",i+min_channel_id);
   mg2->Add(gtime_cpu[i]);
//   leg2.AddEntry(gtime_cpu[i],title,"L");
   gd_mean[i]->Write();
   
   real_time[i][0] = real_time[i][1];
   sprintf(name,"gtime_real%02i",i+min_channel_id);
   gtime_real[i] = new TGraph(kwindow_size+1, d_ws, real_time[i]);
   gtime_real[i]->SetTitle(name);
   gtime_real[i]->SetName(name);
   gtime_real[i]->SetLineColor(i+1);
   gtime_real[i]->SetLineWidth(2);
   sprintf(title,"real time: channel %02i",i+min_channel_id);
   mg2->Add(gtime_real[i]);
//   leg2.AddEntry(gtime_real[i],title,"L");
   gd_rms[i]->Write();
   
   if(i==0){
     leg2.AddEntry(gtime_cpu[0],"cpu time","L");
     leg2.AddEntry(gtime_real[0],"real time","L");
   }
 }
 mg2->Write();

 TCanvas *gtimecan = new TCanvas();
 gtimecan->SetName("canmg2"); 
 gtimecan->SetTitle("canmg2");
 gtimecan->cd();
 mg2->Draw("AL");
 mg2->GetYaxis()->SetTitle("t / s");
 mg2->GetYaxis()->CenterTitle();
 mg2->GetXaxis()->SetTitle("window size");
 leg2.Draw("SAME");
 gtimecan->Write();
  

  char AntPos[NoChannels][50];
  char AntPol[NoChannels][50];
  char position[5];
  char pol[50];
  for(int i=0; i<NoChannels; i++){
    if(Tchannel_profile->GetEntry(i)==0){
      cerr << "Tchannel_profile->GetEntry() error!" << endl;
      exit(0);
    }
    strcpy(AntPos[i],channel_profiles.antenna_pos);
    strcpy(AntPol[i],channel_profiles.polarisation);
    strcat(AntPol[i],",");
//cout << AntPos[i] << " " << AntPol[i] << endl;
  }


  //fill and write the output file
  output->cd();
  int division=(int)NoChannels/2;
  if((int)NoChannels%2 != 0 ) division++;

  for(int j=0; j<kwindow_size+1; j++){
    canWin[j*NoChannels]->cd();
    canWin[j*NoChannels]->Divide(2,division);
    canSpec[j*NoChannels]->cd();
    canSpec[j*NoChannels]->Divide(2,division);
    for(int i=0; i<NoChannels; i++){
      sscanf(gWin[i+j*NoChannels]->GetTitle(),"%*c%*i_%c%c%c_%s CH0",&position[0], &position[1], &position[2], pol);
      for(int a=0; a<NoChannels; a++){
        if(!strcmp(position,AntPos[a]) && !strcmp(pol,AntPol[a])){
//cout << a << " " <<  AntPos[a] << " " << AntPol[a] << " " << position << " " << pol << endl;
          canWin[j*NoChannels]->cd(a+1);
          gWin[i+j*NoChannels]->Draw("AL");
          //define user range
//	  gWin[i+j*NoChannels]->GetXaxis()->SetLimits(gWin[i+j*NoChannels]->GetN()/2-stepsize/2,gWin[i+j*NoChannels]->GetN()/2+stepsize/2);
          
	  canSpec[j*NoChannels]->cd(a+1);
	  gPad->SetLogy();
          gSpec[i+j*NoChannels]->Draw("AL");

	}
       }
    }
    canWin[j*NoChannels]->Write();
    canSpec[j*NoChannels]->Write();
  }
  
  output->Close();
  

  //do not forget to clean up
  delete output;

  delete Tevent;
  delete Theader;
  delete Tchannel_profile;
  
}