Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 11:57:13

0001 
0002 
0003 #include <TGClient.h>
0004 #include <TCanvas.h>
0005 #include <TF1.h>
0006 #include <TGraph.h>
0007 #include <TRandom.h>
0008 #include <TGButton.h>
0009 #include <TGFrame.h>
0010 #include <TFrame.h>
0011 #include <TRootEmbeddedCanvas.h>
0012 #include <RQ_OBJECT.h>
0013 
0014 
0015 //////////////////////////////////////////////////////////////////////
0016 //
0017 // GeoComparisonGUI.C
0018 //
0019 // Geometry comparison GUI
0020 //
0021 // Usage is easy. Invoke the following command:
0022 //
0023 // root 'GeoComparisonGUI.C("geometry.root")'
0024 // 
0025 // where "geometry.root" is an output of the Geometry Comparison Tool.
0026 //
0027 // (in one line). Instead of the given geometry.root you can use any of the
0028 // standard validation output geometry.root files. A canvas will open and draw
0029 // the known 3x3 plots. In each of the nine plots, you can mark with the
0030 // MIDDLE mouse button a region: Click the MIDDLE mouse button on the first
0031 // edge, and drag to the second edge. (The region is only shown as a black box
0032 // after you finish dragging). The, press the "draw" button at the bottom. The
0033 // chosen modules will be shown in read in each of the nine views. That's
0034 // all. You can zoom into the plots. 
0035 // 
0036 // N.B.: This tool uses TH2F with fine-grained binnig for displaying the
0037 // modules.  This means that if you zoom in with a very high zoom factor, you
0038 // are going to see the single bins. This can be annoying since the binning is
0039 // different for all modules and the selected modules. Therefore, with a very
0040 // high zoom factor, do not expect the distributions to match exactly.  There
0041 // is a way out of that: The tool can be extended such that the second draw
0042 // command uses the exact binning of the first draw command.
0043 //
0044 // Author      : Martin Weber
0045 // Revision    : $Revision: 1.17 $
0046 // Last update : $Date: 2009/10/10 14:17:44 $
0047 // by          : $Author: bonato $
0048 //
0049 //////////////////////////////////////////////////////////////////////
0050 
0051 
0052 // global variables, necessary for the 3x3 plots
0053 
0054 // x-axis variable names
0055 const char * varx[3] = { "r", "z", "phi" }; 
0056 // y-axis variable names
0057 const char * vary[3] = { "dr", "dz", "dphi" };
0058 // selection to apply
0059 char selection[255];
0060 // name of input file
0061 char * gName = 0;
0062 
0063 // duplicate string with help of new
0064 char * strdup_new(const char * text)
0065 {
0066   char * temp =  new char[strlen(text)+1];
0067   strcpy(temp, text);
0068   return temp;
0069 }
0070 
0071 // This class contains the main GUI frame with all embedded canvases.
0072 class MyMainFrame {
0073   RQ_OBJECT("MyMainFrame")
0074   private:
0075   TGMainFrame         * fMain;
0076   TRootEmbeddedCanvas * fEcanvas[3][3];
0077 
0078 public:
0079   MyMainFrame(const TGWindow *p);
0080   virtual ~MyMainFrame();
0081 
0082   void DoDraw(); // main draw routine 
0083   void Reset();  // clear selection
0084   void Exec(Int_t event, Int_t x, Int_t y, TObject *selected);
0085 };
0086 
0087 void MyMainFrame::Exec(Int_t event, Int_t x, Int_t y, TObject *selected)
0088 {
0089   static double x1, y1, x2, y2;
0090   static TGraph * gr = 0;
0091   TCanvas * c = (TCanvas *) gTQSender;
0092   // printf("Canvas %s: event=%d, x=%d, y=%d, selected=%s\n", c->GetName(),
0093   //     event, x, y, selected->IsA()->GetName());
0094 
0095   // get from current canvas the x and y indices necessary for drawing the variables
0096   int theNr = atoi(c->GetName()+7)-1;
0097   int xval = theNr % 3;
0098   int yval = theNr / 3;
0099 
0100   // printf("Canvas nr. %d, xval = %d, yval = %d\n", theNr, xval, yval);
0101 
0102   // when middle mouse button is pressed inside the histogram or frame, start marking area
0103   if (event == 2 && (selected->IsA() == TH2F::Class() || selected->IsA() == TFrame::Class())) {
0104     // 1 = left button pressed
0105     // 2 = middle button pressed
0106     x1 = c->AbsPixeltoX(x); 
0107     y1 = c->AbsPixeltoY(y);
0108   }
0109 
0110   // when dragging with mouse or in the moment of releasing, update the coordinates and draw a frame
0111   if ((event == 22 || event == 12) && ((selected->IsA() == TH2F::Class()) || selected->IsA() == TFrame::Class())) {
0112     // 21 = left button drag action, 11 = left button released
0113     // 22 = middle button drag action, 12 = middle button released
0114 
0115     // remove old rectangular area from plot
0116     if (gr != 0) {
0117       delete gr;
0118       gr = 0;
0119     }
0120     c->Modified();
0121     c->Update();
0122 
0123     // compute new coordinates for new area
0124     x2 = c->AbsPixeltoX(x); 
0125     y2 = c->AbsPixeltoY(y);
0126     // cout << "x1 = " << x1 << " y1 = " << y1 << " x2 = " << x2 << " y2 = " << y2 << endl;
0127     c->cd();
0128 
0129     // draw new rectangular area
0130     double xf[5] = { x1, x2, x2, x1, x1 };
0131     double yf[5] = { y1, y1, y2, y2, y1 };
0132     gr = new TGraph(5, xf, yf);
0133     gr->Draw();
0134     c->Update();
0135 
0136     // cout << "Draw frame" << endl;
0137 
0138     // when middle mouse button released, update the selection string with the
0139     // chosen coordinates
0140     if (event == 12) {
0141       // 11 = left button released
0142       // 12 = middle button released
0143       // cout << "creating selection" << endl;
0144       sprintf(selection, 
0145           "(%s>%f)&&(%s<%f)&&(%s>%f)&&(%s<%f)",
0146           vary[xval],
0147           TMath::Min(y2, y1),
0148           vary[xval],
0149           TMath::Max(y2, y1),
0150           varx[yval],
0151           TMath::Min(x2, x1),
0152           varx[yval],
0153           TMath::Max(x2, x1));
0154       // cout << "selection = " << selection << endl;
0155     }
0156   }
0157 }
0158 
0159 MyMainFrame::MyMainFrame(const TGWindow *p) {
0160   // Create a main frame (vertical layout by default)
0161   fMain = new TGMainFrame(p, 800, 600);
0162   
0163   // Create first row horizontal frame
0164   TGHorizontalFrame * hframe = new TGHorizontalFrame(fMain,200,200);
0165   // Create three canvas widgets in row
0166   fEcanvas[0][0] = new TRootEmbeddedCanvas("Ecanvas1",hframe,200,200);
0167   hframe->AddFrame(fEcanvas[0][0], new TGLayoutHints(kLHintsExpandX | kLHintsExpandY,
0168                            5, 5, 5, 5));
0169   fEcanvas[0][0]->GetCanvas()->Connect("ProcessedEvent(Int_t,Int_t,Int_t,TObject*)", "MyMainFrame", "",
0170                        "Exec(Int_t,Int_t,Int_t,TObject*)");
0171   fEcanvas[1][0] = new TRootEmbeddedCanvas("Ecanvas2",hframe,200,200);
0172   hframe->AddFrame(fEcanvas[1][0], new TGLayoutHints(kLHintsExpandX | kLHintsExpandY,
0173                            5, 5, 5, 5));
0174   fEcanvas[1][0]->GetCanvas()->Connect("ProcessedEvent(Int_t,Int_t,Int_t,TObject*)", "MyMainFrame", "",
0175                        "Exec(Int_t,Int_t,Int_t,TObject*)");
0176   fEcanvas[2][0] = new TRootEmbeddedCanvas("Ecanvas3",hframe,200,200);
0177   hframe->AddFrame(fEcanvas[2][0], new TGLayoutHints(kLHintsExpandX | kLHintsExpandY,
0178                            5, 5, 5, 5));
0179   fEcanvas[2][0]->GetCanvas()->Connect("ProcessedEvent(Int_t,Int_t,Int_t,TObject*)", "MyMainFrame", "",
0180                        "Exec(Int_t,Int_t,Int_t,TObject*)");
0181   // add to main frame
0182   fMain->AddFrame(hframe, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY,
0183                         5, 5, 5, 5));
0184 
0185 
0186   // Create second row horizontal frame
0187   TGHorizontalFrame * hframe = new TGHorizontalFrame(fMain,200,200);
0188   // Create three canvas widgets in row
0189   fEcanvas[0][1] = new TRootEmbeddedCanvas("Ecanvas4",hframe,200,200);
0190   hframe->AddFrame(fEcanvas[0][1], new TGLayoutHints(kLHintsExpandX | kLHintsExpandY,
0191                            5, 5, 5, 5));
0192   fEcanvas[0][1]->GetCanvas()->Connect("ProcessedEvent(Int_t,Int_t,Int_t,TObject*)", "MyMainFrame", "",
0193                        "Exec(Int_t,Int_t,Int_t,TObject*)");
0194   fEcanvas[1][1] = new TRootEmbeddedCanvas("Ecanvas5",hframe,200,200);
0195   hframe->AddFrame(fEcanvas[1][1], new TGLayoutHints(kLHintsExpandX | kLHintsExpandY,
0196                            5, 5, 5, 5));
0197   fEcanvas[1][1]->GetCanvas()->Connect("ProcessedEvent(Int_t,Int_t,Int_t,TObject*)", "MyMainFrame", "",
0198                        "Exec(Int_t,Int_t,Int_t,TObject*)");
0199   fEcanvas[2][1] = new TRootEmbeddedCanvas("Ecanvas6",hframe,200,200);
0200   hframe->AddFrame(fEcanvas[2][1], new TGLayoutHints(kLHintsExpandX | kLHintsExpandY,
0201                            5, 5, 5, 5));
0202   fEcanvas[2][1]->GetCanvas()->Connect("ProcessedEvent(Int_t,Int_t,Int_t,TObject*)", "MyMainFrame", "",
0203                        "Exec(Int_t,Int_t,Int_t,TObject*)");
0204   // add to main frame
0205   fMain->AddFrame(hframe, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY,
0206                         5, 5, 5, 5));
0207 
0208   // Create third row horizontal frame
0209   TGHorizontalFrame * hframe = new TGHorizontalFrame(fMain,200,200);
0210   // Create three canvas widgets in row
0211   fEcanvas[0][2] = new TRootEmbeddedCanvas("Ecanvas7",hframe,200,200);
0212   hframe->AddFrame(fEcanvas[0][2], new TGLayoutHints(kLHintsExpandX | kLHintsExpandY,
0213                            5, 5, 5, 5));
0214   fEcanvas[0][2]->GetCanvas()->Connect("ProcessedEvent(Int_t,Int_t,Int_t,TObject*)", "MyMainFrame", "",
0215                        "Exec(Int_t,Int_t,Int_t,TObject*)");
0216   fEcanvas[1][2] = new TRootEmbeddedCanvas("Ecanvas8",hframe,200,200);
0217   hframe->AddFrame(fEcanvas[1][2], new TGLayoutHints(kLHintsExpandX | kLHintsExpandY,
0218                            5, 5, 5, 5));
0219   fEcanvas[1][2]->GetCanvas()->Connect("ProcessedEvent(Int_t,Int_t,Int_t,TObject*)", "MyMainFrame", "",
0220                        "Exec(Int_t,Int_t,Int_t,TObject*)");
0221   fEcanvas[2][2] = new TRootEmbeddedCanvas("Ecanvas9",hframe,200,200);
0222   hframe->AddFrame(fEcanvas[2][2], new TGLayoutHints(kLHintsExpandX | kLHintsExpandY,
0223                            5, 5, 5, 5));
0224   fEcanvas[2][2]->GetCanvas()->Connect("ProcessedEvent(Int_t,Int_t,Int_t,TObject*)", "MyMainFrame", "",
0225                        "Exec(Int_t,Int_t,Int_t,TObject*)");
0226   // add to main frame
0227   fMain->AddFrame(hframe, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY,
0228                         5, 5, 5, 5));
0229 
0230   // Create a horizontal frame widget with buttons
0231   hframe = new TGHorizontalFrame(fMain,200,40);
0232   TGTextButton *draw = new TGTextButton(hframe,"&Draw");
0233   draw->Connect("Clicked()","MyMainFrame",this,"DoDraw()");
0234   hframe->AddFrame(draw, new TGLayoutHints(kLHintsCenterX,5,5,3,4));
0235   TGTextButton *reset = new TGTextButton(hframe,"&Reset");
0236   reset->Connect("Clicked()","MyMainFrame",this,"Reset()");
0237   hframe->AddFrame(reset, new TGLayoutHints(kLHintsCenterX,5,5,3,4));
0238   TGTextButton *exit = new TGTextButton(hframe,"&Exit",
0239                                         "gApplication->Terminate(0)");
0240   // add buttons to frame
0241   hframe->AddFrame(exit, new TGLayoutHints(kLHintsCenterX,5,5,3,4));
0242   // add button frame to main frame
0243   fMain->AddFrame(hframe, new TGLayoutHints(kLHintsCenterX,2,2,2,2));
0244 
0245 
0246   // Set a name to the main frame
0247   fMain->SetWindowName("Geometry Comparison GUI");
0248   // Map all subwindows of main frame
0249   fMain->MapSubwindows();
0250   // Initialize the layout algorithm
0251   fMain->Resize(fMain->GetDefaultSize());
0252   // Map main frame
0253   fMain->MapWindow();
0254 }
0255 
0256 // This is the main drawing routine. It draws to histograms in each canvas:
0257 // First the geometry comparison for all modules, and in a different color on
0258 // top the selected modules.
0259 void MyMainFrame::DoDraw()
0260 {
0261   // variables
0262   float dr, dphi, dz, r, phi, z;
0263 
0264   // open file and get tree from file
0265   TFile * infile = new TFile(gName);
0266   TTree * t = (TTree *) infile->Get("alignTree");
0267   if (t == 0) {
0268     cout << "ERR: could not find \"alignTree\" in file " << gName << endl;
0269     gApplication->Terminate(0);
0270   }
0271   // cout << "selection = " << selection << endl;
0272 
0273 
0274   // We need to store histogram names and the commands to pass to
0275   // TTree::Draw()
0276   char histname[20];
0277   char histname2[20];
0278   char drawstring[64];
0279   char drawstring2[64];
0280 
0281   // loop over the 3x3 matrix of plots
0282   for (int i = 0; i < 3; i++) {
0283     for (int j = 0; j < 3; j++) {
0284       // get and select canvas
0285       TCanvas * fCanvas = fEcanvas[i][j]->GetCanvas();
0286       fCanvas->cd();
0287 
0288       // prepare draw strings
0289       sprintf(histname, "%s vs %s", vary[i], varx[j]);
0290       sprintf(histname2, "%s vs %s_2", vary[i], varx[j]);
0291       sprintf(drawstring, "%s:%s>>%s", vary[i], varx[j], histname);
0292       sprintf(drawstring2, "%s:%s>>%s", vary[i], varx[j], histname2);
0293       // cout << "drawstring = " << drawstring << endl;
0294 
0295       // first plot: draw without selection, draw all modules into histogram. 
0296       t->Draw(drawstring, "", "goff");
0297 
0298       // get histogram and set draw options
0299       TObject * obj = gDirectory->Get(histname);
0300       // cout << "obj is a " << obj->IsA()->GetName() << endl;
0301       TH2F * h2 = (TH2F *) obj;
0302       if (h2 != 0) {
0303         h2->SetTitle(histname);
0304         h2->Draw();
0305         h2->SetDirectory(0);
0306         h2->SetMarkerStyle(1);
0307         h2->Draw("p");
0308       }
0309       else {
0310         cout << "Histo " << histname << " not found" << endl;
0311       }
0312 
0313       // second plot on top: use the selection
0314       t->Draw(drawstring2, selection, "goff");
0315 
0316       // get histogram and set draw options
0317       TH2F * h22 = (TH2F *) gDirectory->Get(histname2);
0318       if (h22 != 0) {
0319         // cout << "Got histo" << endl;
0320         h22->SetDirectory(0);
0321         h22->SetTitle(histname2);
0322         h22->SetMarkerColor(kRed);
0323         h22->SetMarkerStyle(7);
0324         h22->Draw("psame");
0325       }
0326       else {
0327         cout << "Histo " << histname2 << " not found" << endl;
0328       }
0329       fCanvas->Update();
0330     }
0331   }
0332   // close file
0333   delete infile;
0334 }
0335 
0336 void MyMainFrame::Reset()
0337 {
0338   // set the selection in such a way that no module gets selected
0339   strcpy(selection, "z==3000");
0340   DoDraw();
0341 }
0342 
0343 MyMainFrame::~MyMainFrame() 
0344 {
0345   // Clean up used widgets: frames, buttons, layouthints
0346   fMain->Cleanup();
0347   delete fMain;
0348 }
0349 
0350 // main routine: set global options, create frame, draw initial plots
0351 void GeoComparisonGUI(const char * fname) {
0352   // setup binning for plots
0353   gEnv->SetValue("Hist.Binning.2D.x", "1000");
0354   gEnv->SetValue("Hist.Binning.2D.y", "1000");
0355   gStyle->SetOptStat(0);
0356   // Popup the GUI...
0357   gName = strdup_new(fname);
0358   MyMainFrame * frame = new MyMainFrame(gClient->GetRoot());
0359   frame->Reset();
0360 }