StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
LoadLaserDriftVelocityToDb.C
1 //
2 // LoadLaserDriftVelocityToDb.C
3 //
4 // Modified 31 Jan 2008 by G. Van Buren
5 // Account for case of single macro
6 // output of LoopOverLaserTrees.C
7 //
8 
9 #include <iostream>
10 #include "TH1.h"
11 #include "TH1F.h"
12 #include "TH2F.h"
13 #include "TH3F.h"
14 //#include "TPad.h"
15 //#include "TVirtualPad.h"
16 //#include "TCanvas.h"
17 //#include "TText.h"
18 #include "TF1.h"
19 #include "TNtuple.h"
20 #include "TFile.h"
21 #include "TGraph.h"
22 #include "TGraphErrors.h"
23 #include "TSystem.h"
24 #include "TStyle.h"
25 #include "TMath.h"
26 #include "TProfile.h"
27 //#include "TPostScript.h"
28 //#include "TPaveLabel.h"
29 //#include "tpcDriftVelocity.h"
30 //#include "tables/St_tpcDriftVelocity_Table.h"
31 //#include "tpcDriftVelocity_st.h"
32 //#include "StDbManager.hh"
33 //#include "StDbLib/StDbTable.h"
34 
35 int getDVInRun(int nInRun, int *runMacro, unsigned int *runTime, double *runDVE){
36 
37  const double maxRMS = 0.001;
38  const int minFilesInRun=5;
39 
40  // Remove last time entry as it is usually bad, few entries
41  // Will order by DVE value
42  int nToUseInRun = nInRun-1;
43  int iFirstToUseInRun=0;
44  int *runDVEOrderI = new int[nToUseInRun];
45  double *runDVEOrderV = new double[nToUseInRun];
46 
47  for(int iInRun=0; iInRun<nToUseInRun; iInRun++){
48  runDVEOrderV[iInRun]=runDVE[iInRun];
49  runDVEOrderI[iInRun]=iInRun;
50  }
51 
52  // Ordering by DVE value
53  bool bOrder = kFALSE;
54  while(!bOrder){
55  bOrder = kTRUE;
56  for(int iInRun=0; iInRun<nToUseInRun-1; iInRun++){
57  if(runDVEOrderV[iInRun]>runDVEOrderV[iInRun+1]){
58  int tempMinI = runDVEOrderI[iInRun+1];
59  double tempMinV = runDVEOrderV[iInRun+1];
60  runDVEOrderI[iInRun+1]=runDVEOrderI[iInRun];
61  runDVEOrderI[iInRun]=tempMinI;
62  runDVEOrderV[iInRun+1]=runDVEOrderV[iInRun];
63  runDVEOrderV[iInRun]=tempMinV;
64  bOrder=kFALSE;
65  }
66  }
67  }
68 
69  for(int iInRun=0; iInRun<nInRun; iInRun++){
70  cout << "In run: " << iInRun << "/" << nInRun
71  << " " << runMacro[iInRun]
72  << " " << runTime[iInRun]
73  << " " << runDVE[iInRun] << endl;
74  }
75 
76  for(int iInRun=0; iInRun<nToUseInRun; iInRun++){
77  cout << "Order: " << iInRun << "/" << nToUseInRun
78  << " " << runDVEOrderV[iInRun]
79  << " " << runDVEOrderI[iInRun] << endl;
80  }
81 
82  int nInOrderU = 0;
83  int nInOrderD = 0;
84  for(int iInRun=0; iInRun<nToUseInRun; iInRun++){
85  if(runDVEOrderI[iInRun]==iInRun){
86  nInOrderU++;
87  }
88  if(runDVEOrderI[iInRun]==nToUseInRun-iInRun-1){
89  nInOrderD++;
90  }
91  }
92 
93  if(nToUseInRun>=minFilesInRun-1){
94  if(nInOrderU==nToUseInRun || nInOrderD==nToUseInRun){
95  double *meanDiff = new double[nToUseInRun-2];
96  if(nInOrderU==nToUseInRun){
97  cout << "Drift Velocities East are ordered (U) in time. Check convergence ..." << endl;
98  for(int i=0; i<nToUseInRun-2; i++){
99  meanDiff[i]=0.;
100  for(int j=i; j<nToUseInRun-1; j++){
101  meanDiff[i]+=(runDVEOrderV[j+1]-runDVEOrderV[j]);
102  }
103  meanDiff[i]/=(nToUseInRun-i-1);
104  }
105  }
106  if(nInOrderD==nToUseInRun){
107  cout << "Drift Velocities East are ordered (D) in time. Check convergence ..." << endl;
108  for(int i=0; i<nToUseInRun-2; i++){
109  meanDiff[i]=0.;
110  for(int j=nToUseInRun-i-1; j>0; j--){
111  meanDiff[i]+=runDVEOrderV[j-1]-runDVEOrderV[j];
112  }
113  meanDiff[i]/=(nToUseInRun-i-1);
114  }
115  }
116  int nDiffInOrder = 0;
117  for(int i=0; i<nToUseInRun-3; i++){
118  if(meanDiff[i]>meanDiff[i+1]){
119  nDiffInOrder++;
120  }
121  }
122  if(nDiffInOrder>=nToUseInRun-3-1){
123  if(nInOrderU==nToUseInRun) iFirstToUseInRun++;
124  if(nInOrderD==nToUseInRun) nToUseInRun--;
125  cout << "Convergence found! Will remove earliest entry and continue." << endl;
126  }
127  else {
128  cout << "No convergence found! This run should be checked. No macro will be kept!" << endl;
129  return -1;
130  }
131  }
132  }
133 
134  int tryThreeRMS = 0;
135  int iNotValidRMS1 = -1;
136  int iNotValidRMS2 = -1;
137 
138  if(nToUseInRun==minFilesInRun-2){
139  tryThreeRMS=2; // Only one chance
140  }
141 
142  while(tryThreeRMS<3){
143  double runMean = 0.;
144  double runRMS = 0.;
145  int nRMS = 0;
146 
147  for(int iInRun=iFirstToUseInRun; iInRun<nToUseInRun; iInRun++){
148  if(iInRun!=iNotValidRMS1 && iInRun!=iNotValidRMS2){
149  runMean+=runDVEOrderV[iInRun];
150  runRMS+=runDVEOrderV[iInRun]*runDVEOrderV[iInRun];
151  nRMS++;
152  }
153  }
154  runMean/=nRMS;
155  runRMS/=nRMS;
156  runRMS-=runMean*runMean;
157  runRMS=TMath::Sqrt(runRMS);
158 
159  cout << "This run RMS = " << runRMS << endl;
160 
161  int tryTwiceLow = 0;
162  int iNotValidLow = -1;
163 
164  if(runRMS<maxRMS){
165  tryTwiceLow = 0;
166  iNotValidLow = -1;
167  while(tryTwiceLow<2){
168  int iMeanOrder = 0;
169  double diffToMean = 100.;
170  for(int iInRun=iFirstToUseInRun; iInRun<nToUseInRun; iInRun++){
171  double lDiffToMean = TMath::Abs(runDVEOrderV[iInRun]-runMean);
172  if(lDiffToMean<diffToMean && iInRun!=iNotValidLow &&
173  iInRun!=iNotValidRMS1 && iInRun!=iNotValidRMS2){
174  diffToMean=lDiffToMean;
175  iMeanOrder=iInRun;
176  }
177  }
178  int lowThird = nInRun/3;
179  if(runDVEOrderI[iMeanOrder]/lowThird!=0 || (tryTwiceLow==1 && tryThreeRMS==2)){
180  int iKeepMacro = runMacro[runDVEOrderI[iMeanOrder]];
181  cout << "Keep Macro: " << iKeepMacro << " " << runTime[runDVEOrderI[iMeanOrder]]
182  << " " << runDVE[runDVEOrderI[iMeanOrder]] << endl;
183  cout << runDVEOrderI[iMeanOrder] << "/" << nInRun << " " << iMeanOrder << endl;
184 
185  return iKeepMacro;
186  }
187 
188  tryTwiceLow++;
189  iNotValidLow=iMeanOrder;
190  cout << "Drift Velocity East value closer to mean was found in the first third of the run, is safer to try next closest value." << endl;
191  cout << "tryTwiceLow= " << tryTwiceLow << " iNotValidLow= " << iNotValidLow << endl;
192  }
193  }
194 
195  int iFarOrder = 0;
196  double diffToMean = 0.;
197  for(int iInRun=iFirstToUseInRun; iInRun<nToUseInRun; iInRun++){
198  double lDiffToMean = TMath::Abs(runDVEOrderV[iInRun]-runMean);
199  if(lDiffToMean>diffToMean && iInRun!=iNotValidRMS1 && iInRun!=iNotValidRMS2){
200  diffToMean=lDiffToMean;
201  iFarOrder=iInRun;
202  }
203  }
204  tryThreeRMS++;
205  if (tryThreeRMS<2){
206  iNotValidRMS1=iFarOrder;
207  if (tryTwiceLow==2)
208  cout << "Will remove Further most value and try again." << endl;
209  else
210  cout << "RMS of Drift Velocity East is too large! Will remove Further most value." << endl;
211  cout << "RMS " << runRMS << " > " << maxRMS << endl;
212  cout << "tryThreeRMS= " << tryThreeRMS << " iNotValidRMS1= " << iNotValidRMS1 << endl;
213  }
214  else{
215  iNotValidRMS2=iFarOrder;
216  if (tryTwiceLow==2)
217  cout << "Will remove Further most value and try again." << endl;
218  else
219  cout << "RMS of Drift Velocity East is too large! Will remove Further most value." << endl;
220  cout << "RMS " << runRMS << " > " << maxRMS << endl;
221  cout << "tryThreeRMS= " << tryThreeRMS << " iNotValidRMS2= " << iNotValidRMS2 << endl;
222  }
223  }
224 
225  cout << "Couldn't find a good macro to be kept for this run! Run should be checked!" << endl;
226  return -1;
227 
228 }
229 
230 int getDriftVelocityDB(unsigned int funixTime, unsigned int& uitimedb, double& ldvedb ){
231 
232  char *fDbName = "Calibrations_tpc";
233  char *fTableName = "tpcDriftVelocity";
234  char *fFlavorName = "laserDV"; // flavor name, like 'ofl', 'sim'
235  char *fTimestamp = "2004-10-31 00:00:00"; // Timestamp of the data requested
236  // unsigned int funixTime; // unix timestamp
237 
238  StDbManager* mgr = StDbManager::Instance(); // Get the singleton manager
239 
240  StDbConfigNode* configNode = mgr->initConfig(fDbName); // Connect to the db & get an empty container
241 
242  StDbTable* dbtable;
243  dbtable = configNode->addDbTable(fTableName);
244  // Add a table to the container with descriptor given by Database with wsing version name as "default"
245 
246  if (dbtable == 0){ // If table asigned by fTableName does not exist in Dababase
247  cout << " No Table : " << fTableName << endl; // program is stoped and exit from this function.
248  return 0;
249  }
250 
251  if (fFlavorName != 0){
252  dbtable -> setFlavor(fFlavorName);
253  cout << "Flavor is set as " << fFlavorName << " by StDbTable::setFlavor." << endl;
254  }else{
255  cout << "Flavor is NOT assigned. Default value is set as 'ofl'. " << endl;
256  dbtable -> setFlavor("ofl");
257  }
258 
259  if(funixTime) {
260  mgr->setRequestTime(funixTime);
261  } else {
262  mgr->setRequestTime(fTimestamp);
263  }
264 
265  mgr->fetchDbTable(dbtable); // Fetch the data from Database
266  void* cstruct = dbtable->GetTableCpy(); // Get pointer of table and copy to c-structure
267  Int_t nrows = dbtable->GetNRows(); // Get number of raws in the table
268 
269  St_tpcDriftVelocity * tpcDV =
270  (St_tpcDriftVelocity*)TTable::New(fTableName,fTableName,cstruct,nrows);
271 
272  if(!(tpcDV && tpcDV->HasData())) {
273  cout << "No TTable returned!" << endl;
274  return 0;
275  }
276  tpcDriftVelocity_st *ltpcDVs = (tpcDriftVelocity_st*)tpcDV->At(0);
277 
278  uitimedb = dbtable->getBeginTime();
279  ldvedb = ltpcDVs->laserDriftVelocityEast;
280 
281  return 1;
282 }
283 
284 
285 //void ReadLaserDVListAuto(const char* listOfMacros, int nMacros){
286 //void LoadLaserDriftVelocityToDb(const char* listOfMacros){
287 //void LoadLaserDriftVelocityToDb(){
288 //void LoadLaserDriftVelocityToDb(const char* dirName, const char* listOfMacros, int nMacros, const char* baseName){
289 
290 void LoadLaserDriftVelocityToDb(const char* dirName, const char* listOfMacros, const char* baseName, int mode=0){
291  //
292  // Define some used values.
293  //
294 
295  // mode = 0 is the method of taking several macros and selecting a best one
296  // mode = 1 uses just one macro to upload to DB
297 
298  // const char* listOfMacros = "listOfLaserMacrosForTest.list";
299  // const int nMacros = 109;
300  // const char* baesseName = "tpcDriftVelocity";
301  // const char* dirName = "dataLaserDVTestAuto";
302  const int nMacrosMax = 800;
303  int nMacros = 0;
304 
305  TString fullListName(dirName);
306  fullListName +="/";
307  fullListName +=listOfMacros;
308 
309  const char* tmpLines = "/tmp/nLines.txt";
310  TString myCount("/usr/bin/wc -l ");
311  myCount += fullListName;
312  myCount += " > ";
313  myCount += tmpLines;
314 
315 
316  // Prepare to read from the DB
317  //gSystem->Setenv("DB_ACCESS_MODE","read");
318 
319  cout << "Executing: " << myCount.Data() << endl;
320 
321  gSystem->Exec(myCount.Data());
322  ifstream dvc(tmpLines);
323  dvc >> nMacros;
324  TString myRm("/bin/rm ");
325  myRm += tmpLines;
326  gSystem->Exec(myRm.Data());
327 
328  cout << "Number of macros in list: " << nMacros << endl;
329  nMacros = TMath::Min(nMacros,nMacrosMax);
330  cout << "Number of macros to read: " << nMacros << endl;
331 
332  const double maxdDVdt = 3*0.000002;
333 
334  // macro to add a bunch of data from a list of *.C files
335  //
336  //-> not sure which to load so load a bunch of shared libs
337  //
338  gSystem->Load("St_base"); // Standard Libraries
339  gSystem->Load("StUtilities");
340  gSystem->Load("StarClassLibrary");
341  gSystem->Load("St_Tables");
342  gSystem->Load("StDbLib"); // DB Libraries
343 
344  // Create tables to store unix time and dv values
345  double *xLaserDVE = new double[nMacros];
346  double *yLaserDVE = new double[nMacros];
347 
348  double *xLaserDVW = new double[nMacros];
349  double *yLaserDVW = new double[nMacros];
350 
351  int *macroStatus = new int[nMacros];
352 
353  char nameMacro[100];
354  TString fullNameMacro;
355  unsigned int uidatetime;
356 
357  St_tpcDriftVelocity *tpcDV;
358  tpcDriftVelocity_st *tpcDVs;
359 
360  FILE *theList;
361  theList = fopen(fullListName.Data(),"r");
362 
363  // To Get Unix Time from DateTime
365 
366 
367  const int maxDeltaT=120; // Time 2 separate 2 runs
368  const int maxFilesInRun=30; // Maximum number of files in run
369  const int minFilesInRun=5; // Minimum number of files in run
370 
371  int nInRun=0;
372  int nMacroToKeep=0;
373  int nTryToKeep=0;
374  unsigned int lastTime=0;
375  int *macroToKeep = new int[nMacros];
376  int *runMacro = new int[maxFilesInRun];
377  unsigned int *runTime = new unsigned int[maxFilesInRun];
378  double *runDVE = new double[maxFilesInRun];
379 
380  // Loop over macros
381  for(int iMacro=0; iMacro<nMacros; iMacro++){
382  if (!feof(theList)){
383  cout << "\n" << "iMacro = " << iMacro << endl;
384  macroStatus[iMacro]=6; // Base status
385  fscanf(theList,"%s",&nameMacro);
386  if(strstr(nameMacro,baseName)){
387  fullNameMacro = TString(dirName);
388  fullNameMacro+="/";
389  fullNameMacro+=nameMacro;
390  printf("Reading macro: %s\n",fullNameMacro.Data());
391 
392  char* sdatetime=strstr(nameMacro,"."); sdatetime++;
393  TString tsdatetime(sdatetime);
394  tsdatetime = tsdatetime.ReplaceAll(".C","");
395  tsdatetime = tsdatetime.ReplaceAll("."," ");
396  tsdatetime = tsdatetime.Insert(4,"-");
397  tsdatetime = tsdatetime.Insert(7,"-");
398  tsdatetime = tsdatetime.Insert(13,":");
399  tsdatetime = tsdatetime.Insert(16,":");
400  sdatetime = tsdatetime.Data();
401 
402  mgr->setRequestTime(sdatetime);
403  uidatetime = mgr->getUnixRequestTime();
404  cout << " " << sdatetime << " " << uidatetime << endl;
405  if (uidatetime == 0){
406  cout << " Macro error " << endl;
407  return;
408  }
409 
410  // Load Macro to get dv value
411  gROOT->LoadMacro(fullNameMacro.Data());
412  tpcDV = (St_tpcDriftVelocity*)CreateTable();
413  if(tpcDV && tpcDV->HasData()) {
414  cout << " Macro succesfully loaded" << endl;
415 
416  tpcDVs = (tpcDriftVelocity_st*)tpcDV->At(0);
417 
418  xLaserDVE[iMacro]=uidatetime;
419  yLaserDVE[iMacro]=tpcDVs->laserDriftVelocityEast;
420 
421  xLaserDVW[iMacro]=uidatetime;
422  yLaserDVW[iMacro]=tpcDVs->laserDriftVelocityWest;
423 
424  cout << " Laser Drift Velocity East: " <<tpcDVs->laserDriftVelocityEast << endl;
425 
426  //
427  // Good DV selection starts here
428  //
429 
430  if (mode == 1) {
431  // take all macros
432  macroToKeep[nMacroToKeep++] = iMacro;
433  macroStatus[iMacro] = iMacro;
434  continue;
435  }
436 
437  // Need to select all macros in one laser run
438  // First macro in run
439  if (nInRun==0){ lastTime = uidatetime;}
440 
441  if ((uidatetime-lastTime)<maxDeltaT && nInRun<maxFilesInRun ){
442  // A macro of the current run
443  cout << " Delta time passed & not enough count "
444  << nInRun << " <=> " << maxFilesInRun << " yet" << endl;
445  runMacro[nInRun]=iMacro;
446  runTime[nInRun]=uidatetime;
447  runDVE[nInRun]=tpcDVs->laserDriftVelocityEast;
448 
449  nInRun++;
450  lastTime = uidatetime;
451 
452  } else if (nInRun>=minFilesInRun-1){
453  // Have all macros for run, check if enough
454  nTryToKeep++;
455  int keepIt = getDVInRun(nInRun,runMacro,runTime,runDVE);
456  if (keepIt!=-1) {
457  macroToKeep[nMacroToKeep++] = keepIt; // Keep the right macro
458  for (int iInRun=0; iInRun<nInRun; iInRun++){
459  macroStatus[runMacro[iInRun]]=keepIt; // Set Status to Macro To Keep
460  }
461  }
462  else {
463  for (int iInRun=0; iInRun<nInRun; iInRun++){
464  macroStatus[runMacro[iInRun]]=2; // No good macro to keep found
465  }
466  }
467  // Current macro is the start of next run
468  nInRun=0;
469 
470  runMacro[nInRun]=iMacro;
471  runTime[nInRun]=uidatetime;
472  runDVE[nInRun]=tpcDVs->laserDriftVelocityEast;
473  nInRun++;
474  lastTime = uidatetime;
475 
476  } else {
477  // not enough
478  cout << " Not Enough macros!" << nInRun << " " << minFilesInRun << endl;
479  for (int iInRun=0; iInRun<nInRun; iInRun++){
480  macroStatus[runMacro[iInRun]]=1; // Not Enough Macros in Run
481  runMacro[nInRun]=0;
482  runTime[nInRun]=0;
483  runDVE[nInRun]=0;
484  }
485  // Current macro is the start of next run
486  nInRun=0;
487 
488  runMacro[nInRun]=iMacro;
489  runTime[nInRun]=uidatetime;
490  runDVE[nInRun]=tpcDVs->laserDriftVelocityEast;
491  nInRun++;
492  lastTime = uidatetime;
493  }
494  }
495  }
496  }
497  } // End loop over macros
498 
499 
500  cout << "\n" << "Processing information" << endl;
501  if (mode==0) {
502  if (nInRun>=minFilesInRun-1 || mode==1){ // Have all macros for run, check if enough
503  nTryToKeep++;
504  int keepIt = mode || getDVInRun(nInRun,runMacro,runTime,runDVE);
505  if (keepIt!=-1) {
506  macroToKeep[nMacroToKeep++] = keepIt; // Keep the right macro
507  for (int iInRun=0; iInRun<nInRun; iInRun++){
508  macroStatus[runMacro[iInRun]]=keepIt; // Set Status to Macro To Keep
509  }
510  }
511  else {
512  for (int iInRun=0; iInRun<nInRun; iInRun++){
513  macroStatus[runMacro[iInRun]]=2; // No good macro to keep found
514  }
515  }
516 
517  } else {
518  cout << "We concluded we do not have Enough macros at this stage" << endl;
519  for (int iInRun=0; iInRun<nInRun; iInRun++){
520  macroStatus[runMacro[iInRun]]=1; // Not Enough Macros in Run
521  }
522  }
523 
524  cout << "Selected " << nMacroToKeep << " out of " << nTryToKeep
525  << " runs with enough macros." << endl;
526 
527  } // mode==0
528 
529  TString LoadDone(dirName); LoadDone +="/Load/Done/";
530  TString LoadFailed(dirName); LoadFailed +="/Load/Failed/";
531  TString LoadOthers(dirName); LoadOthers +="/Load/Others/";
532  TString CheckBadRun(dirName); CheckBadRun+="/Check/BadRun/";
533  TString CheckVarSel(dirName); CheckVarSel+="/Check/VarSel/";
534  TString CheckVarOth(dirName); CheckVarOth+="/Check/VarOth/";
535 
536  //
537  // One macro per run have been selected. Check whether each of those make sense.
538  // Ddv/Dt < Max
539  // Need to query db for closest dv value.
540 
541  unsigned int uidatetimedb =0;
542  double laseredvdb =0.;
543 
544  double dDVdt = 0.;
545  double dDV =0.;
546  double dt =0.;
547  int iMacroToKeep = 0;
548 
549  int nMacroKept=0;
550  int *macroKept = new int[nMacros];
551 
552  int nSuccess = 0;
553  int nFailed = 0;
554 
555  if (nMacroToKeep>0){
556  rewind(theList);
557  for(int iMacro=0; iMacro<nMacros; iMacro++){
558  cout << "iMacro = " << iMacro << endl;
559  if (!feof(theList)){
560  fscanf(theList,"%s",&nameMacro);
561  cout << "macroToKeep[" << iMacroToKeep << "]=" << macroToKeep[iMacroToKeep] << endl;
562  if (iMacro==macroToKeep[iMacroToKeep]){
563  if(strstr(nameMacro,baseName)){
564  fullNameMacro = TString(dirName);
565  fullNameMacro+="/";
566  fullNameMacro+=nameMacro;
567  printf("Reading macro: %s\n",fullNameMacro.Data());
568 
569  char* sdatetime=strstr(nameMacro,"."); sdatetime++;
570  TString tsdatetime(sdatetime);
571  tsdatetime = tsdatetime.ReplaceAll(".C","");
572  tsdatetime = tsdatetime.ReplaceAll("."," ");
573  tsdatetime = tsdatetime.Insert(4,"-");
574  tsdatetime = tsdatetime.Insert(7,"-");
575  tsdatetime = tsdatetime.Insert(13,":");
576  tsdatetime = tsdatetime.Insert(16,":");
577  sdatetime = tsdatetime.Data();
578  mgr->setRequestTime(sdatetime);
579  uidatetime = mgr->getUnixRequestTime();
580  cout << "sdatetime=" << sdatetime << " uidatetime=" << uidatetime << endl;
581 
582  if(iMacroToKeep==0){
583  // Get previous DV entry in DB
584  getDriftVelocityDB(uidatetime,uidatetimedb,laseredvdb);
585 
586  dt = -(double) uidatetimedb;
587  dDV = -laseredvdb;
588  }
589 
590  // Load Macro to get dv value
591  gROOT->LoadMacro(fullNameMacro.Data());
592  tpcDV = (St_tpcDriftVelocity*)CreateTable();
593  if(!(tpcDV && tpcDV->HasData())) {
594  cout << "No TTable returned!" << endl;
595  continue;
596  }
597  tpcDVs = (tpcDriftVelocity_st*)tpcDV->At(0);
598 
599  // although in double, there is something funny here
600  cout << "dt=" << dt << " uidatetime=" << uidatetime << " -> " << (double) uidatetime << endl;
601  dt += (double) uidatetime;
602  cout << "Now dt=" << dt << endl;
603 
604 
605  dDV += tpcDVs->laserDriftVelocityEast;
606 
607  //dDVdt = TMath::Abs(dDV/dt);
608  if (dt==0){
609  if(dDV==0) dDVdt = 0;
610  else dDVdt = maxdDVdt+1;
611  } else {
612  dDVdt = TMath::Abs(dDV/dt);
613  }
614 
615  cout << "dDV/dt=" << dDV << "/" << dt << "=" << dDVdt << endl;
616 
617  if(dDVdt<maxdDVdt){
618  macroKept[nMacroKept++] = iMacro;
619  int jMacro=iMacro;
620  while(macroStatus[++jMacro]==iMacro){
621  macroStatus[jMacro]=3; // A macro in run was selected for db loading
622  }
623  jMacro=iMacro;
624  while(macroStatus[--jMacro]==iMacro){
625  macroStatus[jMacro]=3; // A macro in run was selected for db loading
626  }
627  macroStatus[iMacro]=0; // Macro selected for db loading
628  // This macro is the new reference for dDV/dt
629  dt=-(double)uidatetime;
630  dDV=-tpcDVs->laserDriftVelocityEast;
631  }
632  else{
633  int jMacro=iMacro;
634  while(macroStatus[++jMacro]==iMacro){
635  macroStatus[jMacro]=4; // Selected macro in run show large dDV/dt
636  }
637  jMacro=iMacro;
638  while(macroStatus[--jMacro]==iMacro){
639  macroStatus[jMacro]=4; // Selected macro in run show large dDV/dt
640  }
641  macroStatus[iMacro]=5; // Macro show large dDV/dt
642  // Keep prevoius reference for dDV/dt
643  dt-=(double)uidatetime;
644  dDV-=tpcDVs->laserDriftVelocityEast;
645  }
646  // gROOT->DeleteAll();
647  gInterpreter->ResetGlobals();
648  iMacroToKeep++;
649  }
650  }
651  }
652  }
653 
654  // Moved before ddv/dt check
655 // TString LoadDone(dirName); LoadDone +="/Load/Done/";
656 // TString LoadFailed(dirName); LoadFailed +="/Load/Failed/";
657 // TString LoadOthers(dirName); LoadOthers +="/Load/Others/";
658 // TString CheckBadRun(dirName); CheckBadRun+="/Check/BadRun/";
659 // TString CheckVarSel(dirName); CheckVarSel+="/Check/VarSel/";
660 // TString CheckVarOth(dirName); CheckVarOth+="/Check/VarOth/";
661 
662 
663  // Prepare to write to the DB
664  //gSystem->Setenv("DB_ACCESS_MODE","write");
665 
666  // --> create a modifier object and set up table definitions
667  StDbModifier *dm = new StDbModifier();
668  dm->SetDbName("Calibrations_tpc");
669  dm->SetFlavor("laserDV");
670  dm->SetTableName("tpcDriftVelocity");
671 
672 
673  // One more Loop!
674  rewind(theList);
675  for(int iMacro=0; iMacro<nMacros; iMacro++){
676  cout << "iMacro = " << iMacro << endl;
677  if (!feof(theList)){
678  fscanf(theList,"%s",&nameMacro);
679  cout << "macroStatus[" << iMacro << "]=" << macroStatus[iMacro] << endl;
680  if(strstr(nameMacro,baseName)){
681  fullNameMacro = TString(dirName);
682  fullNameMacro+="/";
683  fullNameMacro+=nameMacro;
684  TString newFile;
685  switch (macroStatus[iMacro]) {
686  case 0:
687  dm->SetInputFileName(fullNameMacro.Data());
688  char* sdatetime=strstr(nameMacro,"."); sdatetime++;
689  TString tsdatetime(sdatetime);
690  tsdatetime = tsdatetime.ReplaceAll(".C","");
691  tsdatetime = tsdatetime.ReplaceAll("."," ");
692  tsdatetime = tsdatetime.Insert(4,"-");
693  tsdatetime = tsdatetime.Insert(7,"-");
694  tsdatetime = tsdatetime.Insert(13,":");
695  tsdatetime = tsdatetime.Insert(16,":");
696  sdatetime = tsdatetime.Data();
697  dm->SetDateTime(sdatetime);
698  cout << "Loading " << fullNameMacro.Data() << " ... ";
699  if(dm->WriteDataToDB()==1){
700  cout << " Succeeded!" << endl;
701  newFile+=LoadDone;
702  nSuccess++;
703  }
704  else {
705  cout << " Failed!" << endl;
706  newFile+=LoadFailed;
707  nFailed++;
708  }
709  newFile+=nameMacro;
710  break;
711  case 1:
712  newFile = TString(dirName);
713  newFile+="/";
714  newFile+=nameMacro;
715  break;
716  case 2:
717  newFile+=CheckBadRun;
718  newFile+=nameMacro;
719  break;
720  case 3:
721  newFile+=LoadOthers;
722  newFile+=nameMacro;
723  break;
724  case 4:
725  newFile+=CheckVarOth;
726  newFile+=nameMacro;
727  break;
728  case 5:
729  newFile+=CheckVarSel;
730  newFile+=nameMacro;
731  break;
732  default:
733  newFile = TString(dirName);
734  newFile+="/";
735  newFile+=nameMacro;
736  }
737 
738  // Move macro to appropriate directory
739  cout << newFile.Data() << endl;
740  if(rename(fullNameMacro.Data(),newFile.Data()))
741  cout << "Error moving file to " << newFile.Data() << endl;
742  FILE *FI;
743  if( (FI = fopen(fullNameMacro.Data(),"r")) != NULL){
744  cout << "File " << fullNameMacro.Data() << " remained at its original place " << endl;
745  fclose(FI);
746  }
747  }
748  }
749  }
750  }
751 
752  double *xLaserDVEk = new double[nMacroKept];
753  double *yLaserDVEk = new double[nMacroKept];
754 
755  double *xLaserDVWk = new double[nMacroKept];
756  double *yLaserDVWk = new double[nMacroKept];
757 
758  for(int iMacro=0; iMacro<nMacroKept; iMacro++){
759  xLaserDVEk[iMacro] = xLaserDVE[macroKept[iMacro]];
760  yLaserDVEk[iMacro] = yLaserDVE[macroKept[iMacro]];
761  xLaserDVWk[iMacro] = xLaserDVW[macroKept[iMacro]];
762  yLaserDVWk[iMacro] = yLaserDVW[macroKept[iMacro]];
763  }
764 
765  TGraph *tpcLaserDriftVelocityEast = new TGraph(nMacros,xLaserDVE,yLaserDVE);
766  TGraph *tpcLaserDriftVelocityWest = new TGraph(nMacros,xLaserDVW,yLaserDVW);
767 
768  TGraph *tpcLaserDriftVelocityEastK = new TGraph(nMacroKept,xLaserDVEk,yLaserDVEk);
769  TGraph *tpcLaserDriftVelocityWestK = new TGraph(nMacroKept,xLaserDVWk,yLaserDVWk);
770 
771  TString Load(dirName); Load+="/Load/";
772  Load+="tpcLaserDV_Auto.root";
773  TFile *hFile = new TFile(Load.Data(),"RECREATE");
774 
775  tpcLaserDriftVelocityEast->Write("tpcLaserDriftVelocityEast");
776  tpcLaserDriftVelocityWest->Write("tpcLaserDriftVelocityWest");
777  tpcLaserDriftVelocityEastK->Write("tpcLaserDriftVelocityEastK");
778  tpcLaserDriftVelocityWestK->Write("tpcLaserDriftVelocityWestK");
779 
780  hFile->Close();
781 
782  TCanvas cvn1;
783  cvn1.cd();
784 
785  TH1F *oneDHisto = new TH1F("oneDHisto","oneDHisto",100,xLaserDVE[0]-100,xLaserDVE[nMacros-1]+100);
786 
787  oneDHisto->SetMaximum(5.6);
788  oneDHisto->SetMinimum(5.4);
789  oneDHisto->SetTitle("TPC Laser Drift Velocity");
790  oneDHisto->SetXTitle("Time (unix)");
791  oneDHisto->SetYTitle("dv (cm/#mus)");
792  oneDHisto->Draw();
793 
794  tpcLaserDriftVelocityWest->SetMarkerStyle(23);
795  tpcLaserDriftVelocityWest->SetMarkerColor(2);
796  //tpcLaserDriftVelocityWest->Draw("PL");
797 
798  tpcLaserDriftVelocityEast->SetMarkerStyle(22);
799  tpcLaserDriftVelocityEast->SetMarkerColor(4);
800  tpcLaserDriftVelocityEast->Draw("PL");
801 
802  tpcLaserDriftVelocityEastK->SetMarkerStyle(22);
803  tpcLaserDriftVelocityEastK->Draw("PL");
804  // tpcLaserDriftVelocityWest->Draw("PL");
805 
806  cvn1.Update();
807  Load.ReplaceAll(".root",".eps");
808  cvn1.SaveAs(Load.Data());
809 
810  cout << endl << endl << " ******************************************** " << endl;
811  cout << " Number of Macros attempted = " << nMacros << endl;
812  cout << nSuccess << " loaded to DB and " << nFailed << " failed to be load!" << endl;
813 
814 }
815 
static TTable * New(const Char_t *name, const Char_t *type, void *array, UInt_t size)
This static method creates a new TTable object if provided.
Definition: TTable.cxx:1515
static StDbManager * Instance()
strdup(..) is not ANSI
Definition: StDbManager.cc:155
const void * At(Int_t i) const
Returns a pointer to the i-th row of the table.
Definition: TTable.cxx:303