TaskHelpers.cxx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. //-----------------------------------------------------------
  2. // File and Version Information:
  3. // $Id$
  4. //
  5. // Description:
  6. // Implementation of task unilities
  7. // see TaskHelpers.h for details
  8. //
  9. // Environment:
  10. // Software developed for the MPD detector at NICA
  11. //
  12. // Author List:
  13. // Roman Salmin
  14. //
  15. //
  16. //-----------------------------------------------------------
  17. #include "TaskHelpers.h"
  18. #include <TDirectory.h>
  19. #include <TFile.h>
  20. #include <TError.h>
  21. std::vector<std::string> pathToList(const char *path);
  22. TH1F* CreateHistogram1(const char* name, const char *xtitle, int nbin,
  23. Float_t firstBin, Float_t lastBin)
  24. {
  25. TH1F* h = new TH1F(name, name, nbin, firstBin, lastBin);
  26. h->SetXTitle(xtitle);
  27. return h;
  28. }
  29. TH2F* CreateHistogram2(const char* name, const char* xtitle, const char* ytitle,
  30. int nbinx, Float_t firstBinX, Float_t lastBinX, int nbiny, Float_t firstBinY, Float_t lastBinY)
  31. {
  32. TH2F *h = new TH2F(name, name, nbinx, firstBinX, lastBinX, nbiny, firstBinY, lastBinY);
  33. h->SetXTitle(xtitle);
  34. h->SetYTitle(ytitle);
  35. return h;
  36. }
  37. TH3F* CreateHistogram3(const char* name, const char* xtitle, const char* ytitle, const char* ztitle,
  38. int nbinx, Float_t firstBinX, Float_t lastBinX, int nbiny, Float_t firstBinY, Float_t lastBinY,
  39. int nbinz, Float_t firstBinZ, Float_t lastBinZ)
  40. {
  41. TH3F *h = new TH3F(name, name, nbinx, firstBinX, lastBinX, nbiny, firstBinY, lastBinY, nbinz, firstBinZ, lastBinZ);
  42. h->SetXTitle(xtitle);
  43. h->SetYTitle(ytitle);
  44. h->SetZTitle(ztitle);
  45. return h;
  46. }
  47. //go to given directory if directory dose not exists create it
  48. void toDirectory(const char* dir)
  49. {
  50. std::vector<std::string> splitedPath = pathToList(dir);
  51. TDirectory *d = (TDirectory*) gFile;
  52. for(UInt_t i = 0; i < splitedPath.size(); i++)
  53. {
  54. TDirectory *nextDir = (TDirectory*) d->FindObject( splitedPath[i].c_str() );
  55. if (! nextDir)
  56. {
  57. d->mkdir( splitedPath[i].c_str() );
  58. nextDir = (TDirectory*) d->GetDirectory( splitedPath[i].c_str() );
  59. }
  60. if ( !nextDir)
  61. Error("QAHelpers::toDir","Can't create/find directory");
  62. d = nextDir;
  63. }
  64. gFile->cd(dir);
  65. }
  66. //split path for components using '/' as delimeter
  67. std::vector<std::string> pathToList(const char *path)
  68. {
  69. std::vector<std::string> result;
  70. const char *begin = path;
  71. const char *index = path;
  72. while ( *index )
  73. {
  74. for( ; *index != 0 && *index != '/'; index++);
  75. if (begin != index)
  76. result.push_back( std::string(begin, index - begin ) );
  77. for(; *index != 0 && *index == '/'; index++); // if several delimeters then skip them all
  78. begin = index;
  79. }
  80. return result;
  81. }
  82. const TGeoShape* getShape(const TGeoManager *g, const char* name)
  83. {
  84. if (! g)
  85. {
  86. Error("getShape", "Geo Manager not initialized");
  87. return 0;
  88. }
  89. TGeoVolume *vol = gGeoManager->GetVolume(name);
  90. if (! vol)
  91. {
  92. Error("getShape", "Volume %s not found", name);
  93. return 0;
  94. }
  95. return vol->GetShape();
  96. }
  97. //Suffixed versions of CreateHistograms...
  98. TH1F* CreateHistogram1(const char* name, const std::string &suffix, const char *xtitle, int nbin,
  99. Float_t firstBin, Float_t lastBin)
  100. {
  101. std::string fullName = std::string(name) + suffix;
  102. return CreateHistogram1(fullName.c_str(), xtitle, nbin, firstBin, lastBin);
  103. }
  104. TH2F* CreateHistogram2(const char* name, const std::string &suffix, const char* xtitle, const char* ytitle,
  105. int nbinx, Float_t firstBinX, Float_t lastBinX, int nbiny, Float_t firstBinY, Float_t lastBinY)
  106. {
  107. std::string fullName = std::string(name) + suffix;
  108. return CreateHistogram2(fullName.c_str(), xtitle, ytitle, nbinx, firstBinX, lastBinX, nbiny, firstBinY, lastBinY);
  109. }
  110. TH3F* CreateHistogram3(const char* name, const std::string &suffix, const char* xtitle, const char* ytitle,
  111. const char* ztitle,
  112. int nbinx, Float_t firstBinX, Float_t lastBinX, int nbiny, Float_t firstBinY, Float_t lastBinY,
  113. int nbinz, Float_t firstBinZ, Float_t lastBinZ)
  114. {
  115. std::string fullName = std::string(name) + suffix;
  116. return CreateHistogram3(fullName.c_str(), xtitle, ytitle, ztitle, nbinx, firstBinX, lastBinX, nbiny, firstBinY, lastBinY,
  117. nbinz, firstBinZ, lastBinZ);
  118. }