Attachment 'drawCellID2.C'

Download

   1 /// \author Jörgen Samson <joergen.samson@desy.de>
   2 /// Description:
   3 
   4 #include "TTree.h"
   5 #include "TCanvas.h"
   6 #include "TGraph.h"
   7 #include "TPolyLine.h"
   8 #include "TMarker.h"
   9 #include "TString.h"
  10 #include "TText.h"
  11 #include "TLatex.h"
  12 #include "TArrow.h"
  13 #include "TLine.h"
  14 #include "TDirectory.h"
  15 #include "TStyle.h"
  16 
  17 #include <iostream>
  18 #include <map>
  19 #include <utility>
  20 
  21 using namespace std;
  22 
  23 
  24 //===================================================
  25 /// \todo fixme: replace hard-coded cellid and cellsize 
  26 ///       decoding with calice code
  27 inline Int_t decodeI( Int_t cellID )
  28 { 
  29   return (cellID&0x007fc0)>>6; 
  30 }
  31 
  32 inline Int_t decodeJ( Int_t cellID )
  33 { 
  34   return (cellID&0xff8000)>>15; 
  35 }
  36 
  37 inline Int_t decodeLay( Int_t cellID )
  38 {
  39   return ( (cellID&0x3F000000)>>24 ) + 1 ;
  40 }
  41 
  42 inline bool layerCoarse( Int_t lay )
  43 {
  44   return ( lay > 30 );
  45 }
  46 
  47 #define FIRST_COARSE 31
  48 #define I3x3_LOW 31
  49 #define I3x3_HIGH 60
  50 #define I6x6_LOW 13
  51 #define I6x6_HIGH 78
  52 #define J3x3_LOW 31
  53 #define J3x3_HIGH 60
  54 #define J6x6_LOW 13
  55 #define J6x6_HIGH 78
  56 
  57 int cellsize( int i, int j, bool coarse = false )
  58 {
  59 
  60 //  //  cout << cellid << endl;
  61 //  int i = (cellid & 0x7fc0 ) >> 6;
  62 //  int j = (cellid & 0xff8000) >> 15;
  63 
  64 //  int layer = (cellid & 0x3F000000) >> 24 ;
  65 //  if (layer >= FIRST_COARSE ) // layer index starts with 1?
  66 //    coarse = true;
  67 
  68   int tilesize;
  69   if( coarse == false )
  70     tilesize=3;
  71   else
  72     tilesize=6;
  73 
  74   if ( i<I3x3_LOW || j<J3x3_LOW || i>I3x3_HIGH || j>J3x3_HIGH )
  75     tilesize=6;
  76 
  77   if ( i<I6x6_LOW || j<J6x6_LOW || i>I6x6_HIGH || j>J6x6_HIGH )
  78     tilesize=12;
  79 
  80   return tilesize;
  81 
  82 }
  83 
  84 //================================================================
  85 
  86 /// draws a square into current root canvas
  87 /// \param x1   x-coordinate of lower left corner
  88 /// \param y1   y-coordinate of lower left corner
  89 /// \param size width/height of the square
  90 /// \param col  color of the border lines
  91 void paintBox( float x1, float y1, float size, Color_t col=kBlack )
  92 {
  93   float x[5] = { x1, x1, x1+size, x1+size, x1 };
  94   float y[5] = { y1, y1+size, y1+size, y1, y1} ;
  95   
  96   TPolyLine *pline = new TPolyLine(5,x,y);
  97   //pline->SetFillColor(38);
  98   pline->SetLineColor(col);
  99   pline->SetLineWidth(2);
 100   //pline->Draw("f");
 101   pline->Draw();
 102 }
 103 
 104 void paintMarker( float x, float y, Color_t col=kRed)
 105 {
 106   TMarker *m = new TMarker( x, y, 8);
 107   m->SetMarkerColor(col);
 108   m->Draw();
 109 }
 110 
 111 /// draws a text with the format i/j into a root canvas
 112 /// \param x0   x-coordinate of lower left corner of a square in which the id has to fit in
 113 /// \param y0   y-coordinate of lower left corner of a square in which the id has to fit in
 114 /// \parem size width of the square in which the id has to fit in
 115 void paintID( float x0, float y0, float size,  TString i, TString j, Color_t col=kBlack)
 116 {
 117    Float_t tsize=0.05/12*size;
 118 
 119    Float_t x= x0+size/2.;
 120    Float_t y= y0+size/2.;
 121 
 122    TText *ti = new TText( x - size/16, y, i );
 123    ti->SetTextFont(42);
 124    ti->SetTextSize(tsize);
 125    ti->SetTextAlign(31);
 126    ti->SetTextColor(col);
 127    ti->Draw();
 128 
 129    TText *tj = new TText( x + size/20, y, j );
 130    tj->SetTextFont(42);
 131    tj->SetTextSize(tsize);
 132    tj->SetTextAlign(13);
 133    tj->SetTextColor(col);
 134    tj->Draw();
 135 
 136    TLine *slash = new TLine(x-size/5.,y-size/5.,x+size/5.,y+size/5.);
 137    slash->SetLineWidth(2);
 138    slash->Draw();
 139 }
 140 
 141 
 142 void paintCoord()
 143 {
 144 
 145   TMarker *marker = new TMarker(-41,-42,20);
 146   marker->SetMarkerStyle(20);
 147   marker->Draw();
 148 
 149   TArrow *arrow = new TArrow(-41,-42,-36.5,-42,0.02,"|>");
 150   arrow->SetFillColor(1);
 151   arrow->SetFillStyle(1001);
 152   arrow->SetLineWidth(2);
 153   arrow->SetAngle(40);
 154   arrow->Draw();
 155 
 156   TLatex *tex = new TLatex(-36,-44,"X");
 157   tex->SetTextSize(0.025);
 158   tex->SetLineWidth(2);
 159   tex->Draw();
 160 
 161   arrow = new TArrow(-41,-42,-41,-37.5,0.02,"|>");
 162   arrow->SetFillColor(1);
 163   arrow->SetFillStyle(1001);
 164   arrow->SetLineWidth(2);
 165   arrow->SetAngle(40);
 166   arrow->Draw();
 167 
 168   tex = new TLatex(-40,-38,"Y");
 169   tex->SetTextSize(0.025);
 170   tex->SetLineWidth(2);
 171   tex->Draw();
 172 
 173   arrow = new TArrow(-41,-42,-44,-45,0.02,"|>");
 174   arrow->SetFillColor(1);
 175   arrow->SetFillStyle(1001);
 176   arrow->SetLineWidth(2);
 177   arrow->SetAngle(40);
 178   arrow->Draw();
 179 
 180   tex = new TLatex(-42.5,-46.5,"Z");
 181   tex->SetTextSize(0.025);
 182   tex->SetLineWidth(2);
 183   tex->Draw();
 184 
 185 }
 186 
 187 
 188 
 189 /// \param treename name of the tree, written by RootTreeWriter's hitWriteEngine
 190 /// \param col      draw boxes in different colors
 191 /// \param nEvents  number of events to usee for cellid map creation
 192 /// \param firstLay index of first layer which is used to create the cellid map
 193 /// \param lastLay  index of last layer whcih is used to create the cellid map
 194 bool drawCellID2( TString treename = TString("bigtree"), bool col= true, Int_t nEvents=500, Int_t firstLay=1, Int_t lastLay=30)
 195 {
 196   // -------------- prepare reading root tree -----------------------
 197   //gROOT->Reset();
 198 
 199   TTree* tree = (TTree*) gDirectory->Get(treename);
 200   if ( tree == NULL )
 201     {
 202       cerr << "tree \"" << treename 
 203 	   << "\" not in current directory. quit..." << endl;
 204       return false;
 205     }
 206 
 207   Int_t           Ahc_nHits;
 208   Int_t           Ahc_hitCellID[7609];     //[nhitsahc]
 209   Float_t         Ahc_hitPos   [7609][3];  //[nhitsahc][3]
 210 
 211 
 212   TBranch        *b_Ahc_nHits;     //!
 213   TBranch        *b_Ahc_hitCellID; //!
 214   TBranch        *b_Ahc_hitPos;    //!
 215 
 216   tree->SetBranchAddress("ahc_nHits", &Ahc_nHits, &b_Ahc_nHits);
 217   tree->SetBranchAddress("ahc_hitCellID", Ahc_hitCellID, &b_Ahc_hitCellID);
 218   tree->SetBranchAddress("ahc_hitPos", Ahc_hitPos, &b_Ahc_hitPos);
 219 
 220   Long64_t nentries = tree->GetEntriesFast();
 221 
 222   // -------------- prepare map to hold cellid and positions ----------
 223 
 224   typedef pair<int,int> CellID_t;
 225   typedef pair<float,float> HitPos_t;
 226   typedef map< CellID_t, HitPos_t > HitMap_t;
 227   typedef HitMap_t::const_iterator HitMapIter;
 228   
 229   HitMap_t hitMap;
 230 
 231   bool isCoarse = false; //initialise to avoide warning on old compilers
 232   bool isCoarseSet = false;
 233 
 234   // -------------- fill map with cellid and positions ----------------
 235 
 236   for ( Long64_t jentry=0; jentry<nentries && jentry < nEvents; ++jentry )
 237     {
 238       tree->GetEntry(jentry);
 239       
 240       for ( Int_t jcell = 0; jcell < Ahc_nHits; ++jcell)
 241 	{
 242 	  int i = decodeI(Ahc_hitCellID[jcell]);
 243 	  int j = decodeJ(Ahc_hitCellID[jcell]);
 244 	  int lay = decodeLay(Ahc_hitCellID[jcell]);
 245 	  float x = Ahc_hitPos[jcell][0]/10;
 246 	  float y = Ahc_hitPos[jcell][1]/10;
 247 
 248 
 249 	  // only use layers given in function argument
 250 	  if ( lay < firstLay || lay > lastLay )
 251 	    continue;
 252 
 253 	  if ( !isCoarseSet )
 254 	    isCoarse = layerCoarse( lay );
 255 
 256 	  //----- check for consistency of cell position --
 257 	  if ( layerCoarse( lay ) != isCoarse )
 258 	    {
 259 	      cerr << "Mixing fine and coarse modules. Quit..." << endl;
 260 	      return false;
 261 	    }
 262 	  HitMap_t::iterator it = hitMap.find( CellID_t( i,j ) );
 263 	  if ( it != hitMap.end() )
 264 	    {
 265 	      float oldx = it->second.first;
 266 	      float oldy = it->second.second;
 267 	      if ( oldx != x || oldy != y )
 268 		{
 269 		  cerr << "Found cellID with different x/y coordinates:"
 270 		       << " x1=" << oldx <<", x2=" << x
 271 		       <<", y1=" << oldy <<", y2=" << y
 272 		       << endl;
 273 		  return false;
 274 		}
 275 	    }
 276 	  //------- \consitency check ---------------------
 277 					       
 278 	  hitMap[ CellID_t( i, j) ] = HitPos_t( x, y );
 279 	  cout << ".";
 280 	}
 281       cout << flush;
 282     }
 283   cout << endl;
 284 
 285 
 286   //-------------- pait cell map sketch ------------------------------------
 287 
 288 
 289   TCanvas* c1 = new TCanvas("cells","cells",600,600);
 290   c1->Range( -50,-50,50,50);
 291 
 292 
 293   UInt_t counter = 0;
 294   for( HitMapIter pHit = hitMap.begin(); pHit != hitMap.end(); ++pHit, ++counter )
 295     {
 296       int i = pHit->first.first;
 297       int j = pHit->first.second;
 298       //float xi = float(i)-46.;
 299       //float yj = float(j)-46.;
 300       float x = pHit->second.first +2.4;
 301       float y = pHit->second.second -0.2;
 302 
 303       Int_t   tilesize=cellsize( i, j, isCoarse ); 
 304       Color_t tilecolor= kBlack;
 305 
 306       Float_t x0 = x - float(tilesize)/2;
 307       Float_t y0 = y - float(tilesize)/2;
 308 
 309       if ( col )
 310 	{
 311 	  if ( tilesize == 3 )
 312 	    tilecolor = 46;
 313 	  if ( tilesize == 6 )
 314 	    tilecolor = 38;
 315 	  if ( tilesize == 12 )
 316 	    tilecolor = 28;
 317 	}
 318 
 319       paintBox( x0,y0,tilesize, tilecolor );
 320 
 321       TString is, js;
 322       is += i;
 323       //is += 92-i-tilesize;
 324       js += j;
 325 
 326       paintID(x0,y0,tilesize, is, js);
 327 
 328       //paintMarker( xi,yj, ((counter+1)&0xff) );
 329       
 330       cout << "i:"<<i<<" j:"<<j<<" x:"<<x<<" y:"<< y << endl;
 331 
 332     }
 333 
 334   // Draw coordinate system
 335   paintCoord();
 336 
 337   return true;
 338 }
 339 
 340 
 341 void printCellID( TString treename = TString("bigtree"), 
 342 		  TString filename = TString("cellIDMap.eps"), 
 343 		  Int_t nEvents = 500,
 344 		  Int_t layerFirst = 1, Int_t layerLast = 30 )
 345 {
 346   if ( drawCellID2(treename,false,nEvents,layerFirst,layerLast) )
 347     {
 348       gStyle->SetLineScalePS(2.3);
 349       gPad->Print( filename );
 350     }
 351 }

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2009-04-01 11:47:09, 8.4 KB) [[attachment:Makesoft.make]]
  • [get | view] (2009-03-31 22:39:29, 2.2 KB) [[attachment:buildgitafs.sh]]
  • [get | view] (2008-06-26 18:01:39, 8.6 KB) [[attachment:drawCellID2.C]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.