Logo coherent WaveBurst  
Library Reference Guide
Logo
cwb_fix_live_slag_missed.C
Go to the documentation of this file.
1 /*
2 # Copyright (C) 2019 Gabriele Vedovato
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17 
18 
19 {
20  // this fix must be used if live tree has been produced without slag leaf
21 
22  #include <vector>
23 
25 
26  TB.checkFile(gSystem->Getenv("CWB_ROOTLOGON_FILE"));
27  TB.checkFile(gSystem->Getenv("CWB_PARAMETERS_FILE"));
28  TB.checkFile(gSystem->Getenv("CWB_UPARAMETERS_FILE"));
29 
31  if(gSystem->Getenv("CWB_MERGE_LABEL")==NULL) {
32  cout << "Error : environment CWB_MERGE_LABEL is not defined!!!" << endl;exit(1);
33  } else {
34  cwb_merge_label=TString(gSystem->Getenv("CWB_MERGE_LABEL"));
35  }
36 
37  char net_file_name[256];
38  char liv_file_name[256];
39 
40  if(simulation) {
41  cout << "Error : cwb_fix_slag_missed.C must be applied only to production files !!!" << endl;exit(1);
42  } else {
43  sprintf(net_file_name,"%s/wave_%s.%s.root",merge_dir,data_label,cwb_merge_label.Data());
44  sprintf(liv_file_name,"%s/live_%s.%s.root",merge_dir,data_label,cwb_merge_label.Data());
45  cout << net_file_name << endl;
46  cout << liv_file_name << endl;
47 
48  // Check if files exist
49  TB.checkFile(net_file_name);
50  TB.checkFile(liv_file_name);
51  }
52 
53  // open wave tree
54  TFile *fwave = TFile::Open(net_file_name);
55  if(fwave==NULL) {cout << "Error opening file " << net_file_name << endl;exit(1);}
56  TTree* twave = (TTree *) gROOT->FindObject("waveburst");
57  if(twave==NULL) {cout << "Error opening tree wave" << endl;exit(1);}
58 
59  // check if slag is presents in wave tree
60  TBranch* branch;
61  bool net_slag=false;
62  TIter next(twave->GetListOfBranches());
63  while ((branch=(TBranch*)next())) {
64  if (TString("slag").CompareTo(branch->GetName())==0) net_slag=true;
65  }
66  next.Reset();
67  if(!net_slag) {cout << "File " << net_file_name << " not contains slag leaf, apply cwb_fix_slag_missed.C " << endl;exit(1);}
68 
69  // open live tree
70  TFile *flive = TFile::Open(liv_file_name);
71  if(flive==NULL) {cout << "Error opening file " << liv_file_name << endl;exit(1);}
72  TTree* tlive = (TTree *) gROOT->FindObject("liveTime");
73  if(tlive==NULL) {cout << "Error opening tree livetime" << endl;exit(1);}
74 
75  // check if slag is presents in live tree
76  TBranch* branch;
77  bool liv_slag=false;
78  TIter next(tlive->GetListOfBranches());
79  while ((branch=(TBranch*)next())) {
80  if (TString("slag").CompareTo(branch->GetName())==0) liv_slag=true;
81  }
82  next.Reset();
83 
84  bool fixit=false;
85  char answer[256];
86  strcpy(answer,"");
87  do {
88  if (!liv_slag) {
89  cout << "File " << liv_file_name << " do not contains slag leaf" << endl;
90  cout << "Do you want to fix it ? (y/n) ";
91  } else {
92  cout << "File " << liv_file_name << " already contains slag leaf" << endl;
93  cout << "Do you want to fix it anyway ? (y/n) ";
94  }
95  cin >> answer;
96  cout << endl << endl;
97  } while ((strcmp(answer,"y")!=0)&&(strcmp(answer,"n")!=0));
98  if (strcmp(answer,"y")==0) fixit=true;
99  if(!fixit) exit(0);
100 
101  vector<waveSegment> cat1List=TB.readSegList(nDQF, DQF, CWB_CAT1);
102  int slagSegs=TB.getSlagJobList(cat1List, segLen).size();
103  vector<slag> slagList=TB.getSlagList(nIFO, slagSize, slagSegs, slagOff, slagMin, slagMax, slagSite);
104 
105  int nIFO = slagList[0].segId.size();
106 
107  int* ind = new int[slagList.size()+1];
108  for(int j=0; j<slagList.size(); j++) {
109 // slagList[j].jobId; // jobID
110 // slagList[j].slagId[0]; // slagID
111 // slagList[j].segId[0]; // segID
112  ind[slagList[j].jobId] = j;
113 //cout << j << " " << slagList[j].jobId << " " << slagList[j].slagId[0] << endl;
114  }
115 
116  // -------------------------------------------------------------------------
117  // create slag fix live root file
118  // -------------------------------------------------------------------------
119 
120  TString fix_liv_file_name(liv_file_name);
121  fix_liv_file_name.ReplaceAll(".root",".FIX.root");
122  cout << "Fixed live file : " << fix_liv_file_name.Data() << endl;
123 
124  TFile* flive_fix = new TFile(fix_liv_file_name,"RECREATE");
125  if (flive_fix->IsZombie()) {
126  cout << "CWB::Toolbox::setVeto - Error opening file " << fix_liv_file_name.Data() << endl;
127  exit(1);
128  }
129  TTree *tlive_fix = (TTree*)tlive->CloneTree(0);
130  tlive_fix->SetMaxTreeSize(5000000000);
131  // add slag leaf
132  float xslag[6];
133  if (!liv_slag) {
134  tlive_fix->Branch("slag",xslag,"slag[6]/F");
135  tlive_fix->Write();
136  } else {
137  tlive_fix->SetBranchAddress("slag",xslag);
138  }
139 
140  Int_t xrun;
141  tlive->SetBranchAddress("run",&xrun);
142  int ntrg = tlive->GetEntries();
143  for(int i=0; i<ntrg; i++) {
144  if(i%100000==0) cout << i << "/" << ntrg << endl;
145  tlive->GetEntry(i);
146  int j = ind[xrun];
147  for (int n=0;n<nIFO;n++) xslag[n] = segLen*(slagList[j].segId[n]-slagList[j].segId[0]);
148  xslag[nIFO] = slagList[j].slagId[0];
149  for (int n=nIFO+1;n<6;n++) xslag[n] = -1;
150  tlive_fix->Fill();
151  }
152 
153  tlive_fix->Write();
154  flive_fix->Close();
155 
156  delete [] ind;
157 
158  // -------------------------------------------------------------------------
159  // create link slag fix wave root file
160  // -------------------------------------------------------------------------
161 
162  char cmd[256];
163  TString fix_net_file_name(net_file_name);
164  fix_net_file_name.ReplaceAll(".root",".FIX.root");
165  cout << "Fixed wave file : " << fix_net_file_name.Data() << endl;
166  sprintf(cmd,"cp --remove-destination %s %s",net_file_name,fix_net_file_name.Data());
167  //sprintf(cmd,"ln -sf %s %s",net_file_name,fix_net_file_name.Data());
168  cout << cmd << endl;
169  gSystem->Exec(cmd);
170 
171  exit(0);
172 }
TString fix_net_file_name(net_file_name)
int slagSize
Definition: test_config1.C:65
size_t * slagSite
Definition: test_config1.C:69
int slagOff
Definition: test_config1.C:68
sprintf(liv_file_name,"%s/live_%s.%s.root", merge_dir, data_label, cwb_merge_label.Data())
int n
Definition: cwb_net.C:28
TString("c")
float xslag[6]
int slagMax
Definition: test_config1.C:67
TString fix_liv_file_name(liv_file_name)
CWB::Toolbox TB
bool liv_slag
TFile * flive
int j
Definition: cwb_net.C:28
i drho i
static bool checkFile(TString fName, bool question=false, TString message="")
Definition: Toolbox.cc:4670
char cmd[256]
nDQF
Definition: cwb_eced.C:109
char data_label[512]
Definition: test_config1.C:160
dqfile DQF[12]
Definition: test_config1.C:171
exit(0)
bool net_slag
static vector< waveSegment > getSlagJobList(vector< waveSegment > ilist, int seglen=600)
Definition: Toolbox.cc:1791
TIter next(twave->GetListOfBranches())
segLen
Definition: cwb_eced.C:24
char merge_dir[512]
Definition: test_config1.C:147
vector< slag > slagList
cout<< endl<< endl;} while((strcmp(answer,"y")!=0)&&(strcmp(answer,"n")!=0));if(strcmp(answer,"y")==0) fixit=true;if(!fixit) exit(0);vector< waveSegment > cat1List
char net_file_name[256]
char liv_file_name[256]
static vector< slag > getSlagList(size_t nIFO, size_t slagSize, int slagSegs, int slagOff, size_t slagMin, size_t slagMax, size_t *slagSite, char *slagFile=NULL)
Definition: Toolbox.cc:809
static vector< waveSegment > readSegList(dqfile DQF)
Definition: Toolbox.cc:409
char answer[256]
TTree * tlive
TBranch * branch
TString cwb_merge_label
int slagMin
Definition: test_config1.C:66
simulation
Definition: cwb_eced.C:26
strcpy(answer,"")