Back Midas Rome Roody Rootana
  Rome Analyzer Framework  Not logged in ELOG logo
Entry  03 Sep 2015, Farrukh Azfar, Bug Report, Saving canvas as pdf via a button one vs many pads MIDTBCTab.cppMIDTRCTab.cpp
    Reply  07 Sep 2015, Ryu Sawada, Bug Report, Saving canvas as pdf via a button one vs many pads SampleCanvas.20150907.175151.pdf
       Reply  07 Sep 2015, Farrukh Azfar, Bug Report, Saving canvas as pdf via a button one vs many pads 
Message ID: 151     Entry time: 03 Sep 2015     Reply to this: 160
Author: Farrukh Azfar 
Topic: Bug Report 
Subject: Saving canvas as pdf via a button one vs many pads 
Dear Colleagues 

we have implemented in one of our tabs a button to call a function which saves a 
canvas to a pdf file with a time date stamp.

1) This works fine when there is only one histogram on the canvas and this code 
is MIDTBCTab.cpp (attached)

2) When the canvas is divided up into a 5x11 pads with 55 histograms and 
we want to save the _whole_ canvas with the whole picture (_not_ any individual 
pads) the code crashes  and no file is generated - the tab code is MIDTRCTab.cpp
and this is also attached 

I am wondering what is going on - is there anything obviously wrong ?

many thanks 

Farrukh
Attachment 1: MIDTBCTab.cpp  9 kB  Uploaded 03 Sep 2015  | Show | Hide all | Show all
Attachment 2: MIDTRCTab.cpp  8 kB  Uploaded 03 Sep 2015  | Hide | Hide all | Show all
////////////////////////////////////////////////////////////////////////////////
//                                                                            //
// MIDTRCTab                                                                  //
//                                                                            //
// Begin_Html <!--
/*-->

<!--*/
// --> End_Html
//                                                                            //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////

/* Generated header file containing necessary includes                        */
#include "generated/MIDTRCTabGeneratedIncludes.h"

////////////////////////////////////////////////////////////////////////////////
/*  This header was generated by ROMEBuilder. Manual changes above the        *
 * following line will be lost next time ROMEBuilder is executed.             */
/////////////////////////////////////----///////////////////////////////////////

#include "generated/MIDWindow.h"
#include "generated/MIDAnalyzer.h"
#include "tabs/MIDTRCTab.h"
#include "tasks/MIDTFillHistogramraw.h"


#include "TAxis.h"
#include "TLatex.h"
#include <TGClient.h>
#include <TCanvas.h>

#include <TRandom.h>
#include <TGButton.h>
#include <TGFrame.h>
#include <TRootEmbeddedCanvas.h>
#include <RQ_OBJECT.h>
#include<TString.h>
#include<TDatime.h>

// uncomment if you want to include headers of all folders
#include "MIDAllFolders.h"


ClassImp(MIDTRCTab)
using namespace std;
//______________________________________________________________________________
void MIDTRCTab::Init()
{
  // Create style for this tab                                                                                                                                         
  TStyle *orgStyle = gStyle;
  fStyle = new TStyle(*orgStyle); // copy the original style                                                                                                           
  fStyle->SetOptTitle(1);
  fStyle->SetTitleOffset(0.38, "y");
  fStyle->SetPadGridX(1);
  fStyle->SetPadGridY(1);
  fStyle->SetPadTickX(1);
  fStyle->SetPadTickY(1);
  fStyle->SetFrameBorderSize(0);
  fStyle->SetFrameBorderMode(0);
  fStyle->SetFrameFillStyle(0);
  fStyle->SetTitleSize(0.06, "t");
  fStyle->SetStatH(0.30);
  fStyle->SetStatW(0.30);
  fStyle->cd();
  // Create a vertical frame containing buttons and canvas                                                                                                             
  fVert = new TGVerticalFrame(this, (UInt_t) (700 * gAnalyzer->GetWindow()->GetWindowScale()), (UInt_t) (700 * gAnalyzer->GetWindow()->GetWindowScale()));

  // Create an embedded canvas and add to the main frame, centered in x and y                                            


  // set data member                                                                                
  fCanvasName =  new TString ("Sample Canvas");


                                              
  fCanvas = new TRootEmbeddedCanvas("Sample Canvas", fVert, (UInt_t) (600 * gAnalyzer->GetWindow()->GetWindowScale()), (UInt_t) (600 * gAnalyzer->GetWindow()->GetWindowScale()));
  fCanvas->GetCanvas()->Divide(5, 11);



  // Create "Update" button                                                    
  ULong_t yellow;
  gClient->GetColorByName("Yellow",yellow);                                                     
  fBUpdate = new TGTextButton(fVert, "Update");
  fBUpdate->ChangeBackground(yellow);
  fBUpdate->Associate(this);

  //  fVert->AddFrame(fCanvas, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY, 4, 4, 4, 4));
  fVert->AddFrame(fBUpdate, new TGLayoutHints(kLHintsCenterX, 10, 10, 4, 4));
  AddFrame(fVert, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY, 10, 10, 4, 4));


  //create Exit button
  ULong_t red;
  gClient->GetColorByName("Red",red);
  fExit = new TGTextButton(fVert, "&Exit", "gApplication->Terminate(0)");
  fExit->ChangeBackground(red);
  fExit->Associate(this);
  fVert->AddFrame(fExit, new TGLayoutHints(kLHintsCenterX, 10, 10, 4, 4));


  //create save button                                                                                                   
                                                                                                                         
  ULong_t green;
  gClient->GetColorByName("Green",green);
  fSaveCanvas = new TGTextButton(fVert, "SaveCanvas");
  fSaveCanvas->ChangeBackground(green);
  fSaveCanvas->Associate(this);
  fVert->AddFrame(fSaveCanvas, new TGLayoutHints(kLHintsCenterX, 10, 10, 4, 4));


  fVert->AddFrame(fCanvas, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY, 4, 4, 4, 4));
  //  AddFrame(fVert, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY, 4, 4, 4, 4));



  ConnectAll();

  orgStyle->cd();

}

//______________________________________________________________________________
void MIDTRCTab::EndInit()
{
}

//______________________________________________________________________________
void MIDTRCTab::EventHandler()
{
}

//______________________________________________________________________________
void MIDTRCTab::MenuClicked(TGPopupMenu * /* menu */, Long_t /* param */)
{
}

//______________________________________________________________________________
void MIDTRCTab::TabSelected()
{
}

//______________________________________________________________________________
void MIDTRCTab::TabUnSelected()
{
}


  // Save Canvas:                                                                                   
  // you'll need a function of the same signature in your .h -> also make sure that TString is included in your .h                                                                                      
  void MIDTRCTab::SaveCanvas()
{

  // if following doesn't work blame Sabato Leo                                                     
  TString * fileName = new TString (fCanvasName->Data());

  //replace all spaces in your canvas name                                                        
  fileName->ReplaceAll(" ", "");

  //generate data and time stamp -> Recall you'll have to make sure TDatime is included           
  TDatime  dateTime; // time and date remain frozen when this function was called                 
  TString dateAsString = Form("%d", dateTime.GetDate());
  TString timeAsString = Form("%d", dateTime.GetTime());

  // append time date etc to canvas name                                                          

  (*fileName) += ".";
  (*fileName) += dateAsString;
  (*fileName) += ".";
  (*fileName) += timeAsString;
  (*fileName) += ".pdf"; // pdf for now maybe think about png and other options for user later ? 

  // debug                                                                                        
  //cout << " This is the filename " << fileName->Data() << endl;

  // now print it and hope it works ....                                                          
  fCanvas->GetCanvas()->Print(fileName->Data());
  //  cout << " Already attempted to print this file  " << fileName->Data() << endl;
  }





void MIDTRCTab::Update()
{
  TStyle *orgStyle = gStyle;
  fStyle->cd();

  TH1 *histo;

  for (Int_t i = 0; i < 55; i++) { //loop over all 55 pads
    fCanvas->GetCanvas()->cd(i + 1);
    histo = (TH1F *) gAnalyzer->GetFillHistogramrawTask()->Geth1_wfAt(i);
    if (!histo) {
      cout << "Histo ADC not available." << endl;
    } else {
      histo->Draw();
    }
  }

  fCanvas->GetCanvas()->cd(0);
  fCanvas->GetCanvas()->Modified();
  fCanvas->GetCanvas()->Update();

  orgStyle->cd();

  return;




}
//_____________________________________________________________________________\
_                                                                               
void MIDTRCTab::ConnectAll()
{
  fBUpdate->Connect("Pressed()", "MIDTRCTab", this, "Update()");
  fExit->Connect("Pressed()", "MIDTRCTab", this, "&Exit()");
  fSaveCanvas->Connect("Pressed()","MIDTRCTab", this, "SaveCanvas()");

}

//_____________________________________________________________________________\
_                                                                               
void MIDTRCTab::DisconnectAll()
{
  if (fBUpdate) { fBUpdate->Disconnect(this); }
  if (fExit) { fExit->Disconnect(this); }
  if(fSaveCanvas) { fSaveCanvas->Disconnect(this);}

}
ELOG V3.1.4-2e1708b5