Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 316 | f9daq | 1 | #include <stdlib.h> |
| 2 | #include <stdio.h> |
||
| 3 | #include <TSystem.h> |
||
| 4 | #include <TSystemFile.h> |
||
| 5 | #include <TString.h> |
||
| 6 | #include <TSystemDirectory.h> |
||
| 7 | #include <TList.h> |
||
| 8 | #include <TH1F.h> |
||
| 9 | #include <TH2F.h> |
||
| 10 | #include <TGraph.h> |
||
| 11 | #include <TTree.h> |
||
| 12 | #include <TFile.h> |
||
| 13 | #include <TCanvas.h> |
||
| 14 | #include <iostream> |
||
| 15 | |||
| 16 | void ana(TString fname); |
||
| 17 | |||
| 18 | void processRaw(TString fname){ |
||
| 19 | TString troot= fname + "_x0_y0.root"; |
||
| 20 | if(!gSystem->AccessPathName(troot)){ |
||
| 21 | cout << "ROOT File exists " << troot << endl; |
||
| 22 | } else { |
||
| 23 | cout << "Processing. " << troot << endl; |
||
| 24 | ana(fname); |
||
| 25 | } |
||
| 26 | } |
||
| 27 | |||
| 28 | class mytree { |
||
| 29 | private: |
||
| 30 | TH2F hdpo[4]; |
||
| 31 | TH1F hadc[4]; |
||
| 32 | TH1F htdc[4]; |
||
| 33 | TGraph hgr; |
||
| 34 | TTree *tree; |
||
| 35 | TFile *hfile; |
||
| 36 | |||
| 37 | int voltage; |
||
| 38 | int temperature; |
||
| 39 | public: |
||
| 40 | mytree( ); |
||
| 41 | ~mytree(); |
||
| 42 | int init(TString, TDirectory *); |
||
| 43 | int event(TString); |
||
| 44 | TString id; |
||
| 45 | TString sn; |
||
| 46 | }; |
||
| 47 | |||
| 48 | |||
| 49 | |||
| 50 | |||
| 51 | mytree *ptree; |
||
| 52 | int mytree::init(TString fname, TDirectory *dir) |
||
| 53 | { |
||
| 54 | cerr << "init " << fname << endl; |
||
| 55 | |||
| 56 | if(!gSystem->AccessPathName(fname)){ |
||
| 57 | // cout << fname <<" exists. Remove it" << endl; |
||
| 58 | // return -1; |
||
| 59 | } |
||
| 60 | if (hfile) return -1; |
||
| 61 | |||
| 62 | hfile = new TFile (fname,"RECREATE","ROOT FILE with SiPM qdc and tdf"); |
||
| 63 | |||
| 64 | tree = new TTree("tree","sipm data tree"); |
||
| 65 | |||
| 66 | tree->Branch("voltage",&voltage,"voltage/I"); |
||
| 67 | tree->Branch("temp",&temperature,"temperature/I"); |
||
| 68 | tree->Branch("id","TString",&id); |
||
| 69 | tree->Branch("sn","TString",&sn); |
||
| 70 | tree->Branch("dpo2","TH2F",&hdpo[2]); |
||
| 71 | tree->Branch("dpo3","TH2F",&hdpo[3]); |
||
| 72 | tree->Branch("qdc2","TH1F",&hadc[2]); |
||
| 73 | tree->Branch("qdc3","TH1F",&hadc[3]); |
||
| 74 | tree->Branch("tdc2","TH1F",&htdc[2]); |
||
| 75 | tree->Branch("tdc3","TH1F",&htdc[3]); |
||
| 76 | tree->Branch("thscan","TGraph",&hgr, 32000, 0); |
||
| 77 | |||
| 78 | return 0; |
||
| 79 | |||
| 80 | } |
||
| 81 | |||
| 82 | |||
| 83 | |||
| 84 | TObject * getHisto(TDirectory *f,TString cname, TString hname , int subpad){ |
||
| 85 | |||
| 86 | auto c=(TCanvas *) f->Get(cname); |
||
| 87 | if(!c) return NULL; |
||
| 88 | auto p=(TPad *) c->GetPrimitive(cname + Form("_%d",subpad+1)); |
||
| 89 | if(!p) return NULL; |
||
| 90 | //p->ls(); |
||
| 91 | auto o= (TObject *) p->GetPrimitive(hname + Form("_%02d",subpad)); |
||
| 92 | if (!o) return NULL; |
||
| 93 | auto o1=o->Clone(); |
||
| 94 | |||
| 95 | delete p; |
||
| 96 | delete c; |
||
| 97 | o1->ls(); |
||
| 98 | return o1; |
||
| 99 | } |
||
| 100 | |||
| 101 | int mytree::event(TString fname){ |
||
| 102 | |||
| 103 | TFile *f= new TFile(fname + "_x0_y0.root"); |
||
| 104 | TString bname=gSystem->BaseName(fname.Data()); |
||
| 105 | TString pname=gSystem->DirName(gSystem->DirName(fname.Data())); |
||
| 106 | sn =gSystem->BaseName(gSystem->DirName(fname.Data())); |
||
| 107 | TObjArray *ppname = pname.Tokenize("_"); |
||
| 108 | id = ((TObjString *)(ppname->At(1)))->String(); |
||
| 109 | |||
| 110 | if (!hfile || !tree) init("sipm.root",f); |
||
| 111 | |||
| 112 | TObjArray *t = bname.Tokenize("_."); |
||
| 113 | t->Print(); |
||
| 114 | for (Int_t i = 0; i < t->GetEntries(); i++) |
||
| 115 | cout << ((TObjString *)(t->At(i)))->String() << endl; |
||
| 116 | voltage = (((TObjString *)(t->At(2)))->String()).Atof(); |
||
| 117 | temperature = (((TObjString *)(t->At(1)))->String()).Atof(); |
||
| 118 | |||
| 119 | cout << pname << " " << voltage << " " << temperature << endl; |
||
| 120 | for (int i=0;i<4;i++){ |
||
| 121 | |||
| 122 | TH1F* h0=(TH1F *)getHisto(f,"c_adc","qdc",i); |
||
| 123 | TH1F* h1=(TH1F *)getHisto(f,"c_cftdif","cftdif",i); |
||
| 124 | TH2F* h2=(TH2F *)getHisto(f,"c_dpo","dpo",i); |
||
| 125 | |||
| 126 | hadc[i]=(h0)?*h0:TH1F(); |
||
| 127 | htdc[i]=(h1)?*h1:TH1F(); |
||
| 128 | hdpo[i]=(h2)?*h2:TH2F(); |
||
| 129 | |||
| 130 | } |
||
| 131 | f->Close(); |
||
| 132 | delete f; |
||
| 133 | auto gr= new TGraph(fname + ".thr"); |
||
| 134 | hgr = (gr)?*gr:TGraph(); |
||
| 135 | TString grname= id + "_" + sn + Form("_%d_%d", temperature, voltage); |
||
| 136 | hgr.SetName(grname); |
||
| 137 | hgr.ls(); |
||
| 138 | |||
| 139 | if (tree) tree->Fill(); |
||
| 140 | else cerr <<"Error No tree" << endl; |
||
| 141 | |||
| 142 | |||
| 143 | |||
| 144 | |||
| 145 | return 0; |
||
| 146 | |||
| 147 | |||
| 148 | } |
||
| 149 | |||
| 150 | mytree::mytree(){ |
||
| 151 | |||
| 152 | hfile = NULL; |
||
| 153 | } |
||
| 154 | mytree::~mytree(){ |
||
| 155 | |||
| 156 | |||
| 157 | // End of the loop |
||
| 158 | tree->Print(); |
||
| 159 | // Save all objects in this file |
||
| 160 | hfile->Write(); |
||
| 161 | // Close the file. Note that this is automatically done when you leave |
||
| 162 | // the application. |
||
| 163 | hfile->Close(); |
||
| 164 | |||
| 165 | } |
||
| 166 | |||
| 167 | void process_results( TString dirname=".", TString ext=".dat", int mode=0 , int level=0) { |
||
| 168 | TSystemDirectory dir(dirname, dirname); |
||
| 169 | TList *files = dir.GetListOfFiles(); |
||
| 170 | |||
| 171 | if (mode && level==0){ |
||
| 172 | ptree= new mytree(); |
||
| 173 | } |
||
| 174 | if (files) { |
||
| 175 | TSystemFile *file; |
||
| 176 | TString fname; |
||
| 177 | TIter next(files); |
||
| 178 | while ((file=(TSystemFile*)next())) { |
||
| 179 | fname = file->GetName(); |
||
| 180 | cout << "*" << fname.Data() << endl; |
||
| 181 | if (file->IsDirectory()){ |
||
| 182 | cout << "DIR:" << fname.Data() << endl; |
||
| 183 | if ( !fname.EqualTo(".") && !fname.EqualTo("..")){ |
||
| 184 | process_results(dirname + TString("/") + fname , ext, mode, level+1); |
||
| 185 | } |
||
| 186 | } else { |
||
| 187 | cout << "FILE:" << fname.Data() << endl; |
||
| 188 | if ( fname.EndsWith(ext) ){ |
||
| 189 | TSubString k = fname.SubString("run"); |
||
| 190 | TString kk(k); |
||
| 191 | if( kk.Length()>0 ) { |
||
| 192 | TString name = dirname + TString("/") + fname; |
||
| 193 | cout << "#######" << fname.Data() << " " << name << endl; |
||
| 194 | |||
| 195 | if (mode) ptree->event(name); |
||
| 196 | else processRaw(name); |
||
| 197 | |||
| 198 | } |
||
| 199 | } |
||
| 200 | } |
||
| 201 | } |
||
| 202 | |||
| 203 | } |
||
| 204 | if (level==0 && ptree) delete ptree; |
||
| 205 | } |